Skip to content

Commit 76e46dd

Browse files
committed
drop use of deprecated otelhttp.WithMetricAttributesFn
1 parent 0b0ca89 commit 76e46dd

1 file changed

Lines changed: 14 additions & 10 deletions

File tree

webhook/webhook.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import (
3737
"knative.dev/pkg/observability/semconv"
3838

3939
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
40-
"go.opentelemetry.io/otel/attribute"
4140
"go.opentelemetry.io/otel/metric"
4241
"go.opentelemetry.io/otel/propagation"
4342
"go.opentelemetry.io/otel/trace"
@@ -313,19 +312,11 @@ func (wh *Webhook) Run(stop <-chan struct{}) error {
313312
}
314313

315314
otelHandler := otelhttp.NewHandler(
316-
drainer,
315+
&routeLabeler{next: drainer},
317316
wh.Options.ServiceName, // Note this service is k8s service name
318317
otelhttp.WithMeterProvider(wh.Options.MeterProvider),
319318
otelhttp.WithTracerProvider(wh.Options.TracerProvider),
320319
otelhttp.WithPropagators(wh.Options.TextMapPropagator),
321-
otelhttp.WithMetricAttributesFn(func(r *http.Request) []attribute.KeyValue {
322-
if r.URL.Path == "" {
323-
return nil
324-
}
325-
return []attribute.KeyValue{
326-
semconv.HTTPRoute(r.URL.Path),
327-
}
328-
}),
329320
otelhttp.WithFilter(func(r *http.Request) bool {
330321
// Don't trace kubelet probes
331322
return !network.IsKubeletProbe(r)
@@ -406,3 +397,16 @@ func (wh *Webhook) ServeHTTP(w http.ResponseWriter, r *http.Request) {
406397

407398
wh.mux.ServeHTTP(w, r)
408399
}
400+
401+
type routeLabeler struct {
402+
next http.Handler
403+
}
404+
405+
func (rl *routeLabeler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
406+
if r.URL.Path != "" {
407+
labeler, _ := otelhttp.LabelerFromContext(r.Context())
408+
labeler.Add(semconv.HTTPRoute(r.URL.Path))
409+
}
410+
411+
rl.next.ServeHTTP(w, r)
412+
}

0 commit comments

Comments
 (0)