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