@@ -28,26 +28,6 @@ import (
2828// magicString is used for the hacky label test in checkLabels. Remove once fixed.
2929const magicString = "zZgWfBxLqvG8kc8IMv3POi2Bb0tZI3vAnBx+gBaFi9FyPzB/CzKUer1yufDa"
3030
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-
5131// InstrumentHandlerInFlight is a middleware that wraps the provided
5232// http.Handler. It sets the provided prometheus.Gauge to the number of
5333// requests currently handled by the wrapped http.Handler.
@@ -100,7 +80,7 @@ func InstrumentHandlerDuration(obs prometheus.ObserverVec, next http.Handler, op
10080 for label , resolve := range hOpts .extraLabelsFromCtx {
10181 l [label ] = resolve (r .Context ())
10282 }
103- observeWithExemplar ( obs .With (l ), time .Since (now ).Seconds (), hOpts .getExemplarFn (r .Context ()))
83+ obs .With (l ).(prometheus. ExemplarObserver ). ObserveWithExemplar ( time .Since (now ).Seconds (), hOpts .getExemplarFn (r .Context ()))
10484 }
10585 }
10686
@@ -111,7 +91,7 @@ func InstrumentHandlerDuration(obs prometheus.ObserverVec, next http.Handler, op
11191 for label , resolve := range hOpts .extraLabelsFromCtx {
11292 l [label ] = resolve (r .Context ())
11393 }
114- observeWithExemplar ( obs .With (l ), time .Since (now ).Seconds (), hOpts .getExemplarFn (r .Context ()))
94+ obs .With (l ).(prometheus. ExemplarObserver ). ObserveWithExemplar ( time .Since (now ).Seconds (), hOpts .getExemplarFn (r .Context ()))
11595 }
11696}
11797
@@ -150,7 +130,7 @@ func InstrumentHandlerCounter(counter *prometheus.CounterVec, next http.Handler,
150130 for label , resolve := range hOpts .extraLabelsFromCtx {
151131 l [label ] = resolve (r .Context ())
152132 }
153- addWithExemplar ( counter .With (l ), 1 , hOpts .getExemplarFn (r .Context ()))
133+ counter .With (l ).(prometheus. ExemplarAdder ). AddWithExemplar ( 1 , hOpts .getExemplarFn (r .Context ()))
154134 }
155135 }
156136
@@ -161,7 +141,7 @@ func InstrumentHandlerCounter(counter *prometheus.CounterVec, next http.Handler,
161141 for label , resolve := range hOpts .extraLabelsFromCtx {
162142 l [label ] = resolve (r .Context ())
163143 }
164- addWithExemplar ( counter .With (l ), 1 , hOpts .getExemplarFn (r .Context ()))
144+ counter .With (l ).(prometheus. ExemplarAdder ). AddWithExemplar ( 1 , hOpts .getExemplarFn (r .Context ()))
165145 }
166146}
167147
@@ -203,7 +183,7 @@ func InstrumentHandlerTimeToWriteHeader(obs prometheus.ObserverVec, next http.Ha
203183 for label , resolve := range hOpts .extraLabelsFromCtx {
204184 l [label ] = resolve (r .Context ())
205185 }
206- observeWithExemplar ( obs .With (l ), time .Since (now ).Seconds (), hOpts .getExemplarFn (r .Context ()))
186+ obs .With (l ).(prometheus. ExemplarObserver ). ObserveWithExemplar ( time .Since (now ).Seconds (), hOpts .getExemplarFn (r .Context ()))
207187 })
208188 next .ServeHTTP (d , r )
209189 }
@@ -247,7 +227,7 @@ func InstrumentHandlerRequestSize(obs prometheus.ObserverVec, next http.Handler,
247227 for label , resolve := range hOpts .extraLabelsFromCtx {
248228 l [label ] = resolve (r .Context ())
249229 }
250- observeWithExemplar ( obs .With (l ), float64 (size ), hOpts .getExemplarFn (r .Context ()))
230+ obs .With (l ).(prometheus. ExemplarObserver ). ObserveWithExemplar ( float64 (size ), hOpts .getExemplarFn (r .Context ()))
251231 }
252232 }
253233
@@ -259,7 +239,7 @@ func InstrumentHandlerRequestSize(obs prometheus.ObserverVec, next http.Handler,
259239 for label , resolve := range hOpts .extraLabelsFromCtx {
260240 l [label ] = resolve (r .Context ())
261241 }
262- observeWithExemplar ( obs .With (l ), float64 (size ), hOpts .getExemplarFn (r .Context ()))
242+ obs .With (l ).(prometheus. ExemplarObserver ). ObserveWithExemplar ( float64 (size ), hOpts .getExemplarFn (r .Context ()))
263243 }
264244}
265245
@@ -299,7 +279,7 @@ func InstrumentHandlerResponseSize(obs prometheus.ObserverVec, next http.Handler
299279 for label , resolve := range hOpts .extraLabelsFromCtx {
300280 l [label ] = resolve (r .Context ())
301281 }
302- observeWithExemplar ( obs .With (l ), float64 (d .Written ()), hOpts .getExemplarFn (r .Context ()))
282+ obs .With (l ).(prometheus. ExemplarObserver ). ObserveWithExemplar ( float64 (d .Written ()), hOpts .getExemplarFn (r .Context ()))
303283 })
304284}
305285
0 commit comments