@@ -28,22 +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- obs .(prometheus.ExemplarObserver ).ObserveWithExemplar (val , labels )
35- }
36-
37- // addWithExemplar is a wrapper for [prometheus.ExemplarAdder.AddWithExemplar],
38- // which falls back to [prometheus.Counter.Add] if no labels are provided.
39- func addWithExemplar (obs prometheus.Counter , val float64 , labels map [string ]string ) {
40- if labels == nil {
41- obs .Add (val )
42- return
43- }
44- obs .(prometheus.ExemplarAdder ).AddWithExemplar (val , labels )
45- }
46-
4731// InstrumentHandlerInFlight is a middleware that wraps the provided
4832// http.Handler. It sets the provided prometheus.Gauge to the number of
4933// requests currently handled by the wrapped http.Handler.
@@ -96,7 +80,7 @@ func InstrumentHandlerDuration(obs prometheus.ObserverVec, next http.Handler, op
9680 for label , resolve := range hOpts .extraLabelsFromCtx {
9781 l [label ] = resolve (r .Context ())
9882 }
99- 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 ()))
10084 }
10185 }
10286
@@ -107,7 +91,7 @@ func InstrumentHandlerDuration(obs prometheus.ObserverVec, next http.Handler, op
10791 for label , resolve := range hOpts .extraLabelsFromCtx {
10892 l [label ] = resolve (r .Context ())
10993 }
110- 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 ()))
11195 }
11296}
11397
@@ -146,7 +130,7 @@ func InstrumentHandlerCounter(counter *prometheus.CounterVec, next http.Handler,
146130 for label , resolve := range hOpts .extraLabelsFromCtx {
147131 l [label ] = resolve (r .Context ())
148132 }
149- addWithExemplar ( counter .With (l ), 1 , hOpts .getExemplarFn (r .Context ()))
133+ counter .With (l ).(prometheus. ExemplarAdder ). AddWithExemplar ( 1 , hOpts .getExemplarFn (r .Context ()))
150134 }
151135 }
152136
@@ -157,7 +141,7 @@ func InstrumentHandlerCounter(counter *prometheus.CounterVec, next http.Handler,
157141 for label , resolve := range hOpts .extraLabelsFromCtx {
158142 l [label ] = resolve (r .Context ())
159143 }
160- addWithExemplar ( counter .With (l ), 1 , hOpts .getExemplarFn (r .Context ()))
144+ counter .With (l ).(prometheus. ExemplarAdder ). AddWithExemplar ( 1 , hOpts .getExemplarFn (r .Context ()))
161145 }
162146}
163147
@@ -199,7 +183,7 @@ func InstrumentHandlerTimeToWriteHeader(obs prometheus.ObserverVec, next http.Ha
199183 for label , resolve := range hOpts .extraLabelsFromCtx {
200184 l [label ] = resolve (r .Context ())
201185 }
202- 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 ()))
203187 })
204188 next .ServeHTTP (d , r )
205189 }
@@ -243,7 +227,7 @@ func InstrumentHandlerRequestSize(obs prometheus.ObserverVec, next http.Handler,
243227 for label , resolve := range hOpts .extraLabelsFromCtx {
244228 l [label ] = resolve (r .Context ())
245229 }
246- observeWithExemplar ( obs .With (l ), float64 (size ), hOpts .getExemplarFn (r .Context ()))
230+ obs .With (l ).(prometheus. ExemplarObserver ). ObserveWithExemplar ( float64 (size ), hOpts .getExemplarFn (r .Context ()))
247231 }
248232 }
249233
@@ -255,7 +239,7 @@ func InstrumentHandlerRequestSize(obs prometheus.ObserverVec, next http.Handler,
255239 for label , resolve := range hOpts .extraLabelsFromCtx {
256240 l [label ] = resolve (r .Context ())
257241 }
258- observeWithExemplar ( obs .With (l ), float64 (size ), hOpts .getExemplarFn (r .Context ()))
242+ obs .With (l ).(prometheus. ExemplarObserver ). ObserveWithExemplar ( float64 (size ), hOpts .getExemplarFn (r .Context ()))
259243 }
260244}
261245
@@ -295,7 +279,7 @@ func InstrumentHandlerResponseSize(obs prometheus.ObserverVec, next http.Handler
295279 for label , resolve := range hOpts .extraLabelsFromCtx {
296280 l [label ] = resolve (r .Context ())
297281 }
298- 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 ()))
299283 })
300284}
301285
0 commit comments