This example uses the OpenTelemetry Go SDK and the slog instrumentation to create log entries, and send them to Honeycomb or an OpenTelemetry Collector.
First, install the OpenTelemetry API, SDK, and OTLP exporter packages:
go get \
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp \
go.opentelemetry.io/contrib/bridges/otelslog
Next, create and configure the logger providers during application setup:
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 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),
),
)
// Ensure the logger is shutdown before exiting so all pending logs are exported
defer lp.Shutdown(ctx)
// Set the logger provider globally
global.SetLoggerProvider(lp)
// Instantiate a new slog logger
logger := otelslog.NewLogger()
// You can use the logger directly anywhere in your app now
logger.Debug("Something interesting happened")
}
Finally, to send logs to Honeycomb, run your application with environment variables to set the service name and configure the exporter:
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
Alternatively, to send logs to an OpenTelemetry collector, use these environment variables:
OTEL_SERVICE_NAME="my-service" \
OTEL_EXPORTER_OTLP_ENDPOINT="my-collector:4317" \
OTEL_EXPORTER_OTLP_INSECURE=true \
go run app.go