-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcountervec2.go
62 lines (55 loc) · 1.58 KB
/
countervec2.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
package promvec
import (
"strings"
"github.com/prometheus/client_golang/prometheus"
)
// CounterVec2 bundles counters that have different labels in two dimensions,
// with a fixed set of possible label combinations.
type CounterVec2 struct {
desc *prometheus.Desc
counters map[[2]string]prometheus.Counter
}
// NewCounterVec2 creates a CounterVec2, given counter options and definitions
// of label names and label values.
func NewCounterVec2(opts prometheus.CounterOpts, labelNames [2]string, labelValues [][2]string) *CounterVec2 {
v := CounterVec2{
desc: prometheus.NewDesc(
prometheus.BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),
opts.Help,
labelNames[:],
opts.ConstLabels,
),
counters: map[[2]string]prometheus.Counter{},
}
for _, lvs := range labelValues {
labels := prometheus.Labels{}
for cl, cv := range opts.ConstLabels {
labels[cl] = cv
}
for i, l := range labelNames {
labels[l] = lvs[i]
}
opts.ConstLabels = labels
v.counters[lvs] = prometheus.NewCounter(opts)
}
return &v
}
// Describe implements Collector.
func (v *CounterVec2) Describe(ch chan<- *prometheus.Desc) {
ch <- v.desc
}
// Collect implements Collector.
func (v *CounterVec2) Collect(ch chan<- prometheus.Metric) {
for _, c := range v.counters {
ch <- c
}
}
// WithLabelValues returns the Counter for the given label values, and
// panics if the labels are invalid.
func (v *CounterVec2) WithLabelValues(lvs [2]string) prometheus.Counter {
if c, ok := v.counters[lvs]; !ok {
panic("unexpected label values: " + strings.Join(lvs[:], ", "))
} else {
return c
}
}