Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / mvdan.cc / gofumpt@v0.0.0-20200802201014-ab5a8192947d / gofumports / internal / telemetry / event / event.go
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 event provides support for event based telemetry.
6 package event
7
8 import (
9         "fmt"
10         "time"
11 )
12
13 type eventType uint8
14
15 const (
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
23 )
24
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).
33 type sTags [3]Tag
34
35 // Event holds the information about an event of note that ocurred.
36 type Event struct {
37         At time.Time
38
39         typ     eventType
40         static  sTags // inline storage for the first few tags
41         dynamic []Tag // dynamically sized storage for remaining tags
42 }
43
44 // eventTagMap implements TagMap for a the tags of an Event.
45 type eventTagMap struct {
46         event Event
47 }
48
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 }
55
56 func (ev Event) Format(f fmt.State, r rune) {
57         tagMap := TagMap(ev)
58         if !ev.At.IsZero() {
59                 fmt.Fprint(f, ev.At.Format("2006/01/02 15:04:05 "))
60         }
61         msg := Msg.Get(tagMap)
62         err := Err.Get(tagMap)
63         fmt.Fprint(f, msg)
64         if err != nil {
65                 if f.Flag('+') {
66                         fmt.Fprintf(f, ": %+v", err)
67                 } else {
68                         fmt.Fprintf(f, ": %v", err)
69                 }
70         }
71         for index := 0; ev.Valid(index); index++ {
72                 tag := ev.Tag(index)
73                 // msg and err were both already printed above, so we skip them to avoid
74                 // double printing
75                 if !tag.Valid() || tag.Key() == Msg || tag.Key() == Err {
76                         continue
77                 }
78                 fmt.Fprintf(f, "\n\t%v", tag)
79         }
80 }
81
82 func (ev Event) Valid(index int) bool {
83         return index >= 0 && index < len(ev.static)+len(ev.dynamic)
84 }
85
86 func (ev Event) Tag(index int) Tag {
87         if index < len(ev.static) {
88                 return ev.static[index]
89         }
90         return ev.dynamic[index-len(ev.static)]
91 }
92
93 func (ev Event) Find(key Key) Tag {
94         for _, tag := range ev.static {
95                 if tag.Key() == key {
96                         return tag
97                 }
98         }
99         for _, tag := range ev.dynamic {
100                 if tag.Key() == key {
101                         return tag
102                 }
103         }
104         return Tag{}
105 }
106
107 func makeEvent(typ eventType, static sTags, tags []Tag) Event {
108         return Event{
109                 typ:     typ,
110                 static:  static,
111                 dynamic: tags,
112         }
113 }