From 835f5551b7813460b5e9f91482c50fcb94f4bd76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charles-Edouard=20Br=C3=A9t=C3=A9ch=C3=A9?= Date: Thu, 26 Jan 2023 10:39:52 +0100 Subject: [PATCH] fix: tracing attributes length and tracer name (#6117) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Charles-Edouard Brétéché Signed-off-by: Charles-Edouard Brétéché --- cmd/cleanup-controller/main.go | 2 +- cmd/initContainer/main.go | 2 +- cmd/internal/setup.go | 4 +- cmd/internal/tracing.go | 5 ++- cmd/kyverno/main.go | 2 +- cmd/reports-controller/main.go | 2 +- cmd/updaterequest-controller/main.go | 2 +- pkg/tracing/attributes.go | 12 +++++- pkg/tracing/config.go | 4 +- pkg/webhooks/handlers/trace.go | 56 ++++++++++++++-------------- 10 files changed, 51 insertions(+), 40 deletions(-) diff --git a/cmd/cleanup-controller/main.go b/cmd/cleanup-controller/main.go index 27abd745a9..e0c47a8f6a 100644 --- a/cmd/cleanup-controller/main.go +++ b/cmd/cleanup-controller/main.go @@ -75,7 +75,7 @@ func main() { // setup signals // setup maxprocs // setup metrics - ctx, logger, metricsConfig, sdown := internal.Setup() + ctx, logger, metricsConfig, sdown := internal.Setup("kyverno-cleanup-controller") defer sdown() // create instrumented clients kubeClient := internal.CreateKubernetesClient(logger, kubeclient.WithMetrics(metricsConfig, metrics.KubeClient), kubeclient.WithTracing()) diff --git a/cmd/initContainer/main.go b/cmd/initContainer/main.go index 65e5f81aae..4d7467cfb7 100644 --- a/cmd/initContainer/main.go +++ b/cmd/initContainer/main.go @@ -40,7 +40,7 @@ func main() { // start profiling // setup signals // setup maxprocs - ctx, logger, _, sdown := internal.Setup() + ctx, logger, _, sdown := internal.Setup("kyverno-init-controller") defer sdown() // create clients kubeClient := internal.CreateKubernetesClient(logger) diff --git a/cmd/internal/setup.go b/cmd/internal/setup.go index 63dc47e6bf..bd3adad9d0 100644 --- a/cmd/internal/setup.go +++ b/cmd/internal/setup.go @@ -18,7 +18,7 @@ func shutdown(logger logr.Logger, sdowns ...context.CancelFunc) context.CancelFu } } -func Setup() (context.Context, logr.Logger, metrics.MetricsConfigManager, context.CancelFunc) { +func Setup(name string) (context.Context, logr.Logger, metrics.MetricsConfigManager, context.CancelFunc) { logger := SetupLogger() ShowVersion(logger) sdownMaxProcs := SetupMaxProcs(logger) @@ -26,6 +26,6 @@ func Setup() (context.Context, logr.Logger, metrics.MetricsConfigManager, contex client := CreateKubernetesClient(logger) ctx, sdownSignals := SetupSignals(logger) metricsManager, sdownMetrics := SetupMetrics(ctx, logger, client) - sdownTracing := SetupTracing(logger, client) + sdownTracing := SetupTracing(logger, name, client) return ctx, logger, metricsManager, shutdown(logger.WithName("shutdown"), sdownMaxProcs, sdownMetrics, sdownTracing, sdownSignals) } diff --git a/cmd/internal/tracing.go b/cmd/internal/tracing.go index c99404090f..f0205ae1a7 100644 --- a/cmd/internal/tracing.go +++ b/cmd/internal/tracing.go @@ -9,12 +9,13 @@ import ( "k8s.io/client-go/kubernetes" ) -func SetupTracing(logger logr.Logger, kubeClient kubernetes.Interface) context.CancelFunc { - logger = logger.WithName("tracing").WithValues("enabled", tracingEnabled, "address", tracingAddress, "port", tracingPort, "creds", tracingCreds) +func SetupTracing(logger logr.Logger, name string, kubeClient kubernetes.Interface) context.CancelFunc { + logger = logger.WithName("tracing").WithValues("enabled", tracingEnabled, "name", name, "address", tracingAddress, "port", tracingPort, "creds", tracingCreds) if tracingEnabled { logger.Info("setup tracing...") shutdown, err := tracing.NewTraceConfig( logger, + name, net.JoinHostPort(tracingAddress, tracingPort), tracingCreds, kubeClient, diff --git a/cmd/kyverno/main.go b/cmd/kyverno/main.go index 9697874ed4..cf1699a22d 100644 --- a/cmd/kyverno/main.go +++ b/cmd/kyverno/main.go @@ -298,7 +298,7 @@ func main() { // setup signals // setup maxprocs // setup metrics - signalCtx, logger, metricsConfig, sdown := internal.Setup() + signalCtx, logger, metricsConfig, sdown := internal.Setup("kyverno-admission-controller") defer sdown() // show version showWarnings(logger) diff --git a/cmd/reports-controller/main.go b/cmd/reports-controller/main.go index c5353a2ec9..38d882f5e6 100644 --- a/cmd/reports-controller/main.go +++ b/cmd/reports-controller/main.go @@ -228,7 +228,7 @@ func main() { // setup signals // setup maxprocs // setup metrics - ctx, logger, metricsConfig, sdown := internal.Setup() + ctx, logger, metricsConfig, sdown := internal.Setup("kyverno-reports-controller") defer sdown() // create instrumented clients kubeClient := internal.CreateKubernetesClient(logger, kubeclient.WithMetrics(metricsConfig, metrics.KubeClient), kubeclient.WithTracing()) diff --git a/cmd/updaterequest-controller/main.go b/cmd/updaterequest-controller/main.go index 53c61cdbd3..ee884d51f3 100644 --- a/cmd/updaterequest-controller/main.go +++ b/cmd/updaterequest-controller/main.go @@ -155,7 +155,7 @@ func main() { // setup signals // setup maxprocs // setup metrics - signalCtx, logger, metricsConfig, sdown := internal.Setup() + signalCtx, logger, metricsConfig, sdown := internal.Setup("kyverno-updaterequest-controller") defer sdown() // create instrumented clients kubeClient := internal.CreateKubernetesClient(logger, kubeclient.WithMetrics(metricsConfig, metrics.KubeClient), kubeclient.WithTracing()) diff --git a/pkg/tracing/attributes.go b/pkg/tracing/attributes.go index 9e733f3b20..f86fe46e6e 100644 --- a/pkg/tracing/attributes.go +++ b/pkg/tracing/attributes.go @@ -5,7 +5,7 @@ import ( ) const ( - TracerName = "kyverno" + limit = 256 // engine attributes PolicyGroupKey = attribute.Key("kyverno.policy.group") PolicyVersionKey = attribute.Key("kyverno.policy.version") @@ -58,3 +58,13 @@ const ( KubeClientOperationKey = attribute.Key("kube.client.operation") KubeClientNamespaceKey = attribute.Key("kube.client.namespace") ) + +// StringValue truncates the input value if its size is above the limit. +// Some backends impose a limit on the size of a tag value. +func StringValue(value string) string { + if len(value) > limit { + value = value[:limit-3] + value += "..." + } + return value +} diff --git a/pkg/tracing/config.go b/pkg/tracing/config.go index 22fa9d7e44..4ba3ee9c87 100644 --- a/pkg/tracing/config.go +++ b/pkg/tracing/config.go @@ -18,7 +18,7 @@ import ( ) // NewTraceConfig generates the initial tracing configuration with 'address' as the endpoint to connect to the Opentelemetry Collector -func NewTraceConfig(log logr.Logger, address, certs string, kubeClient kubernetes.Interface) (func(), error) { +func NewTraceConfig(log logr.Logger, tracerName, address, certs string, kubeClient kubernetes.Interface) (func(), error) { ctx := context.Background() var client otlptrace.Client if certs != "" { @@ -47,7 +47,7 @@ func NewTraceConfig(log logr.Logger, address, certs string, kubeClient kubernete resource.Default(), resource.NewWithAttributes( semconv.SchemaURL, - semconv.ServiceNameKey.String(TracerName), + semconv.ServiceNameKey.String(tracerName), semconv.ServiceVersionKey.String(version.BuildVersion), ), ) diff --git a/pkg/webhooks/handlers/trace.go b/pkg/webhooks/handlers/trace.go index 472dd95c36..d1968c47a4 100644 --- a/pkg/webhooks/handlers/trace.go +++ b/pkg/webhooks/handlers/trace.go @@ -24,9 +24,9 @@ func (inner HttpHandler) WithTrace(name string) HttpHandler { }, trace.WithAttributes( semconv.HTTPRequestContentLengthKey.Int64(request.ContentLength), - semconv.HTTPHostKey.String(request.Host), - semconv.HTTPMethodKey.String(request.Method), - semconv.HTTPURLKey.String(request.RequestURI), + semconv.HTTPHostKey.String(tracing.StringValue(request.Host)), + semconv.HTTPMethodKey.String(tracing.StringValue(request.Method)), + semconv.HTTPURLKey.String(tracing.StringValue(request.RequestURI)), ), trace.WithSpanKind(trace.SpanKindServer), ) @@ -43,48 +43,48 @@ func (inner AdmissionHandler) WithTrace(name string) AdmissionHandler { response := inner(ctx, logger, request, startTime) if response != nil { span.SetAttributes( - tracing.ResponseUidKey.String(string(response.UID)), + tracing.ResponseUidKey.String(tracing.StringValue(string(response.UID))), tracing.ResponseAllowedKey.Bool(response.Allowed), tracing.ResponseWarningsKey.StringSlice(response.Warnings), ) if response.Result != nil { span.SetAttributes( - tracing.ResponseResultStatusKey.String(response.Result.Status), - tracing.ResponseResultMessageKey.String(response.Result.Message), - tracing.ResponseResultReasonKey.String(string(response.Result.Reason)), + tracing.ResponseResultStatusKey.String(tracing.StringValue(response.Result.Status)), + tracing.ResponseResultMessageKey.String(tracing.StringValue(response.Result.Message)), + tracing.ResponseResultReasonKey.String(tracing.StringValue(string(response.Result.Reason))), tracing.ResponseResultCodeKey.Int(int(response.Result.Code)), ) } if response.PatchType != nil { span.SetAttributes( - tracing.ResponsePatchTypeKey.String(string(*response.PatchType)), + tracing.ResponsePatchTypeKey.String(tracing.StringValue(string(*response.PatchType))), ) } } return response }, trace.WithAttributes( - tracing.RequestNameKey.String(request.Name), - tracing.RequestNamespaceKey.String(request.Namespace), - tracing.RequestUidKey.String(string(request.UID)), - tracing.RequestOperationKey.String(string(request.Operation)), + tracing.RequestNameKey.String(tracing.StringValue(request.Name)), + tracing.RequestNamespaceKey.String(tracing.StringValue(request.Namespace)), + tracing.RequestUidKey.String(tracing.StringValue(string(request.UID))), + tracing.RequestOperationKey.String(tracing.StringValue(string(request.Operation))), tracing.RequestDryRunKey.Bool(request.DryRun != nil && *request.DryRun), - tracing.RequestKindGroupKey.String(request.Kind.Group), - tracing.RequestKindVersionKey.String(request.Kind.Version), - tracing.RequestKindKindKey.String(request.Kind.Kind), - tracing.RequestSubResourceKey.String(request.SubResource), - tracing.RequestRequestKindGroupKey.String(request.RequestKind.Group), - tracing.RequestRequestKindVersionKey.String(request.RequestKind.Version), - tracing.RequestRequestKindKindKey.String(request.RequestKind.Kind), - tracing.RequestRequestSubResourceKey.String(request.RequestSubResource), - tracing.RequestResourceGroupKey.String(request.Resource.Group), - tracing.RequestResourceVersionKey.String(request.Resource.Version), - tracing.RequestResourceResourceKey.String(request.Resource.Resource), - tracing.RequestRequestResourceGroupKey.String(request.RequestResource.Group), - tracing.RequestRequestResourceVersionKey.String(request.RequestResource.Version), - tracing.RequestRequestResourceResourceKey.String(request.RequestResource.Resource), - tracing.RequestUserNameKey.String(request.UserInfo.Username), - tracing.RequestUserUidKey.String(request.UserInfo.UID), + tracing.RequestKindGroupKey.String(tracing.StringValue(request.Kind.Group)), + tracing.RequestKindVersionKey.String(tracing.StringValue(request.Kind.Version)), + tracing.RequestKindKindKey.String(tracing.StringValue(request.Kind.Kind)), + tracing.RequestSubResourceKey.String(tracing.StringValue(request.SubResource)), + tracing.RequestRequestKindGroupKey.String(tracing.StringValue(request.RequestKind.Group)), + tracing.RequestRequestKindVersionKey.String(tracing.StringValue(request.RequestKind.Version)), + tracing.RequestRequestKindKindKey.String(tracing.StringValue(request.RequestKind.Kind)), + tracing.RequestRequestSubResourceKey.String(tracing.StringValue(request.RequestSubResource)), + tracing.RequestResourceGroupKey.String(tracing.StringValue(request.Resource.Group)), + tracing.RequestResourceVersionKey.String(tracing.StringValue(request.Resource.Version)), + tracing.RequestResourceResourceKey.String(tracing.StringValue(request.Resource.Resource)), + tracing.RequestRequestResourceGroupKey.String(tracing.StringValue(request.RequestResource.Group)), + tracing.RequestRequestResourceVersionKey.String(tracing.StringValue(request.RequestResource.Version)), + tracing.RequestRequestResourceResourceKey.String(tracing.StringValue(request.RequestResource.Resource)), + tracing.RequestUserNameKey.String(tracing.StringValue(request.UserInfo.Username)), + tracing.RequestUserUidKey.String(tracing.StringValue(request.UserInfo.UID)), tracing.RequestUserGroupsKey.StringSlice(request.UserInfo.Groups), ), )