This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathscope.go
465 lines (398 loc) · 15.8 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
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
package promutils
import (
"strings"
"time"
"k8s.io/apimachinery/pkg/util/rand"
"github.com/prometheus/client_golang/prometheus"
)
const defaultScopeDelimiterStr = ":"
const defaultMetricDelimiterStr = "_"
var (
defaultObjectives = map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001}
defaultBuckets = []float64{.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10}
)
func panicIfError(err error) {
if err != nil {
panic("Failed to register metrics. Error: " + err.Error())
}
}
// A Simple StopWatch that works with prometheus summary
// It will scale the output to match the expected time scale (milliseconds, seconds etc)
// NOTE: Do not create a StopWatch object by hand, use a Scope to get a new instance of the StopWatch object
type StopWatch struct {
prometheus.Observer
outputScale time.Duration
}
// Start creates a new Instance of the StopWatch called a Timer that is closeable/stoppable.
// Common pattern to time a scope would be
// {
// timer := stopWatch.Start()
// defer timer.Stop()
// ....
// }
func (s StopWatch) Start() Timer {
return Timer{
start: time.Now(),
outputScale: s.outputScale,
timer: s.Observer,
}
}
// Observes specified duration between the start and end time
func (s StopWatch) Observe(start, end time.Time) {
observed := end.Sub(start).Nanoseconds()
outputScaleDuration := s.outputScale.Nanoseconds()
if outputScaleDuration == 0 {
s.Observer.Observe(0)
return
}
scaled := float64(observed / outputScaleDuration)
s.Observer.Observe(scaled)
}
// Observes/records the time to execute the given function synchronously
func (s StopWatch) Time(f func()) {
t := s.Start()
f()
t.Stop()
}
// A Simple StopWatch that works with prometheus summary
// It will scale the output to match the expected time scale (milliseconds, seconds etc)
// NOTE: Do not create a StopWatch object by hand, use a Scope to get a new instance of the StopWatch object
type StopWatchVec struct {
*prometheus.SummaryVec
outputScale time.Duration
}
// Gets a concrete StopWatch instance that can be used to start a timer and record observations.
func (s StopWatchVec) WithLabelValues(values ...string) StopWatch {
return StopWatch{
Observer: s.SummaryVec.WithLabelValues(values...),
outputScale: s.outputScale,
}
}
func (s StopWatchVec) GetMetricWith(labels prometheus.Labels) (StopWatch, error) {
sVec, err := s.SummaryVec.GetMetricWith(labels)
if err != nil {
return StopWatch{}, err
}
return StopWatch{
Observer: sVec,
outputScale: s.outputScale,
}, nil
}
// This is a stoppable instance of a StopWatch or a Timer
// A Timer can only be stopped. On stopping it will output the elapsed duration to prometheus
type Timer struct {
start time.Time
outputScale time.Duration
timer prometheus.Observer
}
// This method observes the elapsed duration since the creation of the timer. The timer is created using a StopWatch
func (s Timer) Stop() float64 {
observed := time.Since(s.start).Nanoseconds()
outputScaleDuration := s.outputScale.Nanoseconds()
if outputScaleDuration == 0 {
s.timer.Observe(0)
return 0
}
scaled := float64(observed / outputScaleDuration)
s.timer.Observe(scaled)
return scaled
}
// A SummaryOptions represents a set of options that can be supplied when creating a new prometheus summary metric
type SummaryOptions struct {
// An Objectives defines the quantile rank estimates with their respective absolute errors.
// Refer to https://godoc.org/github.com/prometheus/client_golang/prometheus#SummaryOpts for details
Objectives map[float64]float64
}
// A Scope represents a prefix in Prometheus. It is nestable, thus every metric that is published does not need to
// provide a prefix, but just the name of the metric. As long as the Scope is used to create a new instance of the metric
// The prefix (or scope) is automatically set.
type Scope interface {
// Creates new prometheus.Gauge metric with the prefix as the CurrentScope
// Name is a string that follows prometheus conventions (mostly [_a-z])
// Refer to https://prometheus.io/docs/concepts/metric_types/ for more information
NewGauge(name, description string) (prometheus.Gauge, error)
MustNewGauge(name, description string) prometheus.Gauge
// Creates new prometheus.GaugeVec metric with the prefix as the CurrentScope
// Refer to https://prometheus.io/docs/concepts/metric_types/ for more information
NewGaugeVec(name, description string, labelNames ...string) (*prometheus.GaugeVec, error)
MustNewGaugeVec(name, description string, labelNames ...string) *prometheus.GaugeVec
// Creates new prometheus.Summary metric with the prefix as the CurrentScope
// Refer to https://prometheus.io/docs/concepts/metric_types/ for more information
NewSummary(name, description string) (prometheus.Summary, error)
MustNewSummary(name, description string) prometheus.Summary
// Creates new prometheus.Summary metric with custom options, such as a custom set of objectives (i.e., target quantiles).
// Refer to https://prometheus.io/docs/concepts/metric_types/ for more information
NewSummaryWithOptions(name, description string, options SummaryOptions) (prometheus.Summary, error)
MustNewSummaryWithOptions(name, description string, options SummaryOptions) prometheus.Summary
// Creates new prometheus.SummaryVec metric with the prefix as the CurrentScope
// Refer to https://prometheus.io/docs/concepts/metric_types/ for more information
NewSummaryVec(name, description string, labelNames ...string) (*prometheus.SummaryVec, error)
MustNewSummaryVec(name, description string, labelNames ...string) *prometheus.SummaryVec
// Creates new prometheus.Histogram metric with the prefix as the CurrentScope
// Refer to https://prometheus.io/docs/concepts/metric_types/ for more information
NewHistogram(name, description string) (prometheus.Histogram, error)
MustNewHistogram(name, description string) prometheus.Histogram
// Creates new prometheus.HistogramVec metric with the prefix as the CurrentScope
// Refer to https://prometheus.io/docs/concepts/metric_types/ for more information
NewHistogramVec(name, description string, labelNames ...string) (*prometheus.HistogramVec, error)
MustNewHistogramVec(name, description string, labelNames ...string) *prometheus.HistogramVec
// Creates new prometheus.Counter metric with the prefix as the CurrentScope
// Refer to https://prometheus.io/docs/concepts/metric_types/ for more information
// Important to note, counters are not like typical counters. These are ever increasing and cumulative.
// So if you want to observe counters within buckets use Summary/Histogram
NewCounter(name, description string) (prometheus.Counter, error)
MustNewCounter(name, description string) prometheus.Counter
// Creates new prometheus.GaugeVec metric with the prefix as the CurrentScope
// Refer to https://prometheus.io/docs/concepts/metric_types/ for more information
NewCounterVec(name, description string, labelNames ...string) (*prometheus.CounterVec, error)
MustNewCounterVec(name, description string, labelNames ...string) *prometheus.CounterVec
// This is a custom wrapper to create a StopWatch object in the current Scope.
// Duration is to specify the scale of the Timer. For example if you are measuring times in milliseconds
// pass scale=times.Millisecond
// https://golang.org/pkg/time/#Duration
// The metric name is auto-suffixed with the right scale. Refer to DurationToString to understand
NewStopWatch(name, description string, scale time.Duration) (StopWatch, error)
MustNewStopWatch(name, description string, scale time.Duration) StopWatch
// This is a custom wrapper to create a StopWatch object in the current Scope.
// Duration is to specify the scale of the Timer. For example if you are measuring times in milliseconds
// pass scale=times.Millisecond
// https://golang.org/pkg/time/#Duration
// The metric name is auto-suffixed with the right scale. Refer to DurationToString to understand
NewStopWatchVec(name, description string, scale time.Duration, labelNames ...string) (*StopWatchVec, error)
MustNewStopWatchVec(name, description string, scale time.Duration, labelNames ...string) *StopWatchVec
// In case nesting is desired for metrics, create a new subScope. This is generally useful in creating
// Scoped and SubScoped metrics
NewSubScope(name string) Scope
// Returns the current ScopeName. Use for creating your own metrics
CurrentScope() string
// Method that provides a scoped metric name. Can be used, if you want to directly create your own metric
NewScopedMetricName(name string) string
}
type metricsScope struct {
scope string
}
func (m metricsScope) NewGauge(name, description string) (prometheus.Gauge, error) {
g := prometheus.NewGauge(
prometheus.GaugeOpts{
Name: m.NewScopedMetricName(name),
Help: description,
},
)
return g, prometheus.Register(g)
}
func (m metricsScope) MustNewGauge(name, description string) prometheus.Gauge {
g, err := m.NewGauge(name, description)
panicIfError(err)
return g
}
func (m metricsScope) NewGaugeVec(name, description string, labelNames ...string) (*prometheus.GaugeVec, error) {
g := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: m.NewScopedMetricName(name),
Help: description,
},
labelNames,
)
return g, prometheus.Register(g)
}
func (m metricsScope) MustNewGaugeVec(name, description string, labelNames ...string) *prometheus.GaugeVec {
g, err := m.NewGaugeVec(name, description, labelNames...)
panicIfError(err)
return g
}
func (m metricsScope) NewSummary(name, description string) (prometheus.Summary, error) {
return m.NewSummaryWithOptions(name, description, SummaryOptions{Objectives: defaultObjectives})
}
func (m metricsScope) MustNewSummary(name, description string) prometheus.Summary {
s, err := m.NewSummary(name, description)
panicIfError(err)
return s
}
func (m metricsScope) NewSummaryWithOptions(name, description string, options SummaryOptions) (prometheus.Summary, error) {
s := prometheus.NewSummary(
prometheus.SummaryOpts{
Name: m.NewScopedMetricName(name),
Help: description,
Objectives: options.Objectives,
},
)
return s, prometheus.Register(s)
}
func (m metricsScope) MustNewSummaryWithOptions(name, description string, options SummaryOptions) prometheus.Summary {
s, err := m.NewSummaryWithOptions(name, description, options)
panicIfError(err)
return s
}
func (m metricsScope) NewSummaryVec(name, description string, labelNames ...string) (*prometheus.SummaryVec, error) {
s := prometheus.NewSummaryVec(
prometheus.SummaryOpts{
Name: m.NewScopedMetricName(name),
Help: description,
Objectives: defaultObjectives,
},
labelNames,
)
return s, prometheus.Register(s)
}
func (m metricsScope) MustNewSummaryVec(name, description string, labelNames ...string) *prometheus.SummaryVec {
s, err := m.NewSummaryVec(name, description, labelNames...)
panicIfError(err)
return s
}
func (m metricsScope) NewHistogram(name, description string) (prometheus.Histogram, error) {
h := prometheus.NewHistogram(
prometheus.HistogramOpts{
Name: m.NewScopedMetricName(name),
Help: description,
Buckets: defaultBuckets,
},
)
return h, prometheus.Register(h)
}
func (m metricsScope) MustNewHistogram(name, description string) prometheus.Histogram {
h, err := m.NewHistogram(name, description)
panicIfError(err)
return h
}
func (m metricsScope) NewHistogramVec(name, description string, labelNames ...string) (*prometheus.HistogramVec, error) {
h := prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: m.NewScopedMetricName(name),
Help: description,
Buckets: defaultBuckets,
},
labelNames,
)
return h, prometheus.Register(h)
}
func (m metricsScope) MustNewHistogramVec(name, description string, labelNames ...string) *prometheus.HistogramVec {
h, err := m.NewHistogramVec(name, description, labelNames...)
panicIfError(err)
return h
}
func (m metricsScope) NewCounter(name, description string) (prometheus.Counter, error) {
c := prometheus.NewCounter(
prometheus.CounterOpts{
Name: m.NewScopedMetricName(name),
Help: description,
},
)
return c, prometheus.Register(c)
}
func (m metricsScope) MustNewCounter(name, description string) prometheus.Counter {
c, err := m.NewCounter(name, description)
panicIfError(err)
return c
}
func (m metricsScope) NewCounterVec(name, description string, labelNames ...string) (*prometheus.CounterVec, error) {
c := prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: m.NewScopedMetricName(name),
Help: description,
},
labelNames,
)
return c, prometheus.Register(c)
}
func (m metricsScope) MustNewCounterVec(name, description string, labelNames ...string) *prometheus.CounterVec {
c, err := m.NewCounterVec(name, description, labelNames...)
panicIfError(err)
return c
}
func (m metricsScope) NewStopWatch(name, description string, scale time.Duration) (StopWatch, error) {
if !strings.HasSuffix(name, defaultMetricDelimiterStr) {
name += defaultMetricDelimiterStr
}
name += DurationToString(scale)
s, err := m.NewSummary(name, description)
if err != nil {
return StopWatch{}, err
}
return StopWatch{
Observer: s,
outputScale: scale,
}, nil
}
func (m metricsScope) MustNewStopWatch(name, description string, scale time.Duration) StopWatch {
s, err := m.NewStopWatch(name, description, scale)
panicIfError(err)
return s
}
func (m metricsScope) NewStopWatchVec(name, description string, scale time.Duration, labelNames ...string) (*StopWatchVec, error) {
if !strings.HasSuffix(name, defaultMetricDelimiterStr) {
name += defaultMetricDelimiterStr
}
name += DurationToString(scale)
s, err := m.NewSummaryVec(name, description, labelNames...)
if err != nil {
return &StopWatchVec{}, err
}
return &StopWatchVec{
SummaryVec: s,
outputScale: scale,
}, nil
}
func (m metricsScope) MustNewStopWatchVec(name, description string, scale time.Duration, labelNames ...string) *StopWatchVec {
s, err := m.NewStopWatchVec(name, description, scale, labelNames...)
panicIfError(err)
return s
}
func (m metricsScope) CurrentScope() string {
return m.scope
}
// Creates a metric name under the scope. Scope will always have a defaultScopeDelimiterRune as the last character
func (m metricsScope) NewScopedMetricName(name string) string {
if name == "" {
panic("metric name cannot be an empty string")
}
return m.scope + name
}
func (m metricsScope) NewSubScope(subscopeName string) Scope {
if subscopeName == "" {
panic("scope name cannot be an empty string")
}
// If the last character of the new subscope is already a defaultScopeDelimiterRune, do not add anything
if !strings.HasSuffix(subscopeName, defaultScopeDelimiterStr) {
subscopeName += defaultScopeDelimiterStr
}
// Always add a new defaultScopeDelimiterRune to every scope name
return NewScope(m.scope + subscopeName)
}
// Creates a new scope in the format `name + defaultScopeDelimiterRune`
// If the last character is already a defaultScopeDelimiterRune, then it does not add it to the scope name
func NewScope(name string) Scope {
if name == "" {
panic("base scope for a metric cannot be an empty string")
}
// If the last character of the new subscope is already a defaultScopeDelimiterRune, do not add anything
if !strings.HasSuffix(name, defaultScopeDelimiterStr) {
name += defaultScopeDelimiterStr
}
return metricsScope{
scope: name,
}
}
// Returns a randomly-named scope for use in tests.
// Prometheus requires that metric names begin with a single word, which is generated from the alphabetic testScopeNameCharset.
func NewTestScope() Scope {
return NewScope("test" + rand.String(6))
}
// DurationToString converts the duration to a string suffix that indicates the scale of the timer.
func DurationToString(duration time.Duration) string {
if duration >= time.Hour {
return "h"
}
if duration >= time.Minute {
return "m"
}
if duration >= time.Second {
return "s"
}
if duration >= time.Millisecond {
return "ms"
}
if duration >= time.Microsecond {
return "us"
}
return "ns"
}