-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
registry.go
311 lines (276 loc) · 8.16 KB
/
registry.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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// Copyright 2015 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package metric
import (
"context"
"encoding/json"
"fmt"
"reflect"
"regexp"
"github.com/cockroachdb/cockroach/pkg/util/buildutil"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
"github.com/gogo/protobuf/proto"
prometheusgo "github.com/prometheus/client_model/go"
)
// A Registry is a list of metrics. It provides a simple way of iterating over
// them, can marshal into JSON, and generate a prometheus format.
//
// A registry can have label pairs that will be applied to all its metrics
// when exported to prometheus.
type Registry struct {
syncutil.Mutex
labels []labelPair
tracked map[string]Iterable
// computedLabels get filled in by GetLabels().
// We hold onto the slice to avoid a re-allocation every
// time the metrics get scraped.
computedLabels []*prometheusgo.LabelPair
}
type labelPair struct {
name string
value interface{}
}
// Struct can be implemented by the types of members of a metric
// container so that the members get automatically registered.
type Struct interface {
MetricStruct()
}
// NewRegistry creates a new Registry.
func NewRegistry() *Registry {
return &Registry{
labels: []labelPair{},
computedLabels: []*prometheusgo.LabelPair{},
tracked: map[string]Iterable{},
}
}
// AddLabel adds a label/value pair for this registry.
func (r *Registry) AddLabel(name string, value interface{}) {
r.Lock()
defer r.Unlock()
r.labels = append(r.labels, labelPair{name: exportedLabel(name), value: value})
r.computedLabels = append(r.computedLabels, &prometheusgo.LabelPair{})
}
func (r *Registry) GetLabels() []*prometheusgo.LabelPair {
r.Lock()
defer r.Unlock()
for i, l := range r.labels {
r.computedLabels[i].Name = proto.String(l.name)
r.computedLabels[i].Value = proto.String(fmt.Sprint(l.value))
}
return r.computedLabels
}
// AddMetric adds the passed-in metric to the registry.
func (r *Registry) AddMetric(metric Iterable) {
r.Lock()
defer r.Unlock()
r.tracked[metric.GetName()] = metric
if log.V(2) {
log.Infof(context.TODO(), "added metric: %s (%T)", metric.GetName(), metric)
}
}
// AddMetricStruct examines all fields of metricStruct and adds
// all Iterable or metric.Struct objects to the registry.
func (r *Registry) AddMetricStruct(metricStruct interface{}) {
if r == nil { // for testing convenience
return
}
ctx := context.TODO()
v := reflect.ValueOf(metricStruct)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
t := v.Type()
for i := 0; i < v.NumField(); i++ {
vfield, tfield := v.Field(i), t.Field(i)
tname := tfield.Name
if !vfield.CanInterface() {
checkFieldCanBeSkipped("unexported", tname, tfield.Type, t)
continue
}
switch vfield.Kind() {
case reflect.Array, reflect.Slice:
for i := 0; i < vfield.Len(); i++ {
velem := vfield.Index(i)
telemName := fmt.Sprintf("%s[%d]", tname, i)
// Permit elements in the array to be nil.
const skipNil = true
r.addMetricValue(ctx, velem, telemName, skipNil, t)
}
default:
// No metric fields should be nil.
const skipNil = false
r.addMetricValue(ctx, vfield, tname, skipNil, t)
}
}
}
func (r *Registry) addMetricValue(
ctx context.Context, val reflect.Value, name string, skipNil bool, parentType reflect.Type,
) {
if val.Kind() == reflect.Ptr && val.IsNil() {
if skipNil {
if log.V(2) {
log.Infof(ctx, "skipping nil metric field %s", name)
}
} else {
log.Fatalf(ctx, "found nil metric field %s", name)
}
return
}
switch typ := val.Interface().(type) {
case Iterable:
r.AddMetric(typ)
case Struct:
r.AddMetricStruct(typ)
default:
checkFieldCanBeSkipped("non-metric", name, val.Type(), parentType)
}
}
// WriteMetricsMetadata writes metadata from all tracked metrics to the
// parameter map.
func (r *Registry) WriteMetricsMetadata(dest map[string]Metadata) {
r.Lock()
defer r.Unlock()
for _, v := range r.tracked {
dest[v.GetName()] = v.GetMetadata()
}
}
// Each calls the given closure for all metrics.
func (r *Registry) Each(f func(name string, val interface{})) {
r.Lock()
defer r.Unlock()
for _, metric := range r.tracked {
metric.Inspect(func(v interface{}) {
f(metric.GetName(), v)
})
}
}
// Select calls the given closure for the selected metric names.
func (r *Registry) Select(metrics map[string]struct{}, f func(name string, val interface{})) {
r.Lock()
defer r.Unlock()
for name := range metrics {
metric, found := r.tracked[name]
if found {
metric.Inspect(func(v interface{}) {
f(name, v)
})
}
}
}
// MarshalJSON marshals to JSON.
func (r *Registry) MarshalJSON() ([]byte, error) {
r.Lock()
defer r.Unlock()
m := make(map[string]interface{})
for _, metric := range r.tracked {
metric.Inspect(func(v interface{}) {
m[metric.GetName()] = v
})
}
return json.Marshal(m)
}
var (
// Prometheus metric names and labels have fairly strict rules, they
// must match the regexp [a-zA-Z_:][a-zA-Z0-9_:]*
// See: https://prometheus.io/docs/concepts/data_model/
prometheusNameReplaceRE = regexp.MustCompile("^[^a-zA-Z_:]|[^a-zA-Z0-9_:]")
prometheusLabelReplaceRE = regexp.MustCompile("^[^a-zA-Z_]|[^a-zA-Z0-9_]")
)
// exportedName takes a metric name and generates a valid prometheus name.
func exportedName(name string) string {
return prometheusNameReplaceRE.ReplaceAllString(name, "_")
}
// exportedLabel takes a metric name and generates a valid prometheus name.
func exportedLabel(name string) string {
return prometheusLabelReplaceRE.ReplaceAllString(name, "_")
}
var panicHandler = log.Fatalf
func testingSetPanicHandler(h func(ctx context.Context, msg string, args ...interface{})) func() {
panicHandler = h
return func() { panicHandler = log.Fatalf }
}
// checkFieldCanBeSkipped detects common mis-use patterns with metrics registry
// when running under test.
func checkFieldCanBeSkipped(
skipReason, fieldName string, fieldType reflect.Type, parentType reflect.Type,
) {
if !buildutil.CrdbTestBuild {
if log.V(2) {
log.Infof(context.Background(), "skipping %s field %s", skipReason, fieldName)
}
return
}
qualifiedFieldName := func() string {
if parentType == nil {
return fieldName
}
parentName := parentType.Name()
if parentName == "" {
parentName = "<unnamed>"
}
return fmt.Sprintf("%s.%s", parentName, fieldName)
}()
if isMetricType(fieldType) {
panicHandler(context.Background(),
"metric field %s (or any of embedded metrics) must be exported", qualifiedFieldName)
}
switch fieldType.Kind() {
case reflect.Array, reflect.Slice:
checkFieldCanBeSkipped(skipReason, fieldName, fieldType.Elem(), parentType)
case reflect.Struct:
containsMetrics := false
for i := 0; i < fieldType.NumField() && !containsMetrics; i++ {
containsMetrics = containsMetricType(fieldType.Field(i).Type)
}
if containsMetrics {
// Embedded struct contains metrics. Make sure the struct implements
// metric.Struct interface.
_, isStruct := reflect.New(fieldType).Interface().(Struct)
if !isStruct {
panicHandler(context.Background(),
"embedded struct field %s (%s) does not implement metric.Struct interface", qualifiedFieldName, fieldType)
}
}
}
}
// isMetricType returns true if the type is one of the metric type interfaces.
func isMetricType(ft reflect.Type) bool {
v := reflect.Zero(ft)
if !v.CanInterface() {
return false
}
switch v.Interface().(type) {
case Iterable, Struct:
return true
default:
return false
}
}
// containsMetricTypes returns true if the type or any types inside aggregate
// type (array, struct, etc) is metric type.
func containsMetricType(ft reflect.Type) bool {
if isMetricType(ft) {
return true
}
switch ft.Kind() {
case reflect.Slice, reflect.Array:
return containsMetricType(ft.Elem())
case reflect.Struct:
for i := 0; i < ft.NumField(); i++ {
if containsMetricType(ft.Field(i).Type) {
return true
}
}
return false
default:
return false
}
}