Collecting Golang Slog logs with OpenTelemetry

OpenTelemetry Slog is a handler for the new standard Golang structured loggingopen in new window library. The handler intercepts log messages and injects relevant OTel data into them. This data includes trace and span IDs that help correlate logs with specific requests or operations within a distributed system.

What is OpenTelemetry?

OpenTelemetryopen in new window is an open source and vendor-neutral API for OpenTelemetry tracingopen in new window, logsopen in new window, and metricsopen in new window.

OpenTelemetry provides a set of APIs, libraries, and instrumentation tools to instrument applications and collect data about their behavior and performance.

The goal of OpenTelemetry is to provide a standardized and vendor agnostic way to instrument applications for observability, for example, many open source tracing toolsopen in new window already support OpenTelemetry.

Otelslog

To install otelslog instrumentation:

go get go.opentelemetry.io/contrib/bridges/otelslog

To configure slog with Otel handler:

import "go.opentelemetry.io/contrib/bridges/otelslog"

slog.SetDefault(otelslog.NewLogger("app_or_package_name"))

Usage

Here is how you configure slog and OpenTelemetry:

package main

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

	slogotel "github.com/remychantenay/slog-otel"
	"github.com/uptrace/uptrace-go/uptrace"
	"go.opentelemetry.io/otel"
	"go.opentelemetry.io/otel/trace"
)

func main() {
	ctx := context.Background()

	uptrace.ConfigureOpentelemetry()
	defer uptrace.Shutdown(ctx)

	// Configure slog with Otel handler.
	slog.SetDefault(otelslog.NewLogger("app_or_package_name"))

	tracer := otel.Tracer("slog-example")

	ctx, span := tracer.Start(ctx, "operation-name")
	defer span.End()

	slog.ErrorContext(ctx, "Hello world!", "locale", "en_US")
	fmt.Println(uptrace.TraceURL(trace.SpanFromContext(ctx)))
}

OpenTelemetry Slog adheres to the OpenTelemetry standards, ensuring compatibility with various tracing and monitoring tools that support OpenTelemetry. This flexibility prevents vendor lock-in and simplifies integration with your existing monitoring infrastructure.

What is Uptrace?

Uptrace is a DataDog alternativeopen in new window that supports distributed tracing, metrics, and logs. You can use it to monitor applications and troubleshoot issues.

Uptrace Overview

Uptrace comes with an intuitive query builder, rich dashboards, alerting rules with notifications, and integrations for most languages and frameworks.

Uptrace can process billions of spans and metrics on a single server and allows you to monitor your applications at 10x lower cost.

In just a few minutes, you can try Uptrace by visiting the cloud demoopen in new window (no login required) or running it locally with Dockeropen in new window. The source code is available on GitHubopen in new window.

What's next?

OpenTelemetry Slog is a valuable tool for improving the observability of Golang applications that use Slog for logging. By correlating logs with OpenTelemetry trace context, you can gain a deeper understanding of your application's behavior and more effectively diagnose problems and optimize performance.

Last Updated: