1
0
mirror of https://github.com/golang/go synced 2024-11-06 03:26:15 -07:00
go/internal/telemetry/event/event.go

113 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 event provides support for event based telemetry.
package event
import (
"fmt"
"time"
)
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
)
// sTags is used to hold a small number of tags inside an event whichout
// requiring a separate allocation.
// As tags are often on the stack, this avoids 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 tags (one for each value and
// one for the message itself).
type sTags [3]Tag
// Event holds the information about an event of note that ocurred.
type Event struct {
At time.Time
typ eventType
static sTags // inline storage for the first few tags
dynamic []Tag // dynamically sized storage for remaining tags
}
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
// eventTagMap implements TagMap for a the tags of an Event.
type eventTagMap struct {
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) {
tagMap := ev.Map()
if !ev.At.IsZero() {
fmt.Fprint(f, ev.At.Format("2006/01/02 15:04:05 "))
}
msg := Msg.Get(tagMap)
err := Err.Get(tagMap)
fmt.Fprint(f, msg)
if err != nil {
if f.Flag('+') {
fmt.Fprintf(f, ": %+v", err)
} else {
fmt.Fprintf(f, ": %v", err)
}
}
for it := ev.Tags(); it.Valid(); it.Advance() {
tag := it.Tag()
// msg and err were both already printed above, so we skip them to avoid
// double printing
if tag.Key == Msg || tag.Key == Err {
continue
}
fmt.Fprintf(f, "\n\t%v", tag)
}
}
func (ev Event) Tags() TagIterator {
return ChainTagIterators(
NewTagIterator(ev.static[:]...),
NewTagIterator(ev.dynamic...))
}
func (ev Event) Map() TagMap {
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 &eventTagMap{event: ev}
}
func (m *eventTagMap) Find(key interface{}) Tag {
for _, tag := range m.event.static {
if tag.Key == key {
return tag
}
}
for _, tag := range m.event.dynamic {
if tag.Key == key {
return tag
}
}
return Tag{}
}
func makeEvent(typ eventType, static sTags, tags []Tag) Event {
return Event{
typ: typ,
static: static,
dynamic: tags,
}
}