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.


To install otelslog instrumentation:

go get

To configure slog with Otel handler:

import ""



Here is how you configure slog and OpenTelemetry:

package main

import (

	slogotel ""

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

	defer uptrace.Shutdown(ctx)

	// Configure slog with Otel handler.

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

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

	slog.ErrorContext(ctx, "Hello world!", "locale", "en_US")

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: