internal/telemetry: remove the ProcessEvent function

There is no reason for it to be public now, it is no longer possible to build an
event
without using the helpers.
This also allows us to delay setting the time until after the nil exporter
check, for
a significant time saving in the no exporter case.

name                old time/op    new time/op    delta
/Baseline-8            588ns ± 4%     575ns ± 4%   -2.24%  (p=0.000 n=18+18)
/StdLog-8             9.61µs ± 3%    9.58µs ± 3%     ~     (p=0.384 n=18+18)
/LogNoExporter-8      3.94µs ± 2%    2.26µs ± 2%  -42.50%  (p=0.000 n=17+18)
/TraceNoExporter-8    2.77µs ± 4%    1.11µs ± 2%  -59.82%  (p=0.000 n=18+18)
/StatsNoExporter-8    3.83µs ± 5%    2.15µs ± 3%  -43.70%  (p=0.000 n=18+17)
/Log-8                23.3µs ± 6%    23.0µs ± 1%     ~     (p=0.245 n=18+17)
/Trace-8              26.4µs ± 3%    26.5µs ± 4%     ~     (p=0.269 n=18+17)
/Stats-8              5.36µs ± 2%    5.45µs ± 3%   +1.68%  (p=0.000 n=17+18)

Change-Id: Ibde0e20eaf99d03f786cd1436f05eab7b2a17b20
Reviewed-on: https://go-review.googlesource.com/c/tools/+/224657
Run-TryBot: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
diff --git a/internal/telemetry/event/export.go b/internal/telemetry/event/export.go
index 82afb89..fd47027 100644
--- a/internal/telemetry/event/export.go
+++ b/internal/telemetry/event/export.go
@@ -7,6 +7,7 @@
 import (
 	"context"
 	"sync/atomic"
+	"time"
 	"unsafe"
 )
 
@@ -33,12 +34,16 @@
 	atomic.StorePointer(&exporter, p)
 }
 
-// ProcessEvent is called to deliver an event to the global exporter.
-func ProcessEvent(ctx context.Context, ev Event) context.Context {
+// dispatch is called to deliver an event to the supplied exporter.
+// it will fill in the time and generate the basic tag source.
+func dispatch(ctx context.Context, ev Event) context.Context {
+	// get the global exporter and abort early if there is not one
 	exporterPtr := (*Exporter)(atomic.LoadPointer(&exporter))
 	if exporterPtr == nil {
 		return ctx
 	}
-	// and now also hand the event of to the current exporter
+	// add the current time to the event
+	ev.At = time.Now()
+	// hand the event off to the current exporter
 	return (*exporterPtr)(ctx, ev, ev.Map())
 }
diff --git a/internal/telemetry/event/label.go b/internal/telemetry/event/label.go
index 7faec1e..5892e7f 100644
--- a/internal/telemetry/event/label.go
+++ b/internal/telemetry/event/label.go
@@ -6,14 +6,12 @@
 
 import (
 	"context"
-	"time"
 )
 
 // Label sends a label event to the exporter with the supplied tags.
 func Label(ctx context.Context, tags ...Tag) context.Context {
-	return ProcessEvent(ctx, Event{
+	return dispatch(ctx, Event{
 		Type: LabelType,
-		At:   time.Now(),
 		tags: tags,
 	})
 }
diff --git a/internal/telemetry/event/log.go b/internal/telemetry/event/log.go
index 6029585..abbe220 100644
--- a/internal/telemetry/event/log.go
+++ b/internal/telemetry/event/log.go
@@ -7,14 +7,12 @@
 import (
 	"context"
 	"errors"
-	"time"
 )
 
 // Log sends a log event with the supplied tag list to the exporter.
 func Log(ctx context.Context, tags ...Tag) {
-	ProcessEvent(ctx, Event{
+	dispatch(ctx, Event{
 		Type: LogType,
-		At:   time.Now(),
 		tags: tags,
 	})
 }
@@ -22,9 +20,8 @@
 // Print takes a message and a tag list and combines them into a single event
 // before delivering them to the exporter.
 func Print(ctx context.Context, message string, tags ...Tag) {
-	ProcessEvent(ctx, Event{
+	dispatch(ctx, Event{
 		Type:    LogType,
-		At:      time.Now(),
 		Message: message,
 		tags:    tags,
 	})
@@ -38,9 +35,8 @@
 		err = errors.New(message)
 		message = ""
 	}
-	ProcessEvent(ctx, Event{
+	dispatch(ctx, Event{
 		Type:    LogType,
-		At:      time.Now(),
 		Message: message,
 		Error:   err,
 		tags:    tags,
diff --git a/internal/telemetry/event/metric.go b/internal/telemetry/event/metric.go
index 7d4aaa5..23af8d5 100644
--- a/internal/telemetry/event/metric.go
+++ b/internal/telemetry/event/metric.go
@@ -6,13 +6,11 @@
 
 import (
 	"context"
-	"time"
 )
 
 func Record(ctx context.Context, tags ...Tag) {
-	ProcessEvent(ctx, Event{
+	dispatch(ctx, Event{
 		Type: RecordType,
-		At:   time.Now(),
 		tags: tags,
 	})
 }
diff --git a/internal/telemetry/event/trace.go b/internal/telemetry/event/trace.go
index 878ca4a..57095ab 100644
--- a/internal/telemetry/event/trace.go
+++ b/internal/telemetry/event/trace.go
@@ -6,20 +6,17 @@
 
 import (
 	"context"
-	"time"
 )
 
 func StartSpan(ctx context.Context, name string, tags ...Tag) (context.Context, func()) {
-	ctx = ProcessEvent(ctx, Event{
+	ctx = dispatch(ctx, Event{
 		Type:    StartSpanType,
 		Message: name,
-		At:      time.Now(),
 		tags:    tags,
 	})
 	return ctx, func() {
-		ProcessEvent(ctx, Event{
+		dispatch(ctx, Event{
 			Type: EndSpanType,
-			At:   time.Now(),
 		})
 	}
 }
@@ -27,8 +24,5 @@
 // Detach returns a context without an associated span.
 // This allows the creation of spans that are not children of the current span.
 func Detach(ctx context.Context) context.Context {
-	return ProcessEvent(ctx, Event{
-		Type: DetachType,
-		At:   time.Now(),
-	})
+	return dispatch(ctx, Event{Type: DetachType})
 }