-
Notifications
You must be signed in to change notification settings - Fork 766
/
scope.go
143 lines (127 loc) · 4.39 KB
/
scope.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
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package metrics
import (
"time"
"github.com/uber-go/tally"
)
type metricsScope struct {
scope tally.Scope
rootScope tally.Scope
defs map[int]metricDefinition
isDomainTagged bool
}
func newMetricsScope(
rootScope tally.Scope,
scope tally.Scope,
defs map[int]metricDefinition,
isDomain bool,
) Scope {
return &metricsScope{
scope: scope,
rootScope: rootScope,
defs: defs,
isDomainTagged: isDomain,
}
}
// NoopScope returns a noop scope of metrics
func NoopScope(serviceIdx ServiceIdx) Scope {
return &metricsScope{
scope: tally.NoopScope,
rootScope: tally.NoopScope,
defs: getMetricDefs(serviceIdx),
}
}
func (m *metricsScope) IncCounter(id int) {
m.AddCounter(id, 1)
}
func (m *metricsScope) AddCounter(id int, delta int64) {
def := m.defs[id]
m.scope.Counter(def.metricName.String()).Inc(delta)
if !def.metricRollupName.Empty() {
m.rootScope.Counter(def.metricRollupName.String()).Inc(delta)
}
}
func (m *metricsScope) UpdateGauge(id int, value float64) {
def := m.defs[id]
m.scope.Gauge(def.metricName.String()).Update(value)
if !def.metricRollupName.Empty() {
m.scope.Gauge(def.metricRollupName.String()).Update(value)
}
}
func (m *metricsScope) StartTimer(id int) Stopwatch {
def := m.defs[id]
timer := m.scope.Timer(def.metricName.String())
switch {
case !def.metricRollupName.Empty():
return NewStopwatch(timer, m.rootScope.Timer(def.metricRollupName.String()))
case m.isDomainTagged:
timerAll := m.scope.Tagged(map[string]string{domain: allValue}).Timer(def.metricName.String())
return NewStopwatch(timer, timerAll)
default:
return NewStopwatch(timer)
}
}
func (m *metricsScope) RecordTimer(id int, d time.Duration) {
def := m.defs[id]
m.scope.Timer(def.metricName.String()).Record(d)
switch {
case !def.metricRollupName.Empty():
m.rootScope.Timer(def.metricRollupName.String()).Record(d)
case m.isDomainTagged:
// N.B. - Dual emit here so that we can see aggregate timer stats across all
// domains along with the individual domains stats
m.scope.Tagged(map[string]string{domain: allValue}).Timer(def.metricName.String()).Record(d)
}
}
func (m *metricsScope) RecordHistogramDuration(id int, value time.Duration) {
def := m.defs[id]
m.scope.Histogram(def.metricName.String(), m.getBuckets(id)).RecordDuration(value)
if !def.metricRollupName.Empty() {
m.rootScope.Histogram(def.metricRollupName.String(), m.getBuckets(id)).RecordDuration(value)
}
}
func (m *metricsScope) RecordHistogramValue(id int, value float64) {
def := m.defs[id]
m.scope.Histogram(def.metricName.String(), m.getBuckets(id)).RecordValue(value)
if !def.metricRollupName.Empty() {
m.rootScope.Histogram(def.metricRollupName.String(), m.getBuckets(id)).RecordValue(value)
}
}
func (m *metricsScope) Tagged(tags ...Tag) Scope {
domainTagged := false
tagMap := make(map[string]string, len(tags))
for _, tag := range tags {
if isDomainTagged(tag) {
domainTagged = true
}
tagMap[tag.Key()] = tag.Value()
}
return newMetricsScope(m.rootScope, m.scope.Tagged(tagMap), m.defs, domainTagged)
}
func (m *metricsScope) getBuckets(id int) tally.Buckets {
if m.defs[id].buckets != nil {
return m.defs[id].buckets
}
return tally.DefaultBuckets
}
func isDomainTagged(tag Tag) bool {
return tag.Key() == domain && tag.Value() != allValue
}