-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhistogram.go
More file actions
42 lines (35 loc) · 1.2 KB
/
histogram.go
File metadata and controls
42 lines (35 loc) · 1.2 KB
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
package metrics
import "github.com/prometheus/client_golang/prometheus"
// Histogram creates a histogram metric
func Histogram(name, help string, buckets []float64) HistogramMetric {
vec := prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: mustValidMetricName(name),
Help: help,
Buckets: buckets,
}, []string{})
prometheus.MustRegister(vec)
return HistogramMetric{vec: vec}
}
// HistogramWith creates a histogram metric with typed labels
func HistogramWith[T any](name, help string, buckets []float64) HistogramMetricLabeled[T] {
vec := prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: mustValidMetricName(name),
Help: help,
Buckets: buckets,
}, getLabelKeys[T]())
prometheus.MustRegister(vec)
return HistogramMetricLabeled[T]{vec: vec}
}
type HistogramMetric struct {
vec *prometheus.HistogramVec
}
func (h *HistogramMetric) Observe(value float64) {
h.vec.With(prometheus.Labels{}).Observe(value)
}
// HistogramMetric represents a histogram metric with typed labels
type HistogramMetricLabeled[T any] struct {
vec *prometheus.HistogramVec
}
func (h *HistogramMetricLabeled[T]) Observe(value float64, labels T) {
h.vec.With(getLabelValues(labels)).Observe(value)
}