From d9f7cd3c29b355a21f524bcb407cb2199ecd51b0 Mon Sep 17 00:00:00 2001 From: Dave Protasowski Date: Wed, 2 Jul 2025 16:40:58 -0400 Subject: [PATCH] when recording the admission/conversion duration use the attributes from the labeler --- webhook/admission.go | 10 +++------- webhook/conversion.go | 19 ++++++++----------- 2 files changed, 11 insertions(+), 18 deletions(-) diff --git a/webhook/admission.go b/webhook/admission.go index 590974629..7b3bce523 100644 --- a/webhook/admission.go +++ b/webhook/admission.go @@ -33,7 +33,6 @@ import ( "knative.dev/pkg/logging/logkey" "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" - "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/metric" ) @@ -106,16 +105,14 @@ func admissionHandler(wh *Webhook, c AdmissionController, synced <-chan struct{} labeler, _ := otelhttp.LabelerFromContext(r.Context()) - attrs := []attribute.KeyValue{ + labeler.Add( AdmissionKind.With(review.Request.Kind.Kind), AdmissionGroup.With(review.Request.Kind.Group), AdmissionVersion.With(review.Request.Kind.Version), AdmissionOperation.With(string(review.Request.Operation)), AdmissionSubresource.With(review.Request.SubResource), webhookTypeAttr, - } - - labeler.Add(attrs...) + ) logger = logger.With( logkey.Kind, review.Request.Kind.String(), @@ -146,12 +143,11 @@ func admissionHandler(wh *Webhook, c AdmissionController, synced <-chan struct{} } allowedAttr := AdmissionAllowed.With(reviewResponse.Allowed) - attrs = append(attrs, allowedAttr) labeler.Add(allowedAttr) wh.metrics.recordHandlerDuration(ctx, time.Since(ttStart), - metric.WithAttributes(attrs...), + metric.WithAttributes(labeler.Get()...), ) if !reviewResponse.Allowed || reviewResponse.PatchType != nil || response.Response == nil { diff --git a/webhook/conversion.go b/webhook/conversion.go index 8a6f152d5..7e7d47bd5 100644 --- a/webhook/conversion.go +++ b/webhook/conversion.go @@ -54,10 +54,11 @@ func conversionHandler(wh *Webhook, c ConversionController) http.HandlerFunc { return } - versionAttr := ConversionDesiredAPIVersion.With(review.Request.DesiredAPIVersion) - labeler, _ := otelhttp.LabelerFromContext(r.Context()) - labeler.Add(webhookTypeAttr, versionAttr) + labeler.Add( + webhookTypeAttr, + ConversionDesiredAPIVersion.With(review.Request.DesiredAPIVersion), + ) logger = logger.With( zap.String("uid", string(review.Request.UID)), @@ -76,18 +77,14 @@ func conversionHandler(wh *Webhook, c ConversionController) http.HandlerFunc { Response: c.Convert(ctx, review.Request), } - wh.metrics.recordHandlerDuration(ctx, time.Since(ttStart), - metric.WithAttributes( - versionAttr, - webhookTypeAttr, - ConversionResultStatus.With(strings.ToLower(response.Response.Result.Status)), - ), - ) - labeler.Add( ConversionResultStatus.With(strings.ToLower(response.Response.Result.Status)), ) + wh.metrics.recordHandlerDuration(ctx, time.Since(ttStart), + metric.WithAttributes(labeler.Get()...), + ) + if err := json.NewEncoder(w).Encode(response); err != nil { http.Error(w, fmt.Sprint("could not encode response:", err), http.StatusInternalServerError) return