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 core provides support for event based telemetry.
12 "golang.org/x/tools/internal/event/label"
15 // Event holds the information about an event of note that ocurred.
19 // As events are often on the stack, storing the first few labels directly
20 // in the event can avoid an allocation at all for the very common cases of
22 // The length needs to be large enough to cope with the majority of events
23 // but no so large as to cause undue stack pressure.
24 // A log message with two values will use 3 labels (one for each value and
25 // one for the message itself).
27 static [3]label.Label // inline storage for the first few labels
28 dynamic []label.Label // dynamically sized storage for remaining labels
31 // eventLabelMap implements label.Map for a the labels of an Event.
32 type eventLabelMap struct {
36 func (ev Event) At() time.Time { return ev.at }
38 func (ev Event) Format(f fmt.State, r rune) {
40 fmt.Fprint(f, ev.at.Format("2006/01/02 15:04:05 "))
42 for index := 0; ev.Valid(index); index++ {
43 if l := ev.Label(index); l.Valid() {
44 fmt.Fprintf(f, "\n\t%v", l)
49 func (ev Event) Valid(index int) bool {
50 return index >= 0 && index < len(ev.static)+len(ev.dynamic)
53 func (ev Event) Label(index int) label.Label {
54 if index < len(ev.static) {
55 return ev.static[index]
57 return ev.dynamic[index-len(ev.static)]
60 func (ev Event) Find(key label.Key) label.Label {
61 for _, l := range ev.static {
66 for _, l := range ev.dynamic {
74 func MakeEvent(static [3]label.Label, labels []label.Label) Event {
81 // CloneEvent event returns a copy of the event with the time adjusted to at.
82 func CloneEvent(ev Event, at time.Time) Event {