@@ -28,15 +28,19 @@ 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
- 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 ) {
32
34
if labels == nil {
33
35
obs .Observe (val )
34
36
return
35
37
}
36
38
obs .(prometheus.ExemplarObserver ).ObserveWithExemplar (val , labels )
37
39
}
38
40
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 ) {
40
44
if labels == nil {
41
45
obs .Add (val )
42
46
return
@@ -91,7 +95,7 @@ func InstrumentHandlerDuration(obs prometheus.ObserverVec, next http.Handler, op
91
95
d := newDelegator (w , nil )
92
96
next .ServeHTTP (d , r )
93
97
94
- exemplarObserve (
98
+ observeWithExemplar (
95
99
obs .With (labels (code , method , r .Method , d .Status (), hOpts .extraMethods ... )),
96
100
time .Since (now ).Seconds (),
97
101
hOpts .getExemplarFn (r .Context ()),
@@ -103,7 +107,7 @@ func InstrumentHandlerDuration(obs prometheus.ObserverVec, next http.Handler, op
103
107
now := time .Now ()
104
108
next .ServeHTTP (w , r )
105
109
106
- exemplarObserve (
110
+ observeWithExemplar (
107
111
obs .With (labels (code , method , r .Method , 0 , hOpts .extraMethods ... )),
108
112
time .Since (now ).Seconds (),
109
113
hOpts .getExemplarFn (r .Context ()),
@@ -141,7 +145,7 @@ func InstrumentHandlerCounter(counter *prometheus.CounterVec, next http.Handler,
141
145
d := newDelegator (w , nil )
142
146
next .ServeHTTP (d , r )
143
147
144
- exemplarAdd (
148
+ addWithExemplar (
145
149
counter .With (labels (code , method , r .Method , d .Status (), hOpts .extraMethods ... )),
146
150
1 ,
147
151
hOpts .getExemplarFn (r .Context ()),
@@ -151,7 +155,7 @@ func InstrumentHandlerCounter(counter *prometheus.CounterVec, next http.Handler,
151
155
152
156
return func (w http.ResponseWriter , r * http.Request ) {
153
157
next .ServeHTTP (w , r )
154
- exemplarAdd (
158
+ addWithExemplar (
155
159
counter .With (labels (code , method , r .Method , 0 , hOpts .extraMethods ... )),
156
160
1 ,
157
161
hOpts .getExemplarFn (r .Context ()),
@@ -192,7 +196,7 @@ func InstrumentHandlerTimeToWriteHeader(obs prometheus.ObserverVec, next http.Ha
192
196
return func (w http.ResponseWriter , r * http.Request ) {
193
197
now := time .Now ()
194
198
d := newDelegator (w , func (status int ) {
195
- exemplarObserve (
199
+ observeWithExemplar (
196
200
obs .With (labels (code , method , r .Method , status , hOpts .extraMethods ... )),
197
201
time .Since (now ).Seconds (),
198
202
hOpts .getExemplarFn (r .Context ()),
@@ -233,7 +237,7 @@ func InstrumentHandlerRequestSize(obs prometheus.ObserverVec, next http.Handler,
233
237
d := newDelegator (w , nil )
234
238
next .ServeHTTP (d , r )
235
239
size := computeApproximateRequestSize (r )
236
- exemplarObserve (
240
+ observeWithExemplar (
237
241
obs .With (labels (code , method , r .Method , d .Status (), hOpts .extraMethods ... )),
238
242
float64 (size ),
239
243
hOpts .getExemplarFn (r .Context ()),
@@ -244,7 +248,7 @@ func InstrumentHandlerRequestSize(obs prometheus.ObserverVec, next http.Handler,
244
248
return func (w http.ResponseWriter , r * http.Request ) {
245
249
next .ServeHTTP (w , r )
246
250
size := computeApproximateRequestSize (r )
247
- exemplarObserve (
251
+ observeWithExemplar (
248
252
obs .With (labels (code , method , r .Method , 0 , hOpts .extraMethods ... )),
249
253
float64 (size ),
250
254
hOpts .getExemplarFn (r .Context ()),
@@ -282,7 +286,7 @@ func InstrumentHandlerResponseSize(obs prometheus.ObserverVec, next http.Handler
282
286
return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
283
287
d := newDelegator (w , nil )
284
288
next .ServeHTTP (d , r )
285
- exemplarObserve (
289
+ observeWithExemplar (
286
290
obs .With (labels (code , method , r .Method , d .Status (), hOpts .extraMethods ... )),
287
291
float64 (d .Written ()),
288
292
hOpts .getExemplarFn (r .Context ()),
0 commit comments