...
1
2
3
4
5 package prometheus
6
7 import (
8 "github.com/prometheus/client_golang/prometheus"
9
10 "github.com/go-kit/kit/metrics"
11 "github.com/go-kit/kit/metrics/internal/lv"
12 )
13
14
15 type Counter struct {
16 cv *prometheus.CounterVec
17 lvs lv.LabelValues
18 }
19
20
21
22 func NewCounterFrom(opts prometheus.CounterOpts, labelNames []string) *Counter {
23 cv := prometheus.NewCounterVec(opts, labelNames)
24 prometheus.MustRegister(cv)
25 return NewCounter(cv)
26 }
27
28
29 func NewCounter(cv *prometheus.CounterVec) *Counter {
30 return &Counter{
31 cv: cv,
32 }
33 }
34
35
36 func (c *Counter) With(labelValues ...string) metrics.Counter {
37 return &Counter{
38 cv: c.cv,
39 lvs: c.lvs.With(labelValues...),
40 }
41 }
42
43
44 func (c *Counter) Add(delta float64) {
45 c.cv.With(makeLabels(c.lvs...)).Add(delta)
46 }
47
48
49 type Gauge struct {
50 gv *prometheus.GaugeVec
51 lvs lv.LabelValues
52 }
53
54
55
56 func NewGaugeFrom(opts prometheus.GaugeOpts, labelNames []string) *Gauge {
57 gv := prometheus.NewGaugeVec(opts, labelNames)
58 prometheus.MustRegister(gv)
59 return NewGauge(gv)
60 }
61
62
63 func NewGauge(gv *prometheus.GaugeVec) *Gauge {
64 return &Gauge{
65 gv: gv,
66 }
67 }
68
69
70 func (g *Gauge) With(labelValues ...string) metrics.Gauge {
71 return &Gauge{
72 gv: g.gv,
73 lvs: g.lvs.With(labelValues...),
74 }
75 }
76
77
78 func (g *Gauge) Set(value float64) {
79 g.gv.With(makeLabels(g.lvs...)).Set(value)
80 }
81
82
83 func (g *Gauge) Add(delta float64) {
84 g.gv.With(makeLabels(g.lvs...)).Add(delta)
85 }
86
87
88
89
90 type Summary struct {
91 sv *prometheus.SummaryVec
92 lvs lv.LabelValues
93 }
94
95
96
97 func NewSummaryFrom(opts prometheus.SummaryOpts, labelNames []string) *Summary {
98 sv := prometheus.NewSummaryVec(opts, labelNames)
99 prometheus.MustRegister(sv)
100 return NewSummary(sv)
101 }
102
103
104 func NewSummary(sv *prometheus.SummaryVec) *Summary {
105 return &Summary{
106 sv: sv,
107 }
108 }
109
110
111 func (s *Summary) With(labelValues ...string) metrics.Histogram {
112 return &Summary{
113 sv: s.sv,
114 lvs: s.lvs.With(labelValues...),
115 }
116 }
117
118
119 func (s *Summary) Observe(value float64) {
120 s.sv.With(makeLabels(s.lvs...)).Observe(value)
121 }
122
123
124
125
126 type Histogram struct {
127 hv *prometheus.HistogramVec
128 lvs lv.LabelValues
129 }
130
131
132
133 func NewHistogramFrom(opts prometheus.HistogramOpts, labelNames []string) *Histogram {
134 hv := prometheus.NewHistogramVec(opts, labelNames)
135 prometheus.MustRegister(hv)
136 return NewHistogram(hv)
137 }
138
139
140 func NewHistogram(hv *prometheus.HistogramVec) *Histogram {
141 return &Histogram{
142 hv: hv,
143 }
144 }
145
146
147 func (h *Histogram) With(labelValues ...string) metrics.Histogram {
148 return &Histogram{
149 hv: h.hv,
150 lvs: h.lvs.With(labelValues...),
151 }
152 }
153
154
155 func (h *Histogram) Observe(value float64) {
156 h.hv.With(makeLabels(h.lvs...)).Observe(value)
157 }
158
159 func makeLabels(labelValues ...string) prometheus.Labels {
160 labels := prometheus.Labels{}
161 for i := 0; i < len(labelValues); i += 2 {
162 labels[labelValues[i]] = labelValues[i+1]
163 }
164 return labels
165 }
166
View as plain text