From a466788a31bba36a089a089300e6e07d6aac7a68 Mon Sep 17 00:00:00 2001 From: Ian Cottrell Date: Mon, 20 Apr 2020 23:00:05 -0400 Subject: [PATCH] internal/event: change event.At to be a private field This was the last piece of Event that was public, and it was only public to allow mutation in tests. Adding CloneEvent allows tests to create an updated copy rather than update the event in place. Change-Id: I2215d1eb0317063948ef0fac955fa768a209564d Reviewed-on: https://go-review.googlesource.com/c/tools/+/229241 Run-TryBot: Ian Cottrell TryBot-Result: Gobot Gobot Reviewed-by: Robert Findley --- internal/event/core/event.go | 14 +++++++++++--- internal/event/core/export.go | 2 +- internal/event/export/log.go | 4 ++-- internal/event/export/log_test.go | 4 ++-- internal/event/export/metric/exporter.go | 2 +- internal/event/export/ocagent/ocagent.go | 6 +++--- internal/event/export/ocagent/ocagent_test.go | 6 +++--- internal/lsp/debug/rpc.go | 2 +- internal/lsp/debug/trace.go | 8 ++++---- 9 files changed, 28 insertions(+), 20 deletions(-) diff --git a/internal/event/core/event.go b/internal/event/core/event.go index 2f979bdf1e..29f293e62d 100644 --- a/internal/event/core/event.go +++ b/internal/event/core/event.go @@ -26,7 +26,7 @@ const ( // Event holds the information about an event of note that ocurred. type Event struct { - At time.Time + at time.Time typ eventType @@ -47,6 +47,8 @@ type eventLabelMap struct { event Event } +func (ev Event) At() time.Time { return ev.at } + func (ev Event) IsLog() bool { return ev.typ == LogType } func (ev Event) IsEndSpan() bool { return ev.typ == EndSpanType } func (ev Event) IsStartSpan() bool { return ev.typ == StartSpanType } @@ -55,8 +57,8 @@ func (ev Event) IsDetach() bool { return ev.typ == DetachType } func (ev Event) IsRecord() bool { return ev.typ == RecordType } func (ev Event) Format(f fmt.State, r rune) { - if !ev.At.IsZero() { - fmt.Fprint(f, ev.At.Format("2006/01/02 15:04:05 ")) + if !ev.at.IsZero() { + fmt.Fprint(f, ev.at.Format("2006/01/02 15:04:05 ")) } for index := 0; ev.Valid(index); index++ { l := ev.Label(index) @@ -96,3 +98,9 @@ func MakeEvent(typ eventType, static [3]label.Label, labels []label.Label) Event dynamic: labels, } } + +// CloneEvent event returns a copy of the event with the time adjusted to at. +func CloneEvent(ev Event, at time.Time) Event { + ev.at = at + return ev +} diff --git a/internal/event/core/export.go b/internal/event/core/export.go index f006ed14f6..05f3a9a579 100644 --- a/internal/event/core/export.go +++ b/internal/event/core/export.go @@ -40,7 +40,7 @@ func SetExporter(e Exporter) { // it will fill in the time. func deliver(ctx context.Context, exporter Exporter, ev Event) context.Context { // add the current time to the event - ev.At = time.Now() + ev.at = time.Now() // hand the event off to the current exporter return exporter(ctx, ev, ev) } diff --git a/internal/event/export/log.go b/internal/event/export/log.go index cf1708ef54..93abae49e6 100644 --- a/internal/event/export/log.go +++ b/internal/event/export/log.go @@ -42,8 +42,8 @@ func (w *logWriter) ProcessEvent(ctx context.Context, ev core.Event, lm label.Ma defer w.mu.Unlock() buf := w.buffer[:0] - if !ev.At.IsZero() { - w.writer.Write(ev.At.AppendFormat(buf, "2006/01/02 15:04:05 ")) + if !ev.At().IsZero() { + w.writer.Write(ev.At().AppendFormat(buf, "2006/01/02 15:04:05 ")) } msg := keys.Msg.Get(lm) io.WriteString(w.writer, msg) diff --git a/internal/event/export/log_test.go b/internal/event/export/log_test.go index c1f6988425..8b553dbffe 100644 --- a/internal/event/export/log_test.go +++ b/internal/event/export/log_test.go @@ -34,7 +34,7 @@ func ExampleLog() { func timeFixer(output event.Exporter) event.Exporter { at, _ := time.Parse(time.RFC3339Nano, "2020-03-05T14:27:48Z") return func(ctx context.Context, ev core.Event, lm label.Map) context.Context { - ev.At = at - return output(ctx, ev, lm) + copy := core.CloneEvent(ev, at) + return output(ctx, copy, lm) } } diff --git a/internal/event/export/metric/exporter.go b/internal/event/export/metric/exporter.go index 87563a41b3..121b0ee1e8 100644 --- a/internal/event/export/metric/exporter.go +++ b/internal/event/export/metric/exporter.go @@ -48,7 +48,7 @@ func (e *Config) Exporter(output event.Exporter) event.Exporter { id := l.Key() if list := e.subscribers[id]; len(list) > 0 { for _, s := range list { - metrics = append(metrics, s(ev.At, lm, l)) + metrics = append(metrics, s(ev.At(), lm, l)) } } } diff --git a/internal/event/export/ocagent/ocagent.go b/internal/event/export/ocagent/ocagent.go index b1396f9e5b..46acca6f75 100644 --- a/internal/event/export/ocagent/ocagent.go +++ b/internal/event/export/ocagent/ocagent.go @@ -200,8 +200,8 @@ func convertSpan(span *export.Span) *wire.Span { ParentSpanID: span.ParentID[:], Name: toTruncatableString(span.Name), Kind: wire.UnspecifiedSpanKind, - StartTime: convertTimestamp(span.Start().At), - EndTime: convertTimestamp(span.Finish().At), + StartTime: convertTimestamp(span.Start().At()), + EndTime: convertTimestamp(span.Finish().At()), Attributes: convertAttributes(label.Filter(span.Start(), keys.Name)), TimeEvents: convertEvents(span.Events()), SameProcessAsParentSpan: true, @@ -307,7 +307,7 @@ func convertEvents(events []core.Event) *wire.TimeEvents { func convertEvent(ev core.Event) wire.TimeEvent { return wire.TimeEvent{ - Time: convertTimestamp(ev.At), + Time: convertTimestamp(ev.At()), Annotation: convertAnnotation(ev), } } diff --git a/internal/event/export/ocagent/ocagent_test.go b/internal/event/export/ocagent/ocagent_test.go index fa88f35d51..0979f530bb 100644 --- a/internal/event/export/ocagent/ocagent_test.go +++ b/internal/event/export/ocagent/ocagent_test.go @@ -130,11 +130,11 @@ func timeFixer(output event.Exporter) event.Exporter { return func(ctx context.Context, ev core.Event, lm label.Map) context.Context { switch { case ev.IsStartSpan(): - ev.At = start + ev = core.CloneEvent(ev, start) case ev.IsEndSpan(): - ev.At = end + ev = core.CloneEvent(ev, end) default: - ev.At = at + ev = core.CloneEvent(ev, at) } return output(ctx, ev, lm) } diff --git a/internal/lsp/debug/rpc.go b/internal/lsp/debug/rpc.go index de90a6cf32..f15f6cabb6 100644 --- a/internal/lsp/debug/rpc.go +++ b/internal/lsp/debug/rpc.go @@ -128,7 +128,7 @@ func endRPC(ctx context.Context, ev core.Event, span *export.Span, stats *rpcSta } // calculate latency if this was an rpc span - elapsedTime := span.Finish().At.Sub(span.Start().At) + elapsedTime := span.Finish().At().Sub(span.Start().At()) latencyMillis := timeUnits(elapsedTime) / timeUnits(time.Millisecond) if stats.Latency.Count == 0 { stats.Latency.Min = latencyMillis diff --git a/internal/lsp/debug/trace.go b/internal/lsp/debug/trace.go index 8213df758d..36dd530f60 100644 --- a/internal/lsp/debug/trace.go +++ b/internal/lsp/debug/trace.go @@ -94,7 +94,7 @@ func (t *traces) ProcessEvent(ctx context.Context, ev core.Event, lm label.Map) SpanID: span.ID.SpanID, ParentID: span.ParentID, Name: span.Name, - Start: span.Start().At, + Start: span.Start().At(), Tags: renderLabels(span.Start()), } t.unfinished[span.ID] = td @@ -118,13 +118,13 @@ func (t *traces) ProcessEvent(ctx context.Context, ev core.Event, lm label.Map) } delete(t.unfinished, span.ID) - td.Finish = span.Finish().At - td.Duration = span.Finish().At.Sub(span.Start().At) + td.Finish = span.Finish().At() + td.Duration = span.Finish().At().Sub(span.Start().At()) events := span.Events() td.Events = make([]traceEvent, len(events)) for i, event := range events { td.Events[i] = traceEvent{ - Time: event.At, + Time: event.At(), Tags: renderLabels(event), } }