-
Notifications
You must be signed in to change notification settings - Fork 64
/
tracer_metrics.go
130 lines (108 loc) · 2.96 KB
/
tracer_metrics.go
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
package gorelic
import (
"sync"
"time"
metrics "github.com/yvasiyarov/go-metrics"
"github.com/yvasiyarov/newrelic_platform_go"
)
type Tracer struct {
sync.RWMutex
metrics map[string]*TraceTransaction
component newrelic_platform_go.IComponent
}
func newTracer(component newrelic_platform_go.IComponent) *Tracer {
return &Tracer{metrics: make(map[string]*TraceTransaction), component: component}
}
func (t *Tracer) Trace(name string, traceFunc func()) {
trace := t.BeginTrace(name)
defer trace.EndTrace()
traceFunc()
}
func (t *Tracer) BeginTrace(name string) *Trace {
tracerName := "Trace/" + name
// Happy path: The transaction has already been created, so just read it from the map.
t.RLock()
trans, ok := t.metrics[tracerName]
t.RUnlock()
if ok {
return newTrace(trans)
}
// Slow path: We need to create the transaction and write it to the map, but first
// we need to check if some other goroutine added the same transaction to the map in the
// mean time.
t.Lock()
trans, ok = t.metrics[tracerName]
if ok {
t.Unlock()
return newTrace(trans)
}
trans = &TraceTransaction{name: tracerName, timer: metrics.NewTimer()}
t.metrics[tracerName] = trans
t.Unlock()
trans.addMetricsToComponent(t.component)
return newTrace(trans)
}
type Trace struct {
transaction *TraceTransaction
startTime time.Time
}
func (t *Trace) EndTrace() {
t.transaction.timer.UpdateSince(t.startTime)
}
func newTrace(trans *TraceTransaction) *Trace {
return &Trace{transaction: trans, startTime: time.Now()}
}
type TraceTransaction struct {
name string
timer metrics.Timer
}
func (transaction *TraceTransaction) addMetricsToComponent(component newrelic_platform_go.IComponent) {
tracerMean := &timerMeanMetrica{
baseTimerMetrica: &baseTimerMetrica{
name: transaction.name + "/mean",
units: "ms",
dataSource: transaction.timer,
},
}
component.AddMetrica(tracerMean)
tracerMax := &timerMaxMetrica{
baseTimerMetrica: &baseTimerMetrica{
name: transaction.name + "/max",
units: "ms",
dataSource: transaction.timer,
},
}
component.AddMetrica(tracerMax)
tracerMin := &timerMinMetrica{
baseTimerMetrica: &baseTimerMetrica{
name: transaction.name + "/min",
units: "ms",
dataSource: transaction.timer,
},
}
component.AddMetrica(tracerMin)
tracer75 := &timerPercentile75Metrica{
baseTimerMetrica: &baseTimerMetrica{
name: transaction.name + "/percentile75",
units: "ms",
dataSource: transaction.timer,
},
}
component.AddMetrica(tracer75)
tracer90 := &timerPercentile90Metrica{
baseTimerMetrica: &baseTimerMetrica{
name: transaction.name + "/percentile90",
units: "ms",
dataSource: transaction.timer,
},
}
component.AddMetrica(tracer90)
tracer95 := &timerPercentile95Metrica{
baseTimerMetrica: &baseTimerMetrica{
name: transaction.name + "/percentile95",
units: "ms",
dataSource: transaction.timer,
},
}
component.AddMetrica(tracer95)
}