-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscope.go
More file actions
236 lines (177 loc) · 5.59 KB
/
scope.go
File metadata and controls
236 lines (177 loc) · 5.59 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package stats
import (
"strings"
)
type limitedScope interface {
namespace() string
tags() map[string]string
rootScope() *rootScope
}
// HistogramOption configures a histogram with custom settings.
type HistogramOption func(*histogramVector)
// Scope is the primary interface for creating and organizing metrics.
// Scopes support hierarchical organization through namespaces and tag inheritance.
// Metrics created from a scope inherit the scope's namespace prefix and tags.
type Scope interface {
limitedScope
// Counter creates or retrieves a counter metric with the given name.
Counter(string) Counter
// CounterVector creates or retrieves a counter vector with the given name and labels.
CounterVector(string, []string) CounterVector
// Gauge creates or retrieves a gauge metric with the given name.
Gauge(string) Gauge
// GaugeVector creates or retrieves a gauge vector with the given name and labels.
GaugeVector(string, []string) GaugeVector
// Histogram creates or retrieves a histogram metric with the given name and optional configuration.
Histogram(string, ...HistogramOption) Histogram
// HistogramVector creates or retrieves a histogram vector with the given name, labels, and optional configuration.
HistogramVector(string, []string, ...HistogramOption) HistogramVector
// Scope creates a child scope with the given namespace and tags.
// The namespace is appended to the parent's namespace with underscore separation.
// Tags are merged with the parent's tags, with child tags overriding parent values.
Scope(string, map[string]string) Scope
// RootScope returns the root scope, ignoring any namespace or tag hierarchy.
RootScope() Scope
}
type scopeWrapper struct {
limitedScope
}
func (sw scopeWrapper) buildLabelValues() ([]string, []string) {
var (
tags = sw.tags()
labels = make([]string, 0, len(tags))
values = make([]string, 0, len(tags))
)
for l, v := range tags {
labels = append(labels, l)
values = append(values, v)
}
return labels, values
}
func (sw scopeWrapper) Histogram(name string, opts ...HistogramOption) Histogram {
var (
ls, vs = sw.buildLabelValues()
hv = sw.rootScope().registerHistogram(
joinStrings(sw.namespace(), name),
ls,
opts...,
)
)
return hv.WithLabels(vs...)
}
func (sw scopeWrapper) HistogramVector(name string, labels []string, opts ...HistogramOption) HistogramVector {
var (
sls, vs = sw.buildLabelValues()
hv = sw.rootScope().registerHistogram(
joinStrings(sw.namespace(), name),
append(sls, labels...),
opts...,
)
)
return partialHistogramVector{hv: hv, vs: vs}
}
func (sw scopeWrapper) Gauge(name string) Gauge {
var (
ls, vs = sw.buildLabelValues()
gv = sw.rootScope().registerGauge(joinStrings(sw.namespace(), name), ls)
)
return gv.WithLabels(vs...)
}
func (sw scopeWrapper) GaugeVector(name string, labels []string) GaugeVector {
var (
sls, vs = sw.buildLabelValues()
gv = sw.rootScope().registerGauge(
joinStrings(sw.namespace(), name),
append(sls, labels...),
)
)
return partialGaugeVector{gv: gv, vs: vs}
}
func (sw scopeWrapper) Counter(name string) Counter {
var (
ls, vs = sw.buildLabelValues()
cv = sw.rootScope().registerCounter(joinStrings(sw.namespace(), name), ls)
)
return cv.WithLabels(vs...)
}
func (sw scopeWrapper) CounterVector(name string, labels []string) CounterVector {
var (
pls, vs = sw.buildLabelValues()
cv = sw.rootScope().registerCounter(
joinStrings(sw.namespace(), name),
append(pls, labels...),
)
)
return partialCounterVector{cv: cv, vs: vs}
}
func (sw scopeWrapper) Scope(ns string, tags map[string]string) Scope {
return scopeWrapper{
limitedScope: &subScope{parent: sw.limitedScope, ns: ns, ts: tags},
}
}
func (sw scopeWrapper) RootScope() Scope {
return scopeWrapper{limitedScope: sw.rootScope()}
}
type subScope struct {
parent limitedScope
ns string
ts map[string]string
}
func (ss *subScope) namespace() string {
return joinStrings(ss.parent.namespace(), ss.ns)
}
func (ss *subScope) tags() map[string]string {
return mergeStringMaps(ss.parent.tags(), ss.ts)
}
func (ss *subScope) rootScope() *rootScope { return ss.parent.rootScope() }
func joinStrings(ss ...string) string {
var (
res []string
append = func(s string) {
if s != "" {
res = append(res, s)
}
}
)
for _, s := range ss {
append(s)
}
return strings.Join(res, "_")
}
func mergeStringMaps(kvs ...map[string]string) map[string]string {
var (
res = make(map[string]string)
merge = func(kv map[string]string) {
for k, v := range kv {
res[k] = v
}
}
)
for _, kv := range kvs {
merge(kv)
}
return res
}
// NoopScope is a scope implementation that discards all metrics.
// Useful for testing or when metrics are conditionally disabled.
var NoopScope Scope = noopScope{}
type noopScope struct{}
func (noopScope) namespace() string { return "" }
func (noopScope) tags() map[string]string { return nil }
func (noopScope) rootScope() *rootScope { return nil }
func (noopScope) Counter(string) Counter { return NoopCounter }
func (noopScope) CounterVector(string, []string) CounterVector {
return NoopCounterVector
}
func (noopScope) Gauge(string) Gauge { return NoopGauge }
func (noopScope) GaugeVector(string, []string) GaugeVector {
return NoopGaugeVector
}
func (noopScope) Histogram(string, ...HistogramOption) Histogram {
return NoopHistogram
}
func (noopScope) HistogramVector(string, []string, ...HistogramOption) HistogramVector {
return NoopHistogramVector
}
func (noopScope) Scope(string, map[string]string) Scope { return noopScope{} }
func (noopScope) RootScope() Scope { return noopScope{} }