@@ -28,6 +28,26 @@ import (
28
28
// magicString is used for the hacky label test in checkLabels. Remove once fixed.
29
29
const magicString = "zZgWfBxLqvG8kc8IMv3POi2Bb0tZI3vAnBx+gBaFi9FyPzB/CzKUer1yufDa"
30
30
31
+ // observeWithExemplar is a wrapper for [prometheus.ExemplarAdder.ExemplarObserver],
32
+ // which falls back to [prometheus.Observer.Observe] if no labels are provided.
33
+ func observeWithExemplar (obs prometheus.Observer , val float64 , labels map [string ]string ) {
34
+ if labels == nil {
35
+ obs .Observe (val )
36
+ return
37
+ }
38
+ obs .(prometheus.ExemplarObserver ).ObserveWithExemplar (val , labels )
39
+ }
40
+
41
+ // addWithExemplar is a wrapper for [prometheus.ExemplarAdder.AddWithExemplar],
42
+ // which falls back to [prometheus.Counter.Add] if no labels are provided.
43
+ func addWithExemplar (obs prometheus.Counter , val float64 , labels map [string ]string ) {
44
+ if labels == nil {
45
+ obs .Add (val )
46
+ return
47
+ }
48
+ obs .(prometheus.ExemplarAdder ).AddWithExemplar (val , labels )
49
+ }
50
+
31
51
// InstrumentHandlerInFlight is a middleware that wraps the provided
32
52
// http.Handler. It sets the provided prometheus.Gauge to the number of
33
53
// requests currently handled by the wrapped http.Handler.
@@ -80,7 +100,7 @@ func InstrumentHandlerDuration(obs prometheus.ObserverVec, next http.Handler, op
80
100
for label , resolve := range hOpts .extraLabelsFromCtx {
81
101
l [label ] = resolve (r .Context ())
82
102
}
83
- obs .With (l ).(prometheus. ExemplarObserver ). ObserveWithExemplar ( time .Since (now ).Seconds (), hOpts .getExemplarFn (r .Context ()))
103
+ observeWithExemplar ( obs .With (l ), time .Since (now ).Seconds (), hOpts .getExemplarFn (r .Context ()))
84
104
}
85
105
}
86
106
@@ -91,7 +111,7 @@ func InstrumentHandlerDuration(obs prometheus.ObserverVec, next http.Handler, op
91
111
for label , resolve := range hOpts .extraLabelsFromCtx {
92
112
l [label ] = resolve (r .Context ())
93
113
}
94
- obs .With (l ).(prometheus. ExemplarObserver ). ObserveWithExemplar ( time .Since (now ).Seconds (), hOpts .getExemplarFn (r .Context ()))
114
+ observeWithExemplar ( obs .With (l ), time .Since (now ).Seconds (), hOpts .getExemplarFn (r .Context ()))
95
115
}
96
116
}
97
117
@@ -130,7 +150,7 @@ func InstrumentHandlerCounter(counter *prometheus.CounterVec, next http.Handler,
130
150
for label , resolve := range hOpts .extraLabelsFromCtx {
131
151
l [label ] = resolve (r .Context ())
132
152
}
133
- counter .With (l ).(prometheus. ExemplarAdder ). AddWithExemplar ( 1 , hOpts .getExemplarFn (r .Context ()))
153
+ addWithExemplar ( counter .With (l ), 1 , hOpts .getExemplarFn (r .Context ()))
134
154
}
135
155
}
136
156
@@ -141,7 +161,7 @@ func InstrumentHandlerCounter(counter *prometheus.CounterVec, next http.Handler,
141
161
for label , resolve := range hOpts .extraLabelsFromCtx {
142
162
l [label ] = resolve (r .Context ())
143
163
}
144
- counter .With (l ).(prometheus. ExemplarAdder ). AddWithExemplar ( 1 , hOpts .getExemplarFn (r .Context ()))
164
+ addWithExemplar ( counter .With (l ), 1 , hOpts .getExemplarFn (r .Context ()))
145
165
}
146
166
}
147
167
@@ -183,7 +203,7 @@ func InstrumentHandlerTimeToWriteHeader(obs prometheus.ObserverVec, next http.Ha
183
203
for label , resolve := range hOpts .extraLabelsFromCtx {
184
204
l [label ] = resolve (r .Context ())
185
205
}
186
- obs .With (l ).(prometheus. ExemplarObserver ). ObserveWithExemplar ( time .Since (now ).Seconds (), hOpts .getExemplarFn (r .Context ()))
206
+ observeWithExemplar ( obs .With (l ), time .Since (now ).Seconds (), hOpts .getExemplarFn (r .Context ()))
187
207
})
188
208
next .ServeHTTP (d , r )
189
209
}
@@ -227,7 +247,7 @@ func InstrumentHandlerRequestSize(obs prometheus.ObserverVec, next http.Handler,
227
247
for label , resolve := range hOpts .extraLabelsFromCtx {
228
248
l [label ] = resolve (r .Context ())
229
249
}
230
- obs .With (l ).(prometheus. ExemplarObserver ). ObserveWithExemplar ( float64 (size ), hOpts .getExemplarFn (r .Context ()))
250
+ observeWithExemplar ( obs .With (l ), float64 (size ), hOpts .getExemplarFn (r .Context ()))
231
251
}
232
252
}
233
253
@@ -239,7 +259,7 @@ func InstrumentHandlerRequestSize(obs prometheus.ObserverVec, next http.Handler,
239
259
for label , resolve := range hOpts .extraLabelsFromCtx {
240
260
l [label ] = resolve (r .Context ())
241
261
}
242
- obs .With (l ).(prometheus. ExemplarObserver ). ObserveWithExemplar ( float64 (size ), hOpts .getExemplarFn (r .Context ()))
262
+ observeWithExemplar ( obs .With (l ), float64 (size ), hOpts .getExemplarFn (r .Context ()))
243
263
}
244
264
}
245
265
@@ -279,7 +299,7 @@ func InstrumentHandlerResponseSize(obs prometheus.ObserverVec, next http.Handler
279
299
for label , resolve := range hOpts .extraLabelsFromCtx {
280
300
l [label ] = resolve (r .Context ())
281
301
}
282
- obs .With (l ).(prometheus. ExemplarObserver ). ObserveWithExemplar ( float64 (d .Written ()), hOpts .getExemplarFn (r .Context ()))
302
+ observeWithExemplar ( obs .With (l ), float64 (d .Written ()), hOpts .getExemplarFn (r .Context ()))
283
303
})
284
304
}
285
305
0 commit comments