From 71a069dc38ee1239c39892a946ea7c04619557e5 Mon Sep 17 00:00:00 2001 From: gotgelf Date: Tue, 5 Mar 2024 07:17:50 +0100 Subject: [PATCH] Standardize OTEL error logging format to match application logs Signed-off-by: gotgelf --- tracing/loggerwriter.go | 21 +++++++++++++++++++++ tracing/tracing.go | 14 +------------- 2 files changed, 22 insertions(+), 13 deletions(-) create mode 100644 tracing/loggerwriter.go diff --git a/tracing/loggerwriter.go b/tracing/loggerwriter.go new file mode 100644 index 000000000..a0479426b --- /dev/null +++ b/tracing/loggerwriter.go @@ -0,0 +1,21 @@ +package tracing + +import "github.com/distribution/distribution/v3/internal/dcontext" + +// loggerWriter is a custom writer that implements the io.Writer interface. +// It is designed to redirect log messages to the Logger interface, specifically +// for use with OpenTelemetry's stdouttrace exporter. +type loggerWriter struct { + logger dcontext.Logger // Use the Logger interface +} + +// Write logs the data using the Debug level of the provided logger. +func (lw *loggerWriter) Write(p []byte) (n int, err error) { + lw.logger.Debug(string(p)) + return len(p), nil +} + +// Handle logs the error using the Error level of the provided logger. +func (lw *loggerWriter) Handle(err error) { + lw.logger.Error(err) +} diff --git a/tracing/tracing.go b/tracing/tracing.go index 899477ca7..dca9d584f 100644 --- a/tracing/tracing.go +++ b/tracing/tracing.go @@ -26,19 +26,6 @@ const ( AttributePrefix = "io.cncf.distribution." ) -// loggerWriter is a custom writer that implements the io.Writer interface. -// It is designed to redirect log messages to the Logger interface, specifically -// for use with OpenTelemetry's stdouttrace exporter. -type loggerWriter struct { - logger dcontext.Logger // Use the Logger interface -} - -// Write logs the data using the Debug level of the provided logger. -func (lw *loggerWriter) Write(p []byte) (n int, err error) { - lw.logger.Debug(string(p)) - return len(p), nil -} - // InitOpenTelemetry initializes OpenTelemetry for the application. This function sets up the // necessary components for collecting telemetry data, such as traces. func InitOpenTelemetry(ctx context.Context) error { @@ -71,6 +58,7 @@ func InitOpenTelemetry(ctx context.Context) error { sdktrace.WithSpanProcessor(sp), ) otel.SetTracerProvider(provider) + otel.SetErrorHandler(lw) pr := propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}) otel.SetTextMapPropagator(pr)