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 otelconf/x and otelslog bridge packages:
go get \
  go.opentelemetry.io/contrib/otelconf/x \
  go.opentelemetry.io/contrib/bridges/otelslog
2

Configure the Logger Provider

During application setup, initialize the SDK from the config file and set the global logger provider:
package main

import (
  "context"

  "go.opentelemetry.io/contrib/bridges/otelslog"
  otelconf "go.opentelemetry.io/contrib/otelconf/x"
  "go.opentelemetry.io/otel"
  "go.opentelemetry.io/otel/log/global"
)

func main() {
  // Set up the OpenTelemetry SDK from the config file
  sdk, err := otelconf.NewSDK()
  if err != nil {
    panic("failed to initialize OTel SDK")
  }
  defer sdk.Shutdown(context.Background())

  otel.SetTracerProvider(sdk.TracerProvider())
  otel.SetTextMapPropagator(sdk.Propagator())

  // Set the logger provider globally
  global.SetLoggerProvider(sdk.LoggerProvider())

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

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

Create a Config File

Create an otelconfig.yaml file to configure your exporter.To send logs to Honeycomb:
file_format: "1.0"

resource:
  attributes:
    - name: service.name
      value: ${OTEL_SERVICE_NAME:-my-service}

logger_provider:
  processors:
    - batch:
        exporter:
          otlp_http:
            endpoint: https://api.honeycomb.io
            headers:
              - name: x-honeycomb-team
                value: ${HONEYCOMB_API_KEY}
To send logs to an OpenTelemetry Collector instead:
file_format: "1.0"

resource:
  attributes:
    - name: service.name
      value: ${OTEL_SERVICE_NAME:-my-service}

logger_provider:
  processors:
    - batch:
        exporter:
          otlp_grpc:
            endpoint: http://my-collector:4317
When OTEL_CONFIG_FILE is set, the configuration file is the single source of truth for the SDK. Other OTEL_* environment variables are ignored by design, so set all SDK options in the YAML file. You can still reference environment variables from inside the YAML using ${VAR_NAME} substitution.
4

Run Your Application

Point the SDK at your configuration file using OTEL_CONFIG_FILE, then run your application:
OTEL_CONFIG_FILE=./otelconfig.yaml go run app.go