@@ -28,15 +28,19 @@ import (
2828// magicString is used for the hacky label test in checkLabels. Remove once fixed.
2929const magicString = "zZgWfBxLqvG8kc8IMv3POi2Bb0tZI3vAnBx+gBaFi9FyPzB/CzKUer1yufDa"
3030
31- func exemplarObserve (obs prometheus.Observer , val float64 , labels map [string ]string ) {
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 ) {
3234 if labels == nil {
3335 obs .Observe (val )
3436 return
3537 }
3638 obs .(prometheus.ExemplarObserver ).ObserveWithExemplar (val , labels )
3739}
3840
39- func exemplarAdd (obs prometheus.Counter , val float64 , labels map [string ]string ) {
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 ) {
4044 if labels == nil {
4145 obs .Add (val )
4246 return
@@ -91,7 +95,7 @@ func InstrumentHandlerDuration(obs prometheus.ObserverVec, next http.Handler, op
9195 d := newDelegator (w , nil )
9296 next .ServeHTTP (d , r )
9397
94- exemplarObserve (
98+ observeWithExemplar (
9599 obs .With (labels (code , method , r .Method , d .Status (), hOpts .extraMethods ... )),
96100 time .Since (now ).Seconds (),
97101 hOpts .getExemplarFn (r .Context ()),
@@ -103,7 +107,7 @@ func InstrumentHandlerDuration(obs prometheus.ObserverVec, next http.Handler, op
103107 now := time .Now ()
104108 next .ServeHTTP (w , r )
105109
106- exemplarObserve (
110+ observeWithExemplar (
107111 obs .With (labels (code , method , r .Method , 0 , hOpts .extraMethods ... )),
108112 time .Since (now ).Seconds (),
109113 hOpts .getExemplarFn (r .Context ()),
@@ -141,7 +145,7 @@ func InstrumentHandlerCounter(counter *prometheus.CounterVec, next http.Handler,
141145 d := newDelegator (w , nil )
142146 next .ServeHTTP (d , r )
143147
144- exemplarAdd (
148+ addWithExemplar (
145149 counter .With (labels (code , method , r .Method , d .Status (), hOpts .extraMethods ... )),
146150 1 ,
147151 hOpts .getExemplarFn (r .Context ()),
@@ -151,7 +155,7 @@ func InstrumentHandlerCounter(counter *prometheus.CounterVec, next http.Handler,
151155
152156 return func (w http.ResponseWriter , r * http.Request ) {
153157 next .ServeHTTP (w , r )
154- exemplarAdd (
158+ addWithExemplar (
155159 counter .With (labels (code , method , r .Method , 0 , hOpts .extraMethods ... )),
156160 1 ,
157161 hOpts .getExemplarFn (r .Context ()),
@@ -192,7 +196,7 @@ func InstrumentHandlerTimeToWriteHeader(obs prometheus.ObserverVec, next http.Ha
192196 return func (w http.ResponseWriter , r * http.Request ) {
193197 now := time .Now ()
194198 d := newDelegator (w , func (status int ) {
195- exemplarObserve (
199+ observeWithExemplar (
196200 obs .With (labels (code , method , r .Method , status , hOpts .extraMethods ... )),
197201 time .Since (now ).Seconds (),
198202 hOpts .getExemplarFn (r .Context ()),
@@ -233,7 +237,7 @@ func InstrumentHandlerRequestSize(obs prometheus.ObserverVec, next http.Handler,
233237 d := newDelegator (w , nil )
234238 next .ServeHTTP (d , r )
235239 size := computeApproximateRequestSize (r )
236- exemplarObserve (
240+ observeWithExemplar (
237241 obs .With (labels (code , method , r .Method , d .Status (), hOpts .extraMethods ... )),
238242 float64 (size ),
239243 hOpts .getExemplarFn (r .Context ()),
@@ -244,7 +248,7 @@ func InstrumentHandlerRequestSize(obs prometheus.ObserverVec, next http.Handler,
244248 return func (w http.ResponseWriter , r * http.Request ) {
245249 next .ServeHTTP (w , r )
246250 size := computeApproximateRequestSize (r )
247- exemplarObserve (
251+ observeWithExemplar (
248252 obs .With (labels (code , method , r .Method , 0 , hOpts .extraMethods ... )),
249253 float64 (size ),
250254 hOpts .getExemplarFn (r .Context ()),
@@ -282,7 +286,7 @@ func InstrumentHandlerResponseSize(obs prometheus.ObserverVec, next http.Handler
282286 return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
283287 d := newDelegator (w , nil )
284288 next .ServeHTTP (d , r )
285- exemplarObserve (
289+ observeWithExemplar (
286290 obs .With (labels (code , method , r .Method , d .Status (), hOpts .extraMethods ... )),
287291 float64 (d .Written ()),
288292 hOpts .getExemplarFn (r .Context ()),
0 commit comments