1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-14 11:57:48 +00:00

fix: tracing attributes length and tracer name (#6117)

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
This commit is contained in:
Charles-Edouard Brétéché 2023-01-26 10:39:52 +01:00 committed by GitHub
parent 37338be747
commit 835f5551b7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 51 additions and 40 deletions

View file

@ -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())

View file

@ -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)

View file

@ -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)
}

View file

@ -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,

View file

@ -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)

View file

@ -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())

View file

@ -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())

View file

@ -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
}

View file

@ -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),
),
)

View file

@ -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),
),
)