when recording the admission/conversion duration use the attributes from the labeler

This commit is contained in:
Dave Protasowski 2025-07-02 16:40:58 -04:00
parent 48df033c70
commit d9f7cd3c29
2 changed files with 11 additions and 18 deletions

View File

@ -33,7 +33,6 @@ import (
"knative.dev/pkg/logging/logkey" "knative.dev/pkg/logging/logkey"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric" "go.opentelemetry.io/otel/metric"
) )
@ -106,16 +105,14 @@ func admissionHandler(wh *Webhook, c AdmissionController, synced <-chan struct{}
labeler, _ := otelhttp.LabelerFromContext(r.Context()) labeler, _ := otelhttp.LabelerFromContext(r.Context())
attrs := []attribute.KeyValue{ labeler.Add(
AdmissionKind.With(review.Request.Kind.Kind), AdmissionKind.With(review.Request.Kind.Kind),
AdmissionGroup.With(review.Request.Kind.Group), AdmissionGroup.With(review.Request.Kind.Group),
AdmissionVersion.With(review.Request.Kind.Version), AdmissionVersion.With(review.Request.Kind.Version),
AdmissionOperation.With(string(review.Request.Operation)), AdmissionOperation.With(string(review.Request.Operation)),
AdmissionSubresource.With(review.Request.SubResource), AdmissionSubresource.With(review.Request.SubResource),
webhookTypeAttr, webhookTypeAttr,
} )
labeler.Add(attrs...)
logger = logger.With( logger = logger.With(
logkey.Kind, review.Request.Kind.String(), logkey.Kind, review.Request.Kind.String(),
@ -146,12 +143,11 @@ func admissionHandler(wh *Webhook, c AdmissionController, synced <-chan struct{}
} }
allowedAttr := AdmissionAllowed.With(reviewResponse.Allowed) allowedAttr := AdmissionAllowed.With(reviewResponse.Allowed)
attrs = append(attrs, allowedAttr)
labeler.Add(allowedAttr) labeler.Add(allowedAttr)
wh.metrics.recordHandlerDuration(ctx, wh.metrics.recordHandlerDuration(ctx,
time.Since(ttStart), time.Since(ttStart),
metric.WithAttributes(attrs...), metric.WithAttributes(labeler.Get()...),
) )
if !reviewResponse.Allowed || reviewResponse.PatchType != nil || response.Response == nil { if !reviewResponse.Allowed || reviewResponse.PatchType != nil || response.Response == nil {

View File

@ -54,10 +54,11 @@ func conversionHandler(wh *Webhook, c ConversionController) http.HandlerFunc {
return return
} }
versionAttr := ConversionDesiredAPIVersion.With(review.Request.DesiredAPIVersion)
labeler, _ := otelhttp.LabelerFromContext(r.Context()) labeler, _ := otelhttp.LabelerFromContext(r.Context())
labeler.Add(webhookTypeAttr, versionAttr) labeler.Add(
webhookTypeAttr,
ConversionDesiredAPIVersion.With(review.Request.DesiredAPIVersion),
)
logger = logger.With( logger = logger.With(
zap.String("uid", string(review.Request.UID)), 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), 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( labeler.Add(
ConversionResultStatus.With(strings.ToLower(response.Response.Result.Status)), 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 { if err := json.NewEncoder(w).Encode(response); err != nil {
http.Error(w, fmt.Sprint("could not encode response:", err), http.StatusInternalServerError) http.Error(w, fmt.Sprint("could not encode response:", err), http.StatusInternalServerError)
return return