@@ -87,31 +87,31 @@ func InstrumentHandlerDuration(obs prometheus.ObserverVec, next http.Handler, op
8787 o .apply (hOpts )
8888 }
8989
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 ()))
9192
9293 if code {
9394 return func (w http.ResponseWriter , r * http.Request ) {
9495 now := time .Now ()
9596 d := newDelegator (w , nil )
9697 next .ServeHTTP (d , r )
9798
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 ()) )
103104 }
104105 }
105106
106107 return func (w http.ResponseWriter , r * http.Request ) {
107108 now := time .Now ()
108109 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 ()))
115115 }
116116}
117117
@@ -138,28 +138,30 @@ func InstrumentHandlerCounter(counter *prometheus.CounterVec, next http.Handler,
138138 o .apply (hOpts )
139139 }
140140
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 ()))
142143
143144 if code {
144145 return func (w http.ResponseWriter , r * http.Request ) {
145146 d := newDelegator (w , nil )
146147 next .ServeHTTP (d , r )
147148
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 ()) )
153154 }
154155 }
155156
156157 return func (w http.ResponseWriter , r * http.Request ) {
157158 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 ()))
163165 }
164166}
165167
@@ -191,16 +193,17 @@ func InstrumentHandlerTimeToWriteHeader(obs prometheus.ObserverVec, next http.Ha
191193 o .apply (hOpts )
192194 }
193195
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 ()))
195198
196199 return func (w http.ResponseWriter , r * http.Request ) {
197200 now := time .Now ()
198201 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 ()) )
204207 })
205208 next .ServeHTTP (d , r )
206209 }
@@ -231,28 +234,32 @@ func InstrumentHandlerRequestSize(obs prometheus.ObserverVec, next http.Handler,
231234 o .apply (hOpts )
232235 }
233236
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+
235240 if code {
236241 return func (w http.ResponseWriter , r * http.Request ) {
237242 d := newDelegator (w , nil )
238243 next .ServeHTTP (d , r )
239244 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 ()))
245251 }
246252 }
247253
248254 return func (w http.ResponseWriter , r * http.Request ) {
249255 next .ServeHTTP (w , r )
250256 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 ()))
256263 }
257264}
258265
@@ -281,16 +288,18 @@ func InstrumentHandlerResponseSize(obs prometheus.ObserverVec, next http.Handler
281288 o .apply (hOpts )
282289 }
283290
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 ()))
285293
286294 return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
287295 d := newDelegator (w , nil )
288296 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 ()))
294303 })
295304}
296305
0 commit comments