@@ -28,26 +28,6 @@ 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
- // 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
-
51
31
// InstrumentHandlerInFlight is a middleware that wraps the provided
52
32
// http.Handler. It sets the provided prometheus.Gauge to the number of
53
33
// requests currently handled by the wrapped http.Handler.
@@ -100,7 +80,7 @@ func InstrumentHandlerDuration(obs prometheus.ObserverVec, next http.Handler, op
100
80
for label , resolve := range hOpts .extraLabelsFromCtx {
101
81
l [label ] = resolve (r .Context ())
102
82
}
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 ()))
104
84
}
105
85
}
106
86
@@ -111,7 +91,7 @@ func InstrumentHandlerDuration(obs prometheus.ObserverVec, next http.Handler, op
111
91
for label , resolve := range hOpts .extraLabelsFromCtx {
112
92
l [label ] = resolve (r .Context ())
113
93
}
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 ()))
115
95
}
116
96
}
117
97
@@ -150,7 +130,7 @@ func InstrumentHandlerCounter(counter *prometheus.CounterVec, next http.Handler,
150
130
for label , resolve := range hOpts .extraLabelsFromCtx {
151
131
l [label ] = resolve (r .Context ())
152
132
}
153
- addWithExemplar ( counter .With (l ), 1 , hOpts .getExemplarFn (r .Context ()))
133
+ counter .With (l ).(prometheus. ExemplarAdder ). AddWithExemplar ( 1 , hOpts .getExemplarFn (r .Context ()))
154
134
}
155
135
}
156
136
@@ -161,7 +141,7 @@ func InstrumentHandlerCounter(counter *prometheus.CounterVec, next http.Handler,
161
141
for label , resolve := range hOpts .extraLabelsFromCtx {
162
142
l [label ] = resolve (r .Context ())
163
143
}
164
- addWithExemplar ( counter .With (l ), 1 , hOpts .getExemplarFn (r .Context ()))
144
+ counter .With (l ).(prometheus. ExemplarAdder ). AddWithExemplar ( 1 , hOpts .getExemplarFn (r .Context ()))
165
145
}
166
146
}
167
147
@@ -203,7 +183,7 @@ func InstrumentHandlerTimeToWriteHeader(obs prometheus.ObserverVec, next http.Ha
203
183
for label , resolve := range hOpts .extraLabelsFromCtx {
204
184
l [label ] = resolve (r .Context ())
205
185
}
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 ()))
207
187
})
208
188
next .ServeHTTP (d , r )
209
189
}
@@ -247,7 +227,7 @@ func InstrumentHandlerRequestSize(obs prometheus.ObserverVec, next http.Handler,
247
227
for label , resolve := range hOpts .extraLabelsFromCtx {
248
228
l [label ] = resolve (r .Context ())
249
229
}
250
- observeWithExemplar ( obs .With (l ), float64 (size ), hOpts .getExemplarFn (r .Context ()))
230
+ obs .With (l ).(prometheus. ExemplarObserver ). ObserveWithExemplar ( float64 (size ), hOpts .getExemplarFn (r .Context ()))
251
231
}
252
232
}
253
233
@@ -259,7 +239,7 @@ func InstrumentHandlerRequestSize(obs prometheus.ObserverVec, next http.Handler,
259
239
for label , resolve := range hOpts .extraLabelsFromCtx {
260
240
l [label ] = resolve (r .Context ())
261
241
}
262
- observeWithExemplar ( obs .With (l ), float64 (size ), hOpts .getExemplarFn (r .Context ()))
242
+ obs .With (l ).(prometheus. ExemplarObserver ). ObserveWithExemplar ( float64 (size ), hOpts .getExemplarFn (r .Context ()))
263
243
}
264
244
}
265
245
@@ -299,7 +279,7 @@ func InstrumentHandlerResponseSize(obs prometheus.ObserverVec, next http.Handler
299
279
for label , resolve := range hOpts .extraLabelsFromCtx {
300
280
l [label ] = resolve (r .Context ())
301
281
}
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 ()))
303
283
})
304
284
}
305
285
0 commit comments