Go
1. Add the setup file
Section titled “1. Add the setup file”Save this as otel.go in your main package:
package main
import ( "context" "errors"
"go.opentelemetry.io/otel" "go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp" "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp" "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp" "go.opentelemetry.io/otel/log/global" "go.opentelemetry.io/otel/propagation" sdklog "go.opentelemetry.io/otel/sdk/log" sdkmetric "go.opentelemetry.io/otel/sdk/metric" sdktrace "go.opentelemetry.io/otel/sdk/trace")
// setupOTel sends traces, metrics and logs to the endpoint in OTEL_EXPORTER_OTLP_ENDPOINT.func setupOTel(ctx context.Context) (func(context.Context) error, error) { traces, err := otlptracehttp.New(ctx) if err != nil { return nil, err } metrics, err := otlpmetrichttp.New(ctx) if err != nil { return nil, err } logs, err := otlploghttp.New(ctx) if err != nil { return nil, err } tp := sdktrace.NewTracerProvider(sdktrace.WithBatcher(traces)) mp := sdkmetric.NewMeterProvider(sdkmetric.WithReader(sdkmetric.NewPeriodicReader(metrics))) lp := sdklog.NewLoggerProvider(sdklog.WithProcessor(sdklog.NewBatchProcessor(logs))) otel.SetTracerProvider(tp) otel.SetMeterProvider(mp) global.SetLoggerProvider(lp) otel.SetTextMapPropagator(propagation.TraceContext{}) return func(ctx context.Context) error { return errors.Join(tp.Shutdown(ctx), mp.Shutdown(ctx), lp.Shutdown(ctx)) }, nil}Call it at the start of main:
shutdown, err := setupOTel(context.Background())if err != nil { log.Fatal(err)}defer shutdown(context.Background())Then fetch the packages:
go mod tidy2. Configure
Section titled “2. Configure”Set these where your app runs. Replace YOUR_INGEST_KEY with your key and checkout with your service name.
export OTEL_SERVICE_NAME=checkoutexport OTEL_RESOURCE_ATTRIBUTES=deployment.environment.name=productionexport OTEL_EXPORTER_OTLP_ENDPOINT=https://otlp.siteqwality.comexport OTEL_EXPORTER_OTLP_HEADERS=Authorization=YOUR_INGEST_KEYexport OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf3. Trace requests
Section titled “3. Trace requests”Wrap your HTTP handlers with otelhttp.NewHandler and every request becomes a trace:
http.Handle("/order", otelhttp.NewHandler(http.HandlerFunc(order), "order"))Start your app as usual, for example with go run ..
4. Send a log
Section titled “4. Send a log”Log with the standard log/slog package through the OpenTelemetry bridge. Pass the request context to link a record to its trace:
var logger = otelslog.NewLogger("checkout")
logger.InfoContext(r.Context(), "order placed", "orderId", 42)5. Record a metric (optional)
Section titled “5. Record a metric (optional)”orders, _ := otel.Meter("checkout").Int64Counter("orders.placed")
orders.Add(r.Context(), 1)See your data
Section titled “See your data”Open Logs, Traces or Metrics. New data usually shows up within a minute, and a page that says it is waiting for data switches to your data on its own.
The code on this page comes from a runnable example that is checked against our ingest rules every month.
Nothing arriving? Check that the key is an ingest key for this account, that your account has early access, and the status codes in Ingest routes and limits.