๐Ÿ“ฆ go-chi / metrics

๐Ÿ“„ gauge.go ยท 64 lines
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
63
64package metrics

import "github.com/prometheus/client_golang/prometheus"

// Gauge creates a gauge metric
func Gauge(name, help string) GaugeMetric {
	vec := prometheus.NewGaugeVec(prometheus.GaugeOpts{
		Name: mustValidMetricName(name),
		Help: help,
	}, []string{})
	prometheus.MustRegister(vec)
	return GaugeMetric{vec: vec}
}

// GaugeWith creates a gauge metric with typed labels
func GaugeWith[T any](name, help string) GaugeMetricLabeled[T] {
	vec := prometheus.NewGaugeVec(prometheus.GaugeOpts{
		Name: mustValidMetricName(name),
		Help: help,
	}, getLabelKeys[T]())
	prometheus.MustRegister(vec)
	return GaugeMetricLabeled[T]{vec: vec}
}

type GaugeMetric struct {
	vec *prometheus.GaugeVec
}

func (g *GaugeMetric) Set(value float64) {
	g.vec.With(prometheus.Labels{}).Set(value)
}

func (g *GaugeMetric) Add(value float64) {
	g.vec.With(prometheus.Labels{}).Add(value)
}

func (g *GaugeMetric) Inc() {
	g.vec.With(prometheus.Labels{}).Add(1.0)
}

func (g *GaugeMetric) Dec() {
	g.vec.With(prometheus.Labels{}).Add(-1.0)
}

type GaugeMetricLabeled[T any] struct {
	vec *prometheus.GaugeVec
}

func (g *GaugeMetricLabeled[T]) Set(value float64, labels T) {
	g.vec.With(getLabelValues(labels)).Set(value)
}

func (g *GaugeMetricLabeled[T]) Add(value float64, labels T) {
	g.vec.With(getLabelValues(labels)).Add(value)
}

func (g *GaugeMetricLabeled[T]) Inc(labels T) {
	g.vec.With(getLabelValues(labels)).Add(1.0)
}

func (g *GaugeMetricLabeled[T]) Dec(labels T) {
	g.vec.With(getLabelValues(labels)).Add(-1.0)
}