1 // Copyright 2019 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Package event provides support for event based telemetry.
16 invalidType = eventType(iota)
17 LogType // an event that should be recorded in a log
18 StartSpanType // the start of a span of time
19 EndSpanType // the end of a span of time
20 LabelType // some values that should be noted for later events
21 DetachType // an event that causes a context to detach
22 RecordType // a value that should be tracked
25 // sTags is used to hold a small number of tags inside an event whichout
26 // requiring a separate allocation.
27 // As tags are often on the stack, this avoids an allocation at all for
28 // the very common cases of simple events.
29 // The length needs to be large enough to cope with the majority of events
30 // but no so large as to cause undue stack pressure.
31 // A log message with two values will use 3 tags (one for each value and
32 // one for the message itself).
35 // Event holds the information about an event of note that ocurred.
40 static sTags // inline storage for the first few tags
41 dynamic []Tag // dynamically sized storage for remaining tags
44 // eventTagMap implements TagMap for a the tags of an Event.
45 type eventTagMap struct {
49 func (ev Event) IsLog() bool { return ev.typ == LogType }
50 func (ev Event) IsEndSpan() bool { return ev.typ == EndSpanType }
51 func (ev Event) IsStartSpan() bool { return ev.typ == StartSpanType }
52 func (ev Event) IsLabel() bool { return ev.typ == LabelType }
53 func (ev Event) IsDetach() bool { return ev.typ == DetachType }
54 func (ev Event) IsRecord() bool { return ev.typ == RecordType }
56 func (ev Event) Format(f fmt.State, r rune) {
59 fmt.Fprint(f, ev.At.Format("2006/01/02 15:04:05 "))
61 msg := Msg.Get(tagMap)
62 err := Err.Get(tagMap)
66 fmt.Fprintf(f, ": %+v", err)
68 fmt.Fprintf(f, ": %v", err)
71 for index := 0; ev.Valid(index); index++ {
73 // msg and err were both already printed above, so we skip them to avoid
75 if !tag.Valid() || tag.Key() == Msg || tag.Key() == Err {
78 fmt.Fprintf(f, "\n\t%v", tag)
82 func (ev Event) Valid(index int) bool {
83 return index >= 0 && index < len(ev.static)+len(ev.dynamic)
86 func (ev Event) Tag(index int) Tag {
87 if index < len(ev.static) {
88 return ev.static[index]
90 return ev.dynamic[index-len(ev.static)]
93 func (ev Event) Find(key Key) Tag {
94 for _, tag := range ev.static {
99 for _, tag := range ev.dynamic {
100 if tag.Key() == key {
107 func makeEvent(typ eventType, static sTags, tags []Tag) Event {