PitchHut logo
otel-logger-middleware
Seamlessly inject OpenTelemetry context into your logs.
Pitch

otel-logger-middleware is a Go library that enhances log entries by integrating OpenTelemetry trace context. It automatically adds Trace ID, Span ID, and Trace Flags to your logs, enabling better observability in distributed systems. This middleware ensures logs can be easily correlated with traces, simplifying debugging and monitoring.

Description

otel-logger-middleware is a Go library designed to enhance logging capabilities by automatically injecting OpenTelemetry trace context, including Trace ID, Span ID, and Trace Flags, into logs generated by the log/slog framework.

Significance of Trace Context in Logging

In modern distributed systems, establishing a connection between logs and traces is essential for effective observability. With each log message, it becomes imperative to identify the specific request (trace) that generated it. Similarly, during trace analysis, having access to relevant logs is vital for understanding the operational context. The otel-logger-middleware addresses this challenge by ensuring that slog.Record automatically includes relevant identifiers (i.e., trace_id, span_id, and trace_flags) whenever a valid span exists in the context.

Implementation Example

Integrating this middleware into your logging setup is straightforward. Consider the following example, demonstrating how to apply the otel-logger-middleware to a base slog.Handler:

package main

import (
	"context"
	"log/slog"
	"os"

	otelLoggerMiddleware "github.com/gozeloglu/otel-logger-middleware"
	"go.opentelemetry.io/otel"
)

func main() {
	// Create a base JSON handler.
	baseHandler := slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
		Level: slog.LevelInfo,
	})

	// Wrap it with OtelLoggerMiddleware, selecting your preferred key naming convention:
	middleware := otelLoggerMiddleware.NewOtelLoggerMiddleware(baseHandler, otelLoggerMiddleware.SemConv)

	// Initialize the logger with the middleware.
	logger := slog.New(middleware)

	// Utilize the logger within a context containing an active span.
	ctx := context.Background()
	tracer := otel.Tracer("example-tracer")
	ctx, span := tracer.Start(ctx, "my-operation")
	defer span.End()

	// The log output now includes trace.id and span.id for correlation.
	logger.InfoContext(ctx, "This log is correlated with a trace")
}

Flexible Naming Conventions

The middleware accommodates various key naming conventions to ensure compatibility with different logging backends. Users can select from:

  • otelLoggerMiddleware.SemConv: Adheres to OpenTelemetry semantic conventions (e.g., trace.id, span.id).
  • otelLoggerMiddleware.SnakeCase: Implements snake_case (e.g., trace_id, span_id).
  • otelLoggerMiddleware.CamelCase: Adopts camelCase (e.g., traceId, spanId).
  • otelLoggerMiddleware.PascalCase: Applies PascalCase (e.g., TraceId, SpanId).

By incorporating otel-logger-middleware, developers can seamlessly enhance their logging mechanisms, improving observability and making it easier to trace issues in distributed applications.

0 comments

No comments yet.

Sign in to be the first to comment.