-
Notifications
You must be signed in to change notification settings - Fork 0
/
histogramvec2.go
60 lines (54 loc) · 1.61 KB
/
histogramvec2.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
package promvec
import (
"github.com/prometheus/client_golang/prometheus"
)
// HistogramVec2 bundles histogramss that have different labels in two dimensions,
// with a fixed set of possible label combinations.
type HistogramVec2 struct {
desc *prometheus.Desc
histograms map[[2]string]prometheus.Histogram
}
// NewHistogramVec2 creates a HistogramVec2, given histogram options and definitions
// of label names and label values.
func NewHistogramVec2(opts prometheus.HistogramOpts, labelNames [2]string, labelValues [][2]string) *HistogramVec2 {
v := HistogramVec2{
desc: prometheus.NewDesc(
prometheus.BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),
opts.Help,
labelNames[:],
opts.ConstLabels,
),
histograms: map[[2]string]prometheus.Histogram{},
}
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.histograms[lvs] = prometheus.NewHistogram(opts)
}
return &v
}
// Describe implements Collector.
func (v *HistogramVec2) Describe(ch chan<- *prometheus.Desc) {
ch <- v.desc
}
// Collect implements Collector.
func (v *HistogramVec2) Collect(ch chan<- prometheus.Metric) {
for _, c := range v.histograms {
ch <- c
}
}
// WithLabelValues returns the Histogram for the given label values, and
// panics if the labels are invalid.
func (v *HistogramVec2) WithLabelValues(lvs [2]string) prometheus.Histogram {
if c, ok := v.histograms[lvs]; !ok {
panic("unexpected label values: " + lvs[0] + ", " + lvs[1])
} else {
return c
}
}