| 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. |
| 4 | |
| 5 | // Package core provides support for event based telemetry. |
| 6 | package core |
| 7 | |
| 8 | import ( |
| 9 | "fmt" |
| 10 | "iter" |
| 11 | "time" |
| 12 | |
| 13 | "golang.org/x/tools/internal/event/label" |
| 14 | ) |
| 15 | |
| 16 | // Event holds the information about an event of note that occurred. |
| 17 | type Event struct { |
| 18 | at time.Time |
| 19 | |
| 20 | // As events are often on the stack, storing the first few labels directly |
| 21 | // in the event can avoid an allocation at all for the very common cases of |
| 22 | // simple events. |
| 23 | // The length needs to be large enough to cope with the majority of events |
| 24 | // but no so large as to cause undue stack pressure. |
| 25 | // A log message with two values will use 3 labels (one for each value and |
| 26 | // one for the message itself). |
| 27 | |
| 28 | static [3]label.Label // inline storage for the first few labels |
| 29 | dynamic []label.Label // dynamically sized storage for remaining labels |
| 30 | } |
| 31 | |
| 32 | func (ev Event) At() time.Time { return ev.at } |
| 33 | |
| 34 | func (ev Event) Format(f fmt.State, r rune) { |
| 35 | if !ev.at.IsZero() { |
| 36 | fmt.Fprint(f, ev.at.Format("2006/01/02 15:04:05 ")) |
| 37 | } |
| 38 | for l := range ev.Labels() { |
| 39 | fmt.Fprintf(f, "\n\t%v", l) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | func (ev Event) Valid(index int) bool { |
| 44 | return index >= 0 && index < len(ev.static)+len(ev.dynamic) |
| 45 | } |
| 46 | |
| 47 | func (ev Event) Label(index int) label.Label { |
| 48 | if index < len(ev.static) { |
| 49 | return ev.static[index] |
| 50 | } |
| 51 | return ev.dynamic[index-len(ev.static)] |
| 52 | } |
| 53 | |
| 54 | // Labels returns an iterator over the event's valid labels. |
| 55 | func (ev Event) Labels() iter.Seq[label.Label] { |
| 56 | return func(yield func(label.Label) bool) { |
| 57 | for _, l := range ev.static { |
| 58 | if l.Valid() && !yield(l) { |
| 59 | return |
| 60 | } |
| 61 | } |
| 62 | for _, l := range ev.dynamic { |
| 63 | if l.Valid() && !yield(l) { |
| 64 | return |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | func (ev Event) Find(key label.Key) label.Label { |
| 71 | for _, l := range ev.static { |
| 72 | if l.Key() == key { |
| 73 | return l |
| 74 | } |
| 75 | } |
| 76 | for _, l := range ev.dynamic { |
| 77 | if l.Key() == key { |
| 78 | return l |
| 79 | } |
| 80 | } |
| 81 | return label.Label{} |
| 82 | } |
| 83 | |
| 84 | func MakeEvent(static [3]label.Label, labels []label.Label) Event { |
| 85 | return Event{ |
| 86 | static: static, |
| 87 | dynamic: labels, |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | // CloneEvent event returns a copy of the event with the time adjusted to at. |
| 92 | func CloneEvent(ev Event, at time.Time) Event { |
| 93 | ev.at = at |
| 94 | return ev |
| 95 | } |