-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcountervec3.go
62 lines (55 loc) · 1.58 KB
/
countervec3.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"
)
// CounterVec3 bundles counters that have different labels in three dimensions,
// with a fixed set of possible label combinations.
type CounterVec3 struct {
desc *prometheus.Desc
counters map[[3]string]prometheus.Counter
}
// NewCounterVec3 creates a CounterVec3, given counter options and definitions
// of label names and label values.
func NewCounterVec3(opts prometheus.CounterOpts, labelNames [3]string, labelValues [][3]string) *CounterVec3 {
v := CounterVec3{
desc: prometheus.NewDesc(
prometheus.BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),
opts.Help,
labelNames[:],
opts.ConstLabels,
),
counters: map[[3]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 *CounterVec3) Describe(ch chan<- *prometheus.Desc) {
ch <- v.desc
}
// Collect implements Collector.
func (v *CounterVec3) 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 *CounterVec3) WithLabelValues(lvs [3]string) prometheus.Counter {
if c, ok := v.counters[lvs]; !ok {
panic("unexpected label values: " + strings.Join(lvs[:], ", "))
} else {
return c
}
}