1
0
mirror of https://github.com/golang/go synced 2024-11-18 14:54:40 -07:00
go/internal/event/core/event.go

115 lines
3.0 KiB
Go
Raw Normal View History

// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package core provides support for event based telemetry.
package core
import (
"fmt"
"time"
"golang.org/x/tools/internal/event/label"
)
type eventType uint8
const (
invalidType = eventType(iota)
LogType // an event that should be recorded in a log
StartSpanType // the start of a span of time
EndSpanType // the end of a span of time
LabelType // some values that should be noted for later events
DetachType // an event that causes a context to detach
RecordType // a value that should be tracked
)
// Event holds the information about an event of note that ocurred.
type Event struct {
At time.Time
typ eventType
// As events are often on the stack, storing the first few labels directly
// in the event can avoid an allocation at all for the very common cases of
// simple events.
// The length needs to be large enough to cope with the majority of events
// but no so large as to cause undue stack pressure.
// A log message with two values will use 3 labels (one for each value and
// one for the message itself).
static [3]label.Label // inline storage for the first few labels
dynamic []label.Label // dynamically sized storage for remaining labels
}
// eventLabelMap implements label.Map for a the labels of an Event.
type eventLabelMap struct {
internal/telemetry: give Event a custom implementation of TagMap This cuts the allocation cost of preparing an event for export from 4 down to 1. name old time/op new time/op delta /LogIgnore-8 5.97µs ± 6% 4.18µs ± 4% -29.87% (p=0.000 n=20+19) /TraceIgnore-8 13.6µs ± 4% 10.8µs ± 6% -20.92% (p=0.000 n=18+19) /StatsIgnore-8 11.3µs ± 6% 7.9µs ± 6% -30.29% (p=0.000 n=20+20) /Log-8 47.8µs ± 6% 43.9µs ± 6% -8.15% (p=0.000 n=20+19) /Trace-8 60.5µs ±12% 54.7µs ± 4% -9.58% (p=0.000 n=20+19) /Stats-8 13.8µs ± 3% 10.6µs ± 5% -23.57% (p=0.000 n=20+19) name old alloc/op new alloc/op delta /LogIgnore-8 5.12kB ± 0% 3.58kB ± 0% -30.00% (p=0.000 n=20+20) /TraceIgnore-8 14.6kB ± 0% 11.5kB ± 0% -21.05% (p=0.000 n=20+20) /StatsIgnore-8 10.2kB ± 0% 7.2kB ± 0% -30.00% (p=0.000 n=20+20) /Log-8 24.0kB ± 0% 20.9kB ± 0% -12.81% (p=0.000 n=18+20) /Trace-8 31.0kB ± 0% 27.9kB ± 0% -9.92% (p=0.000 n=20+20) /Stats-8 10.2kB ± 0% 7.2kB ± 0% -30.00% (p=0.000 n=20+20) name old allocs/op new allocs/op delta /LogIgnore-8 64.0 ± 0% 16.0 ± 0% -75.00% (p=0.000 n=20+20) /TraceIgnore-8 160 ± 0% 64 ± 0% -60.00% (p=0.000 n=20+20) /StatsIgnore-8 128 ± 0% 32 ± 0% -75.00% (p=0.000 n=20+20) /Log-8 382 ± 0% 286 ± 0% -25.13% (p=0.000 n=20+20) /Trace-8 480 ± 0% 384 ± 0% -20.00% (p=0.000 n=20+20) /Stats-8 128 ± 0% 32 ± 0% -75.00% (p=0.000 n=20+20) Change-Id: I76ff4ac9658b766daf3929e7cfc0d2cfcab036ed Reviewed-on: https://go-review.googlesource.com/c/tools/+/226359 Run-TryBot: Ian Cottrell <iancottrell@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
2020-03-30 06:09:07 -06:00
event Event
}
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 }
func (ev Event) IsLabel() bool { return ev.typ == LabelType }
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) {
lm := label.Map(ev)
if !ev.At.IsZero() {
fmt.Fprint(f, ev.At.Format("2006/01/02 15:04:05 "))
}
msg := Msg.Get(lm)
err := Err.Get(lm)
fmt.Fprint(f, msg)
if err != nil {
if f.Flag('+') {
fmt.Fprintf(f, ": %+v", err)
} else {
fmt.Fprintf(f, ": %v", err)
}
}
for index := 0; ev.Valid(index); index++ {
l := ev.Label(index)
// msg and err were both already printed above, so we skip them to avoid
// double printing
if !l.Valid() || l.Key() == Msg || l.Key() == Err {
continue
}
fmt.Fprintf(f, "\n\t%v", l)
}
}
func (ev Event) Valid(index int) bool {
return index >= 0 && index < len(ev.static)+len(ev.dynamic)
}
func (ev Event) Label(index int) label.Label {
if index < len(ev.static) {
return ev.static[index]
}
return ev.dynamic[index-len(ev.static)]
}
func (ev Event) Find(key label.Key) label.Label {
for _, l := range ev.static {
if l.Key() == key {
return l
internal/telemetry: give Event a custom implementation of TagMap This cuts the allocation cost of preparing an event for export from 4 down to 1. name old time/op new time/op delta /LogIgnore-8 5.97µs ± 6% 4.18µs ± 4% -29.87% (p=0.000 n=20+19) /TraceIgnore-8 13.6µs ± 4% 10.8µs ± 6% -20.92% (p=0.000 n=18+19) /StatsIgnore-8 11.3µs ± 6% 7.9µs ± 6% -30.29% (p=0.000 n=20+20) /Log-8 47.8µs ± 6% 43.9µs ± 6% -8.15% (p=0.000 n=20+19) /Trace-8 60.5µs ±12% 54.7µs ± 4% -9.58% (p=0.000 n=20+19) /Stats-8 13.8µs ± 3% 10.6µs ± 5% -23.57% (p=0.000 n=20+19) name old alloc/op new alloc/op delta /LogIgnore-8 5.12kB ± 0% 3.58kB ± 0% -30.00% (p=0.000 n=20+20) /TraceIgnore-8 14.6kB ± 0% 11.5kB ± 0% -21.05% (p=0.000 n=20+20) /StatsIgnore-8 10.2kB ± 0% 7.2kB ± 0% -30.00% (p=0.000 n=20+20) /Log-8 24.0kB ± 0% 20.9kB ± 0% -12.81% (p=0.000 n=18+20) /Trace-8 31.0kB ± 0% 27.9kB ± 0% -9.92% (p=0.000 n=20+20) /Stats-8 10.2kB ± 0% 7.2kB ± 0% -30.00% (p=0.000 n=20+20) name old allocs/op new allocs/op delta /LogIgnore-8 64.0 ± 0% 16.0 ± 0% -75.00% (p=0.000 n=20+20) /TraceIgnore-8 160 ± 0% 64 ± 0% -60.00% (p=0.000 n=20+20) /StatsIgnore-8 128 ± 0% 32 ± 0% -75.00% (p=0.000 n=20+20) /Log-8 382 ± 0% 286 ± 0% -25.13% (p=0.000 n=20+20) /Trace-8 480 ± 0% 384 ± 0% -20.00% (p=0.000 n=20+20) /Stats-8 128 ± 0% 32 ± 0% -75.00% (p=0.000 n=20+20) Change-Id: I76ff4ac9658b766daf3929e7cfc0d2cfcab036ed Reviewed-on: https://go-review.googlesource.com/c/tools/+/226359 Run-TryBot: Ian Cottrell <iancottrell@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
2020-03-30 06:09:07 -06:00
}
}
for _, l := range ev.dynamic {
if l.Key() == key {
return l
internal/telemetry: give Event a custom implementation of TagMap This cuts the allocation cost of preparing an event for export from 4 down to 1. name old time/op new time/op delta /LogIgnore-8 5.97µs ± 6% 4.18µs ± 4% -29.87% (p=0.000 n=20+19) /TraceIgnore-8 13.6µs ± 4% 10.8µs ± 6% -20.92% (p=0.000 n=18+19) /StatsIgnore-8 11.3µs ± 6% 7.9µs ± 6% -30.29% (p=0.000 n=20+20) /Log-8 47.8µs ± 6% 43.9µs ± 6% -8.15% (p=0.000 n=20+19) /Trace-8 60.5µs ±12% 54.7µs ± 4% -9.58% (p=0.000 n=20+19) /Stats-8 13.8µs ± 3% 10.6µs ± 5% -23.57% (p=0.000 n=20+19) name old alloc/op new alloc/op delta /LogIgnore-8 5.12kB ± 0% 3.58kB ± 0% -30.00% (p=0.000 n=20+20) /TraceIgnore-8 14.6kB ± 0% 11.5kB ± 0% -21.05% (p=0.000 n=20+20) /StatsIgnore-8 10.2kB ± 0% 7.2kB ± 0% -30.00% (p=0.000 n=20+20) /Log-8 24.0kB ± 0% 20.9kB ± 0% -12.81% (p=0.000 n=18+20) /Trace-8 31.0kB ± 0% 27.9kB ± 0% -9.92% (p=0.000 n=20+20) /Stats-8 10.2kB ± 0% 7.2kB ± 0% -30.00% (p=0.000 n=20+20) name old allocs/op new allocs/op delta /LogIgnore-8 64.0 ± 0% 16.0 ± 0% -75.00% (p=0.000 n=20+20) /TraceIgnore-8 160 ± 0% 64 ± 0% -60.00% (p=0.000 n=20+20) /StatsIgnore-8 128 ± 0% 32 ± 0% -75.00% (p=0.000 n=20+20) /Log-8 382 ± 0% 286 ± 0% -25.13% (p=0.000 n=20+20) /Trace-8 480 ± 0% 384 ± 0% -20.00% (p=0.000 n=20+20) /Stats-8 128 ± 0% 32 ± 0% -75.00% (p=0.000 n=20+20) Change-Id: I76ff4ac9658b766daf3929e7cfc0d2cfcab036ed Reviewed-on: https://go-review.googlesource.com/c/tools/+/226359 Run-TryBot: Ian Cottrell <iancottrell@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
2020-03-30 06:09:07 -06:00
}
}
return label.Label{}
}
func MakeEvent(typ eventType, static [3]label.Label, labels []label.Label) Event {
return Event{
typ: typ,
static: static,
dynamic: labels,
}
}