@@ -87,31 +87,31 @@ func InstrumentHandlerDuration(obs prometheus.ObserverVec, next http.Handler, op
87
87
o .apply (hOpts )
88
88
}
89
89
90
- code , method := checkLabels (obs )
90
+ // Curry the observer with dynamic labels before checking the remaining labels.
91
+ code , method := checkLabels (obs .MustCurryWith (hOpts .emptyDynamicLabels ()))
91
92
92
93
if code {
93
94
return func (w http.ResponseWriter , r * http.Request ) {
94
95
now := time .Now ()
95
96
d := newDelegator (w , nil )
96
97
next .ServeHTTP (d , r )
97
98
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
- )
99
+ l := labels ( code , method , r . Method , d . Status (), hOpts . extraMethods ... )
100
+ for label , resolve := range hOpts .labelResolverFns {
101
+ l [ label ] = resolve ( r . Context ())
102
+ }
103
+ observeWithExemplar ( obs . With ( l ), time . Since ( now ). Seconds (), hOpts . getExemplarFn ( r . Context ()) )
103
104
}
104
105
}
105
106
106
107
return func (w http.ResponseWriter , r * http.Request ) {
107
108
now := time .Now ()
108
109
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
- )
110
+ l := labels (code , method , r .Method , 0 , hOpts .extraMethods ... )
111
+ for label , resolve := range hOpts .labelResolverFns {
112
+ l [label ] = resolve (r .Context ())
113
+ }
114
+ observeWithExemplar (obs .With (l ), time .Since (now ).Seconds (), hOpts .getExemplarFn (r .Context ()))
115
115
}
116
116
}
117
117
@@ -138,28 +138,30 @@ func InstrumentHandlerCounter(counter *prometheus.CounterVec, next http.Handler,
138
138
o .apply (hOpts )
139
139
}
140
140
141
- code , method := checkLabels (counter )
141
+ // Curry the counter with dynamic labels before checking the remaining labels.
142
+ code , method := checkLabels (counter .MustCurryWith (hOpts .emptyDynamicLabels ()))
142
143
143
144
if code {
144
145
return func (w http.ResponseWriter , r * http.Request ) {
145
146
d := newDelegator (w , nil )
146
147
next .ServeHTTP (d , r )
147
148
148
- addWithExemplar (
149
- counter . With ( labels ( code , method , r . Method , d . Status (), hOpts .extraMethods ... )),
150
- 1 ,
151
- hOpts . getExemplarFn ( r . Context ()),
152
- )
149
+ l := labels ( code , method , r . Method , d . Status (), hOpts . extraMethods ... )
150
+ for label , resolve := range hOpts .labelResolverFns {
151
+ l [ label ] = resolve ( r . Context ())
152
+ }
153
+ addWithExemplar ( counter . With ( l ), 1 , hOpts . getExemplarFn ( r . Context ()) )
153
154
}
154
155
}
155
156
156
157
return func (w http.ResponseWriter , r * http.Request ) {
157
158
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
- )
159
+
160
+ l := labels (code , method , r .Method , 0 , hOpts .extraMethods ... )
161
+ for label , resolve := range hOpts .labelResolverFns {
162
+ l [label ] = resolve (r .Context ())
163
+ }
164
+ addWithExemplar (counter .With (l ), 1 , hOpts .getExemplarFn (r .Context ()))
163
165
}
164
166
}
165
167
@@ -191,16 +193,17 @@ func InstrumentHandlerTimeToWriteHeader(obs prometheus.ObserverVec, next http.Ha
191
193
o .apply (hOpts )
192
194
}
193
195
194
- code , method := checkLabels (obs )
196
+ // Curry the observer with dynamic labels before checking the remaining labels.
197
+ code , method := checkLabels (obs .MustCurryWith (hOpts .emptyDynamicLabels ()))
195
198
196
199
return func (w http.ResponseWriter , r * http.Request ) {
197
200
now := time .Now ()
198
201
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
- )
202
+ l := labels ( code , method , r . Method , status , hOpts . extraMethods ... )
203
+ for label , resolve := range hOpts .labelResolverFns {
204
+ l [ label ] = resolve ( r . Context ())
205
+ }
206
+ observeWithExemplar ( obs . With ( l ), time . Since ( now ). Seconds (), hOpts . getExemplarFn ( r . Context ()) )
204
207
})
205
208
next .ServeHTTP (d , r )
206
209
}
@@ -231,28 +234,32 @@ func InstrumentHandlerRequestSize(obs prometheus.ObserverVec, next http.Handler,
231
234
o .apply (hOpts )
232
235
}
233
236
234
- code , method := checkLabels (obs )
237
+ // Curry the observer with dynamic labels before checking the remaining labels.
238
+ code , method := checkLabels (obs .MustCurryWith (hOpts .emptyDynamicLabels ()))
239
+
235
240
if code {
236
241
return func (w http.ResponseWriter , r * http.Request ) {
237
242
d := newDelegator (w , nil )
238
243
next .ServeHTTP (d , r )
239
244
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
- )
245
+
246
+ l := labels (code , method , r .Method , d .Status (), hOpts .extraMethods ... )
247
+ for label , resolve := range hOpts .labelResolverFns {
248
+ l [label ] = resolve (r .Context ())
249
+ }
250
+ observeWithExemplar (obs .With (l ), float64 (size ), hOpts .getExemplarFn (r .Context ()))
245
251
}
246
252
}
247
253
248
254
return func (w http.ResponseWriter , r * http.Request ) {
249
255
next .ServeHTTP (w , r )
250
256
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
- )
257
+
258
+ l := labels (code , method , r .Method , 0 , hOpts .extraMethods ... )
259
+ for label , resolve := range hOpts .labelResolverFns {
260
+ l [label ] = resolve (r .Context ())
261
+ }
262
+ observeWithExemplar (obs .With (l ), float64 (size ), hOpts .getExemplarFn (r .Context ()))
256
263
}
257
264
}
258
265
@@ -281,16 +288,18 @@ func InstrumentHandlerResponseSize(obs prometheus.ObserverVec, next http.Handler
281
288
o .apply (hOpts )
282
289
}
283
290
284
- code , method := checkLabels (obs )
291
+ // Curry the observer with dynamic labels before checking the remaining labels.
292
+ code , method := checkLabels (obs .MustCurryWith (hOpts .emptyDynamicLabels ()))
285
293
286
294
return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
287
295
d := newDelegator (w , nil )
288
296
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
- )
297
+
298
+ l := labels (code , method , r .Method , d .Status (), hOpts .extraMethods ... )
299
+ for label , resolve := range hOpts .labelResolverFns {
300
+ l [label ] = resolve (r .Context ())
301
+ }
302
+ observeWithExemplar (obs .With (l ), float64 (d .Written ()), hOpts .getExemplarFn (r .Context ()))
294
303
})
295
304
}
296
305
0 commit comments