Skip to main content
Logs support in the OpenTelemetry Go SDK is currently experimental and subject to change. To check the status for each language, refer to the OpenTelemetry status page.
This example shows how to configure the OpenTelemetry Go SDK with slog instrumentation to send logs from a Go application to Honeycomb or an OpenTelemetry Collector.
1

Acquire Dependencies

Install the OpenTelemetry OTLP log exporter and otelslog bridge packages:
go get \
  go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp \
  go.opentelemetry.io/contrib/bridges/otelslog
2

Configure the Logger Provider

During application setup, create and configure the logger provider:
package main

import (
  "context"

  "go.opentelemetry.io/contrib/bridges/otelslog"
  "go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp"
  "go.opentelemetry.io/otel/log/global"
  "go.opentelemetry.io/otel/sdk/log"
)

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

  // Create the OTLP log exporter that sends logs to the configured destination
  logExporter, err := otlploghttp.New(ctx)
  if err != nil {
    panic("failed to initialize exporter")
  }

  // Create the logger provider
  lp := log.NewLoggerProvider(
    log.WithProcessor(
      log.NewBatchProcessor(logExporter),
    ),
  )

  // Shut down the logger provider before exiting to export all pending logs
  defer lp.Shutdown(ctx)

  // Set the logger provider globally
  global.SetLoggerProvider(lp)

  // Create a new slog logger instance
  logger := otelslog.NewLogger()

  // Use the logger directly anywhere in your app
  logger.Debug("Something interesting happened")
}
3

Run Your Application

Set environment variables to configure the exporter and define your service name, then run your application.To send logs directly to Honeycomb:
OTEL_SERVICE_NAME="my-service" \
  OTEL_EXPORTER_OTLP_ENDPOINT="https://api.honeycomb.io" \
  OTEL_EXPORTER_OTLP_HEADERS="x-honeycomb-team=<your-api-key>" \
  go run app.go
To send logs to an OpenTelemetry Collector instead:
OTEL_SERVICE_NAME="my-service" \
  OTEL_EXPORTER_OTLP_ENDPOINT="my-collector:4317" \
  OTEL_EXPORTER_OTLP_INSECURE=true \
  go run app.go