-
Notifications
You must be signed in to change notification settings - Fork 16
/
metric.go
44 lines (37 loc) · 1.41 KB
/
metric.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
package metric
import (
"github.com/prometheus/client_golang/prometheus"
)
// SettableCounterOpts is an alias for prometheus.GaugeOpts.
type SettableCounterOpts prometheus.GaugeOpts
// SettableCounterVec is a Counter metric that can be updated by using `Set()`.
//
// Some folks suggest implementing custom collector, see
// https://www.robustperception.io/setting-a-prometheus-counter
type SettableCounterVec struct {
*prometheus.GaugeVec
}
// NewSettableCounterVec constructs and returns a SettableGaugeVec.
func NewSettableCounterVec(opts SettableCounterOpts, labelNames []string) *SettableCounterVec {
gv := prometheus.NewGaugeVec(prometheus.GaugeOpts(opts), labelNames)
return &SettableCounterVec{GaugeVec: gv}
}
// NewSettableCounterVecFrom constructs, registers and returns a SettableGaugeVec.
func NewSettableCounterVecFrom(opts SettableCounterOpts, labelNames []string) *SettableCounterVec {
scv := NewSettableCounterVec(opts, labelNames)
prometheus.MustRegister(scv)
return scv
}
// NewGaugeFrom constructs, registers and returns a GaugeVec.
func NewGaugeVecFrom(opts prometheus.GaugeOpts, labelNames []string) *prometheus.GaugeVec {
gv := prometheus.NewGaugeVec(opts, labelNames)
prometheus.MustRegister(gv)
return gv
}
func MakeLabels(labelValues ...string) prometheus.Labels {
labels := prometheus.Labels{}
for i := 0; i < len(labelValues); i += 2 {
labels[labelValues[i]] = labelValues[i+1]
}
return labels
}