event: default to adding current time to events

The default should be that events are timestamped.
You can always use an explicit ExporterOptions to disable.

Change-Id: I508ad4a666eec0369cff9dbcd0abf0b7cc1e08ca
Reviewed-on: https://go-review.googlesource.com/c/exp/+/324633
Trust: Jonathan Amsterdam <jba@google.com>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
diff --git a/event/adapter/eventtest/eventtest.go b/event/adapter/eventtest/eventtest.go
index 9928955..616ffdc 100644
--- a/event/adapter/eventtest/eventtest.go
+++ b/event/adapter/eventtest/eventtest.go
@@ -22,7 +22,7 @@
 // NewContext returns a context you should use for the active test.
 func NewContext(ctx context.Context, tb testing.TB) context.Context {
 	h := &testHandler{tb: tb}
-	return event.WithExporter(ctx, event.NewExporter(h))
+	return event.WithExporter(ctx, event.NewExporter(h, nil))
 }
 
 type testHandler struct {
@@ -57,9 +57,9 @@
 	h.printer.Event(os.Stdout, ev)
 }
 
-func ExporterOptions() event.ExporterOptions {
+func ExporterOptions() *event.ExporterOptions {
 	nextTime, _ := time.Parse(time.RFC3339Nano, "2020-03-05T14:27:48Z")
-	return event.ExporterOptions{
+	return &event.ExporterOptions{
 		Now: func() time.Time {
 			thisTime := nextTime
 			nextTime = nextTime.Add(time.Second)
diff --git a/event/alloc_test.go b/event/alloc_test.go
index dc01b84..52c4500 100644
--- a/event/alloc_test.go
+++ b/event/alloc_test.go
@@ -19,7 +19,7 @@
 	anInt := event.Label{Name: "int", Value: event.Int64Of(4)}
 	aString := event.Label{Name: "string", Value: event.StringOf("value")}
 
-	e := event.NewExporter(logfmt.NewHandler(ioutil.Discard))
+	e := event.NewExporter(logfmt.NewHandler(ioutil.Discard), nil)
 	ctx := event.WithExporter(context.Background(), e)
 	allocs := int(testing.AllocsPerRun(5, func() {
 		event.To(ctx).With(aString).With(anInt).Log("message")
diff --git a/event/bench/event_test.go b/event/bench/event_test.go
index 8056b08..cd794d6 100644
--- a/event/bench/event_test.go
+++ b/event/bench/event_test.go
@@ -89,11 +89,11 @@
 }
 
 func eventNoop() context.Context {
-	return event.WithExporter(context.Background(), eventtest.ExporterOptions().NewExporter(noopHandler{}))
+	return event.WithExporter(context.Background(), event.NewExporter(noopHandler{}, eventtest.ExporterOptions()))
 }
 
 func eventPrint(w io.Writer) context.Context {
-	return event.WithExporter(context.Background(), eventtest.ExporterOptions().NewExporter(logfmt.NewHandler(w)))
+	return event.WithExporter(context.Background(), event.NewExporter(logfmt.NewHandler(w), eventtest.ExporterOptions()))
 }
 
 func BenchmarkLogEventNoExporter(b *testing.B) {
diff --git a/event/builder_test.go b/event/builder_test.go
index 65d429a..8e01474 100644
--- a/event/builder_test.go
+++ b/event/builder_test.go
@@ -22,7 +22,7 @@
 		labels = append(labels, keys.Int(fmt.Sprintf("l%d", i)).Of(i))
 	}
 
-	ctx := event.WithExporter(context.Background(), event.NewExporter(nil))
+	ctx := event.WithExporter(context.Background(), event.NewExporter(nil, nil))
 	b1 := event.To(ctx)
 	b1.With(labels[0]).With(labels[1])
 	check(t, b1, labels[:2])
@@ -65,7 +65,7 @@
 func TestTraceBuilder(t *testing.T) {
 	// Verify that the context returned from the handler is also returned from Start,
 	// and is the context passed to End.
-	ctx := event.WithExporter(context.Background(), event.NewExporter(&testTraceHandler{t}))
+	ctx := event.WithExporter(context.Background(), event.NewExporter(&testTraceHandler{t}, nil))
 	ctx, end := event.To(ctx).Start("s")
 	val := ctx.Value("x")
 	if val != 1 {
diff --git a/event/common_test.go b/event/common_test.go
index b86f9aa..1bbb4e3 100644
--- a/event/common_test.go
+++ b/event/common_test.go
@@ -15,7 +15,7 @@
 
 func TestCommon(t *testing.T) {
 	h := &catchHandler{}
-	ctx := event.WithExporter(context.Background(), event.NewExporter(h))
+	ctx := event.WithExporter(context.Background(), event.NewExporter(h, nil))
 
 	const simple = "simple message"
 	const trace = "a trace"
diff --git a/event/event_test.go b/event/event_test.go
index 1d830ed..e929438 100644
--- a/event/event_test.go
+++ b/event/event_test.go
@@ -98,7 +98,7 @@
 time=2020-03-05T14:27:49 myString="some string value" msg="string event"
 `}} {
 		buf := &strings.Builder{}
-		ctx := event.WithExporter(ctx, eventtest.ExporterOptions().NewExporter(logfmt.NewHandler(buf)))
+		ctx := event.WithExporter(ctx, event.NewExporter(logfmt.NewHandler(buf), eventtest.ExporterOptions()))
 		test.events(ctx)
 		got := strings.TrimSpace(buf.String())
 		expect := strings.TrimSpace(test.expect)
@@ -109,7 +109,7 @@
 }
 
 func ExampleLog() {
-	ctx := event.WithExporter(context.Background(), eventtest.ExporterOptions().NewExporter(logfmt.NewHandler(os.Stdout)))
+	ctx := event.WithExporter(context.Background(), event.NewExporter(logfmt.NewHandler(os.Stdout), eventtest.ExporterOptions()))
 	event.To(ctx).With(keys.Int("myInt").Of(6)).Log("my event")
 	event.To(ctx).With(keys.String("myString").Of("some string value")).Log("error event")
 	// Output:
diff --git a/event/export.go b/event/export.go
index 5366399..117ccf7 100644
--- a/event/export.go
+++ b/event/export.go
@@ -50,14 +50,17 @@
 	defaultExporter unsafe.Pointer
 )
 
-// NewExporter creates an Exporter using the supplied handler.
+// NewExporter creates an Exporter using the supplied handler and options.
 // Event delivery is serialized to enable safe atomic handling.
-func NewExporter(handler Handler) *Exporter {
-	return (ExporterOptions{}).NewExporter(handler)
-}
-
-func (opts ExporterOptions) NewExporter(handler Handler) *Exporter {
-	return &Exporter{opts: opts, handler: handler}
+func NewExporter(handler Handler, opts *ExporterOptions) *Exporter {
+	e := &Exporter{handler: handler}
+	if opts != nil {
+		e.opts = *opts
+	}
+	if e.opts.Now == nil {
+		e.opts.Now = time.Now
+	}
+	return e
 }
 
 func setDefaultExporter(e *Exporter) {
diff --git a/event/logging/internal/internal.go b/event/logging/internal/internal.go
index 55abb20..1f86804 100644
--- a/event/logging/internal/internal.go
+++ b/event/logging/internal/internal.go
@@ -34,6 +34,5 @@
 
 func NewTestExporter() (*event.Exporter, *TestHandler) {
 	te := &TestHandler{}
-	opts := event.ExporterOptions{Now: func() time.Time { return TestAt }}
-	return opts.NewExporter(te), te
+	return event.NewExporter(te, &event.ExporterOptions{Now: func() time.Time { return TestAt }}), te
 }
diff --git a/event/otel/trace_test.go b/event/otel/trace_test.go
index a7eea9c..fe05ef0 100644
--- a/event/otel/trace_test.go
+++ b/event/otel/trace_test.go
@@ -111,7 +111,7 @@
 	stp := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(bsp))
 	tracer := stp.Tracer("")
 
-	ee := event.NewExporter(otel.NewTraceHandler(tracer))
+	ee := event.NewExporter(otel.NewTraceHandler(tracer), nil)
 	ctx = event.WithExporter(ctx, ee)
 	return ctx, tracer, func() string { stp.Shutdown(ctx); return e.got }
 }
diff --git a/event/severity/severity_test.go b/event/severity/severity_test.go
index b2f0c8c..d4e045c 100644
--- a/event/severity/severity_test.go
+++ b/event/severity/severity_test.go
@@ -34,7 +34,7 @@
 		expect: `time=2020-03-05T14:27:48 level=info msg="a message"`},
 	} {
 		buf := &strings.Builder{}
-		ctx := event.WithExporter(ctx, eventtest.ExporterOptions().NewExporter(logfmt.NewHandler(buf)))
+		ctx := event.WithExporter(ctx, event.NewExporter(logfmt.NewHandler(buf), eventtest.ExporterOptions()))
 		test.events(ctx)
 		got := strings.TrimSpace(buf.String())
 		expect := strings.TrimSpace(test.expect)