1
0
mirror of https://github.com/golang/go synced 2024-11-18 08:54:45 -07:00

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 <iancottrell@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
Ian Cottrell 2020-04-20 23:00:05 -04:00
parent a82abb5396
commit a466788a31
9 changed files with 28 additions and 20 deletions

View File

@ -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
}

View File

@ -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)
}

View File

@ -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)

View File

@ -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)
}
}

View File

@ -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))
}
}
}

View File

@ -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),
}
}

View File

@ -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)
}

View File

@ -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

View File

@ -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),
}
}