-
Notifications
You must be signed in to change notification settings - Fork 0
/
tally.go
188 lines (155 loc) · 3.84 KB
/
tally.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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package stats
import (
"sync"
"time"
"github.com/uber/tchannel-go"
"github.com/uber-go/tally"
)
type wrapper struct {
sync.RWMutex
scope tally.Scope
byTags map[knownTags]*taggedScope
}
type knownTags struct {
dest string
source string
procedure string
retryCount string
}
type taggedScope struct {
sync.RWMutex
scope tally.Scope // already tagged with some set of tags
counters map[string]tally.Counter
gauges map[string]tally.Gauge
timers map[string]tally.Timer
}
// NewTallyReporter takes a tally.Scope and wraps it so it ca be used as a
// StatsReporter. The list of metrics emitted is documented on:
// https://tchannel.readthedocs.io/en/latest/metrics/
// The metrics emitted are similar to YARPC, the tags emitted are:
// source, dest, procedure, and retry-count.
func NewTallyReporter(scope tally.Scope) tchannel.StatsReporter {
return &wrapper{
scope: scope,
byTags: make(map[knownTags]*taggedScope),
}
}
func (w *wrapper) IncCounter(name string, tags map[string]string, value int64) {
ts := w.getTaggedScope(tags)
ts.getCounter(name).Inc(value)
}
func (w *wrapper) UpdateGauge(name string, tags map[string]string, value int64) {
ts := w.getTaggedScope(tags)
ts.getGauge(name).Update(float64(value))
}
func (w *wrapper) RecordTimer(name string, tags map[string]string, d time.Duration) {
ts := w.getTaggedScope(tags)
ts.getTimer(name).Record(d)
}
func (w *wrapper) getTaggedScope(tags map[string]string) *taggedScope {
kt := convertTags(tags)
w.RLock()
ts, ok := w.byTags[kt]
w.RUnlock()
if ok {
return ts
}
w.Lock()
defer w.Unlock()
// Always double-check under the write-lock.
if ts, ok := w.byTags[kt]; ok {
return ts
}
ts = &taggedScope{
scope: w.scope.Tagged(kt.tallyTags()),
counters: make(map[string]tally.Counter),
gauges: make(map[string]tally.Gauge),
timers: make(map[string]tally.Timer),
}
w.byTags[kt] = ts
return ts
}
func convertTags(tags map[string]string) knownTags {
if ts, ok := tags["target-service"]; ok {
// Outbound call.
return knownTags{
dest: ts,
source: tags["service"],
procedure: tags["target-endpoint"],
retryCount: tags["retry-count"],
}
}
if cs, ok := tags["calling-service"]; ok {
// Inbound call.
return knownTags{
dest: tags["service"],
source: cs,
procedure: tags["endpoint"],
retryCount: tags["retry-count"],
}
}
// TChannel doesn't use any other tags, so ignore all others for now.
return knownTags{}
}
// Create a sub-scope for this set of known tags.
func (kt knownTags) tallyTags() map[string]string {
tallyTags := make(map[string]string, 5)
if kt.dest != "" {
tallyTags["dest"] = kt.dest
}
if kt.source != "" {
tallyTags["source"] = kt.source
}
if kt.procedure != "" {
tallyTags["procedure"] = kt.procedure
}
if kt.retryCount != "" {
tallyTags["retry-count"] = kt.retryCount
}
return tallyTags
}
func (ts *taggedScope) getCounter(name string) tally.Counter {
ts.RLock()
counter, ok := ts.counters[name]
ts.RUnlock()
if ok {
return counter
}
ts.Lock()
defer ts.Unlock()
// No double-check under the lock, as overwriting the counter has
// no impact.
counter = ts.scope.Counter(name)
ts.counters[name] = counter
return counter
}
func (ts *taggedScope) getGauge(name string) tally.Gauge {
ts.RLock()
gauge, ok := ts.gauges[name]
ts.RUnlock()
if ok {
return gauge
}
ts.Lock()
defer ts.Unlock()
// No double-check under the lock, as overwriting the counter has
// no impact.
gauge = ts.scope.Gauge(name)
ts.gauges[name] = gauge
return gauge
}
func (ts *taggedScope) getTimer(name string) tally.Timer {
ts.RLock()
timer, ok := ts.timers[name]
ts.RUnlock()
if ok {
return timer
}
ts.Lock()
defer ts.Unlock()
// No double-check under the lock, as overwriting the counter has
// no impact.
timer = ts.scope.Timer(name)
ts.timers[name] = timer
return timer
}