-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroot_scope.go
More file actions
178 lines (137 loc) · 3.66 KB
/
root_scope.go
File metadata and controls
178 lines (137 loc) · 3.66 KB
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
package stats
import (
"fmt"
"sort"
"sync"
)
type rootScope struct {
c Collector
mu sync.Mutex
lm labelMarshaler
counters map[string]*atomicInt64Vector
gauges map[string]*atomicInt64Vector
histograms map[string]*histogramVector
}
// RootScope creates a new root scope that registers metrics with the given collector.
// This is the primary entry point for creating a metrics hierarchy.
func RootScope(c Collector) Scope {
return scopeWrapper{
&rootScope{
c: c,
lm: newDefaultMarshaler(),
counters: make(map[string]*atomicInt64Vector),
gauges: make(map[string]*atomicInt64Vector),
histograms: make(map[string]*histogramVector),
},
}
}
func (rs *rootScope) assertMetricUniqueness(n string) {
if _, ok := rs.counters[n]; ok {
panic(fmt.Sprintf("counter with the same name already registered: %q", n))
}
if _, ok := rs.gauges[n]; ok {
panic(fmt.Sprintf("gauge with the same name already registered: %q", n))
}
if _, ok := rs.histograms[n]; ok {
panic(fmt.Sprintf("hisogram with the same name already registered: %q", n))
}
}
type labelOrderer interface {
order([]string) []string
}
type mappingLabelOdrderer struct {
mapping map[int]int
}
func (mlo mappingLabelOdrderer) order(in []string) []string {
if len(mlo.mapping) != len(in) {
panic("wrong number of labels passed")
}
var out = make([]string, len(in))
for i, v := range in {
out[mlo.mapping[i]] = v
}
return out
}
func buildLabelOrderer(base, target []string) labelOrderer {
if len(base) != len(target) {
panic("Another metric is already created with different number of label")
}
var (
baseCopy = append(base[:0:0], base...)
targetCopy = append(target[:0:0], target...)
)
sort.Strings(baseCopy)
sort.Strings(targetCopy)
for i, bv := range baseCopy {
if targetCopy[i] != bv {
panic("Another metric is already created with different label")
}
}
var transferMap = make(map[int]int, len(base))
for i, tv := range target {
for j, bv := range base {
if tv == bv {
transferMap[i] = j
break
}
}
}
return mappingLabelOdrderer{mapping: transferMap}
}
func (rs *rootScope) registerHistogram(n string, ls []string, opts ...HistogramOption) HistogramVector {
rs.mu.Lock()
defer rs.mu.Unlock()
if h, ok := rs.histograms[n]; ok {
// TODO: ensure the cutoffs are similare or panic
return reorderHistogramVector{
hv: h,
labelOrderer: buildLabelOrderer(h.labels, ls),
}
}
rs.assertMetricUniqueness(n)
v := &histogramVector{
cutoffs: defaultCutoffs,
labels: ls,
hs: map[uint64]*histogram{},
marshaler: rs.lm,
}
for _, opt := range opts {
opt(v)
}
rs.histograms[n] = v
rs.c.RegisterHistogram(n, v)
return v
}
func (rs *rootScope) registerGauge(n string, ls []string) GaugeVector {
rs.mu.Lock()
defer rs.mu.Unlock()
if c, ok := rs.gauges[n]; ok {
return reorderGaugeVector{
gv: gaugeVector{c},
labelOrderer: buildLabelOrderer(c.labels, ls),
}
}
rs.assertMetricUniqueness(n)
v := newAtomicInt64Vector(ls, rs.lm)
rs.gauges[n] = v
rs.c.RegisterGauge(n, v)
return gaugeVector{v}
}
func (rs *rootScope) registerCounter(n string, ls []string) CounterVector {
rs.mu.Lock()
defer rs.mu.Unlock()
if c, ok := rs.counters[n]; ok {
return reorderCounterVector{
cv: counterVector{c},
labelOrderer: buildLabelOrderer(c.labels, ls),
}
}
rs.assertMetricUniqueness(n)
v := newAtomicInt64Vector(ls, rs.lm)
rs.counters[n] = v
rs.c.RegisterCounter(n, v)
return counterVector{v}
}
func (*rootScope) namespace() string { return "" }
func (*rootScope) tags() map[string]string { return nil }
func (rs *rootScope) rootScope() *rootScope { return rs }