@@ -28,6 +28,26 @@ 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+
3151// InstrumentHandlerInFlight is a middleware that wraps the provided
3252// http.Handler. It sets the provided prometheus.Gauge to the number of
3353// requests currently handled by the wrapped http.Handler.
@@ -80,7 +100,7 @@ func InstrumentHandlerDuration(obs prometheus.ObserverVec, next http.Handler, op
80100 for label , resolve := range hOpts .extraLabelsFromCtx {
81101 l [label ] = resolve (r .Context ())
82102 }
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 ()))
84104 }
85105 }
86106
@@ -91,7 +111,7 @@ func InstrumentHandlerDuration(obs prometheus.ObserverVec, next http.Handler, op
91111 for label , resolve := range hOpts .extraLabelsFromCtx {
92112 l [label ] = resolve (r .Context ())
93113 }
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 ()))
95115 }
96116}
97117
@@ -130,7 +150,7 @@ func InstrumentHandlerCounter(counter *prometheus.CounterVec, next http.Handler,
130150 for label , resolve := range hOpts .extraLabelsFromCtx {
131151 l [label ] = resolve (r .Context ())
132152 }
133- counter .With (l ).(prometheus. ExemplarAdder ). AddWithExemplar ( 1 , hOpts .getExemplarFn (r .Context ()))
153+ addWithExemplar ( counter .With (l ), 1 , hOpts .getExemplarFn (r .Context ()))
134154 }
135155 }
136156
@@ -141,7 +161,7 @@ func InstrumentHandlerCounter(counter *prometheus.CounterVec, next http.Handler,
141161 for label , resolve := range hOpts .extraLabelsFromCtx {
142162 l [label ] = resolve (r .Context ())
143163 }
144- counter .With (l ).(prometheus. ExemplarAdder ). AddWithExemplar ( 1 , hOpts .getExemplarFn (r .Context ()))
164+ addWithExemplar ( counter .With (l ), 1 , hOpts .getExemplarFn (r .Context ()))
145165 }
146166}
147167
@@ -183,7 +203,7 @@ func InstrumentHandlerTimeToWriteHeader(obs prometheus.ObserverVec, next http.Ha
183203 for label , resolve := range hOpts .extraLabelsFromCtx {
184204 l [label ] = resolve (r .Context ())
185205 }
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 ()))
187207 })
188208 next .ServeHTTP (d , r )
189209 }
@@ -227,7 +247,7 @@ func InstrumentHandlerRequestSize(obs prometheus.ObserverVec, next http.Handler,
227247 for label , resolve := range hOpts .extraLabelsFromCtx {
228248 l [label ] = resolve (r .Context ())
229249 }
230- obs .With (l ).(prometheus. ExemplarObserver ). ObserveWithExemplar ( float64 (size ), hOpts .getExemplarFn (r .Context ()))
250+ observeWithExemplar ( obs .With (l ), float64 (size ), hOpts .getExemplarFn (r .Context ()))
231251 }
232252 }
233253
@@ -239,7 +259,7 @@ func InstrumentHandlerRequestSize(obs prometheus.ObserverVec, next http.Handler,
239259 for label , resolve := range hOpts .extraLabelsFromCtx {
240260 l [label ] = resolve (r .Context ())
241261 }
242- obs .With (l ).(prometheus. ExemplarObserver ). ObserveWithExemplar ( float64 (size ), hOpts .getExemplarFn (r .Context ()))
262+ observeWithExemplar ( obs .With (l ), float64 (size ), hOpts .getExemplarFn (r .Context ()))
243263 }
244264}
245265
@@ -279,7 +299,7 @@ func InstrumentHandlerResponseSize(obs prometheus.ObserverVec, next http.Handler
279299 for label , resolve := range hOpts .extraLabelsFromCtx {
280300 l [label ] = resolve (r .Context ())
281301 }
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 ()))
283303 })
284304}
285305
0 commit comments