@@ -87,31 +87,36 @@ func InstrumentHandlerDuration(obs prometheus.ObserverVec, next http.Handler, op
87
87
o .apply (hOpts )
88
88
}
89
89
90
- code , method := checkLabels (obs )
90
+ dynamicLabels := prometheus.Labels {}
91
+ for label := range hOpts .labelResolverFns {
92
+ dynamicLabels [label ] = "dummy"
93
+ }
94
+
95
+ // Curry the observer with dynamic labels before checking the remaining labels
96
+ code , method := checkLabels (obs .MustCurryWith (dynamicLabels ))
91
97
92
98
if code {
93
99
return func (w http.ResponseWriter , r * http.Request ) {
94
100
now := time .Now ()
95
101
d := newDelegator (w , nil )
96
102
next .ServeHTTP (d , r )
97
103
98
- observeWithExemplar (
99
- obs . With ( labels ( code , method , r . Method , d . Status (), hOpts .extraMethods ... )),
100
- time . Since ( now ). Seconds (),
101
- hOpts . getExemplarFn ( r . Context ()),
102
- )
104
+ l := labels ( code , method , r . Method , d . Status (), hOpts . extraMethods ... )
105
+ for label , resolve := range hOpts .labelResolverFns {
106
+ l [ label ] = resolve ( r , nil )
107
+ }
108
+ observeWithExemplar ( obs . With ( l ), time . Since ( now ). Seconds (), hOpts . getExemplarFn ( r . Context ()) )
103
109
}
104
110
}
105
111
106
112
return func (w http.ResponseWriter , r * http.Request ) {
107
113
now := time .Now ()
108
114
next .ServeHTTP (w , r )
109
-
110
- observeWithExemplar (
111
- obs .With (labels (code , method , r .Method , 0 , hOpts .extraMethods ... )),
112
- time .Since (now ).Seconds (),
113
- hOpts .getExemplarFn (r .Context ()),
114
- )
115
+ l := labels (code , method , r .Method , 0 , hOpts .extraMethods ... )
116
+ for label , resolve := range hOpts .labelResolverFns {
117
+ l [label ] = resolve (r , nil )
118
+ }
119
+ observeWithExemplar (obs .With (l ), time .Since (now ).Seconds (), hOpts .getExemplarFn (r .Context ()))
115
120
}
116
121
}
117
122
@@ -138,28 +143,35 @@ func InstrumentHandlerCounter(counter *prometheus.CounterVec, next http.Handler,
138
143
o .apply (hOpts )
139
144
}
140
145
141
- code , method := checkLabels (counter )
146
+ dynamicLabels := prometheus.Labels {}
147
+ for label := range hOpts .labelResolverFns {
148
+ dynamicLabels [label ] = "dummy"
149
+ }
150
+
151
+ // Curry the counter with dynamic labels before checking the remaining labels
152
+ code , method := checkLabels (counter .MustCurryWith (dynamicLabels ))
142
153
143
154
if code {
144
155
return func (w http.ResponseWriter , r * http.Request ) {
145
156
d := newDelegator (w , nil )
146
157
next .ServeHTTP (d , r )
147
158
148
- addWithExemplar (
149
- counter . With ( labels ( code , method , r . Method , d . Status (), hOpts .extraMethods ... )),
150
- 1 ,
151
- hOpts . getExemplarFn ( r . Context ()),
152
- )
159
+ l := labels ( code , method , r . Method , d . Status (), hOpts . extraMethods ... )
160
+ for label , resolve := range hOpts .labelResolverFns {
161
+ l [ label ] = resolve ( r , nil )
162
+ }
163
+ addWithExemplar ( counter . With ( l ), 1 , hOpts . getExemplarFn ( r . Context ()) )
153
164
}
154
165
}
155
166
156
167
return func (w http.ResponseWriter , r * http.Request ) {
157
168
next .ServeHTTP (w , r )
158
- addWithExemplar (
159
- counter .With (labels (code , method , r .Method , 0 , hOpts .extraMethods ... )),
160
- 1 ,
161
- hOpts .getExemplarFn (r .Context ()),
162
- )
169
+
170
+ l := labels (code , method , r .Method , 0 , hOpts .extraMethods ... )
171
+ for label , resolve := range hOpts .labelResolverFns {
172
+ l [label ] = resolve (r , nil )
173
+ }
174
+ addWithExemplar (counter .With (l ), 1 , hOpts .getExemplarFn (r .Context ()))
163
175
}
164
176
}
165
177
@@ -191,16 +203,22 @@ func InstrumentHandlerTimeToWriteHeader(obs prometheus.ObserverVec, next http.Ha
191
203
o .apply (hOpts )
192
204
}
193
205
194
- code , method := checkLabels (obs )
206
+ dynamicLabels := prometheus.Labels {}
207
+ for label := range hOpts .labelResolverFns {
208
+ dynamicLabels [label ] = "dummy"
209
+ }
210
+
211
+ // Curry the observer with dynamic labels before checking the remaining labels
212
+ code , method := checkLabels (obs .MustCurryWith (dynamicLabels ))
195
213
196
214
return func (w http.ResponseWriter , r * http.Request ) {
197
215
now := time .Now ()
198
216
d := newDelegator (w , func (status int ) {
199
- observeWithExemplar (
200
- obs . With ( labels ( code , method , r . Method , status , hOpts .extraMethods ... )),
201
- time . Since ( now ). Seconds (),
202
- hOpts . getExemplarFn ( r . Context ()),
203
- )
217
+ l := labels ( code , method , r . Method , status , hOpts . extraMethods ... )
218
+ for label , resolve := range hOpts .labelResolverFns {
219
+ l [ label ] = resolve ( r , nil )
220
+ }
221
+ observeWithExemplar ( obs . With ( l ), time . Since ( now ). Seconds (), hOpts . getExemplarFn ( r . Context ()) )
204
222
})
205
223
next .ServeHTTP (d , r )
206
224
}
@@ -231,28 +249,37 @@ func InstrumentHandlerRequestSize(obs prometheus.ObserverVec, next http.Handler,
231
249
o .apply (hOpts )
232
250
}
233
251
234
- code , method := checkLabels (obs )
252
+ dynamicLabels := prometheus.Labels {}
253
+ for label := range hOpts .labelResolverFns {
254
+ dynamicLabels [label ] = "dummy"
255
+ }
256
+
257
+ // Curry the observer with dynamic labels before checking the remaining labels
258
+ code , method := checkLabels (obs .MustCurryWith (dynamicLabels ))
259
+
235
260
if code {
236
261
return func (w http.ResponseWriter , r * http.Request ) {
237
262
d := newDelegator (w , nil )
238
263
next .ServeHTTP (d , r )
239
264
size := computeApproximateRequestSize (r )
240
- observeWithExemplar (
241
- obs .With (labels (code , method , r .Method , d .Status (), hOpts .extraMethods ... )),
242
- float64 (size ),
243
- hOpts .getExemplarFn (r .Context ()),
244
- )
265
+
266
+ l := labels (code , method , r .Method , d .Status (), hOpts .extraMethods ... )
267
+ for label , resolve := range hOpts .labelResolverFns {
268
+ l [label ] = resolve (r , nil )
269
+ }
270
+ observeWithExemplar (obs .With (l ), float64 (size ), hOpts .getExemplarFn (r .Context ()))
245
271
}
246
272
}
247
273
248
274
return func (w http.ResponseWriter , r * http.Request ) {
249
275
next .ServeHTTP (w , r )
250
276
size := computeApproximateRequestSize (r )
251
- observeWithExemplar (
252
- obs .With (labels (code , method , r .Method , 0 , hOpts .extraMethods ... )),
253
- float64 (size ),
254
- hOpts .getExemplarFn (r .Context ()),
255
- )
277
+
278
+ l := labels (code , method , r .Method , 0 , hOpts .extraMethods ... )
279
+ for label , resolve := range hOpts .labelResolverFns {
280
+ l [label ] = resolve (r , nil )
281
+ }
282
+ observeWithExemplar (obs .With (l ), float64 (size ), hOpts .getExemplarFn (r .Context ()))
256
283
}
257
284
}
258
285
@@ -281,16 +308,23 @@ func InstrumentHandlerResponseSize(obs prometheus.ObserverVec, next http.Handler
281
308
o .apply (hOpts )
282
309
}
283
310
284
- code , method := checkLabels (obs )
311
+ dynamicLabels := prometheus.Labels {}
312
+ for label := range hOpts .labelResolverFns {
313
+ dynamicLabels [label ] = "dummy"
314
+ }
315
+
316
+ // Curry the observer with dynamic labels before checking the remaining labels
317
+ code , method := checkLabels (obs .MustCurryWith (dynamicLabels ))
285
318
286
319
return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
287
320
d := newDelegator (w , nil )
288
321
next .ServeHTTP (d , r )
289
- observeWithExemplar (
290
- obs .With (labels (code , method , r .Method , d .Status (), hOpts .extraMethods ... )),
291
- float64 (d .Written ()),
292
- hOpts .getExemplarFn (r .Context ()),
293
- )
322
+
323
+ l := labels (code , method , r .Method , d .Status (), hOpts .extraMethods ... )
324
+ for label , resolve := range hOpts .labelResolverFns {
325
+ l [label ] = resolve (r , nil )
326
+ }
327
+ observeWithExemplar (obs .With (l ), float64 (d .Written ()), hOpts .getExemplarFn (r .Context ()))
294
328
})
295
329
}
296
330
0 commit comments