Debian Codegraph @

event/common.go

raw symbols SCIP
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
6
7 import (
8 "context"
9 "fmt"
10 "sync"
11 )
12
13 const (
14 MetricKey = interfaceKey("metric")
15 MetricVal = "metricValue"
16 DurationMetric = interfaceKey("durationMetric")
17 )
18
19 type Kind int
20
21 const (
22 unknownKind = Kind(iota)
23
24 LogKind
25 MetricKind
26 StartKind
27 EndKind
28
29 dynamicKindStart
30 )
31
32 type (
33 valueKey string
34 interfaceKey string
35 )
36
37 var (
38 dynamicKindMu sync.Mutex
39 nextDynamicKind = dynamicKindStart
40 dynamicKindNames map[Kind]string
41 )
42
43 func NewKind(name string) Kind {
44 dynamicKindMu.Lock()
45 defer dynamicKindMu.Unlock()
46 for _, n := range dynamicKindNames {
47 if n == name {
48 panic(fmt.Errorf("kind %s is already registered", name))
49 }
50 }
51 k := nextDynamicKind
52 nextDynamicKind++
53 dynamicKindNames[k] = name
54 return k
55 }
56
57 func (k Kind) String() string {
58 switch k {
59 case unknownKind:
60 return "unknown"
61 case LogKind:
62 return "log"
63 case MetricKind:
64 return "metric"
65 case StartKind:
66 return "start"
67 case EndKind:
68 return "end"
69 default:
70 dynamicKindMu.Lock()
71 defer dynamicKindMu.Unlock()
72 name, ok := dynamicKindNames[k]
73 if !ok {
74 return fmt.Sprintf("?unknownKind:%d?", k)
75 }
76 return name
77 }
78 }
79
80 func Log(ctx context.Context, msg string, labels ...Label) {
81 ev := New(ctx, LogKind)
82 if ev != nil {
83 ev.Labels = append(ev.Labels, labels...)
84 ev.Labels = append(ev.Labels, String("msg", msg))
85 ev.Deliver()
86 }
87 }
88
89 func Logf(ctx context.Context, msg string, args ...interface{}) {
90 ev := New(ctx, LogKind)
91 if ev != nil {
92 ev.Labels = append(ev.Labels, String("msg", fmt.Sprintf(msg, args...)))
93 ev.Deliver()
94 }
95 }
96
97 func Error(ctx context.Context, msg string, err error, labels ...Label) {
98 ev := New(ctx, LogKind)
99 if ev != nil {
100 ev.Labels = append(ev.Labels, labels...)
101 ev.Labels = append(ev.Labels, String("msg", msg), Value("error", err))
102 ev.Deliver()
103 }
104 }
105
106 func Annotate(ctx context.Context, labels ...Label) {
107 ev := New(ctx, 0)
108 if ev != nil {
109 ev.Labels = append(ev.Labels, labels...)
110 ev.Deliver()
111 }
112 }
113
114 func Start(ctx context.Context, name string, labels ...Label) context.Context {
115 ev := New(ctx, StartKind)
116 if ev != nil {
117 ev.Labels = append(ev.Labels, String("name", name))
118 ev.Labels = append(ev.Labels, labels...)
119 ev.Trace()
120 ctx = ev.Deliver()
121 }
122 return ctx
123 }
124
125 func End(ctx context.Context, labels ...Label) {
126 ev := New(ctx, EndKind)
127 if ev != nil {
128 ev.Labels = append(ev.Labels, labels...)
129 ev.prepare()
130 // this was an end event, do we need to send a duration?
131 if v, ok := DurationMetric.Find(ev); ok {
132 //TODO: do we want the rest of the values from the end event?
133 v.(*DurationDistribution).Record(ctx, ev.At.Sub(ev.target.startTime))
134 }
135 ev.Deliver()
136 }
137 }
138
139 func (k interfaceKey) Of(v interface{}) Label {
140 return Value(string(k), v)
141 }
142
143 func (k interfaceKey) Find(ev *Event) (interface{}, bool) {
144 v, ok := lookupValue(string(k), ev.Labels)
145 if !ok {
146 return nil, false
147 }
148 return v.Interface(), true
149
150 }
151
152 func lookupValue(name string, labels []Label) (Label, bool) {
153 for i := len(labels) - 1; i >= 0; i-- {
154 if labels[i].Name == name {
155 return labels[i], true
156 }
157 }
158 return Label{}, false
159 }