Skip to content
Download

Send OpenTelemetry from Go to your Mac

Go has no automatic instrumentation. You start the SDK yourself, in about twenty lines, and wrap the handlers you care about.

  • Loggerhead is open.
  • You know the effective port. Open Collectors → Loggerhead receiver → Connection and copy the HTTP port. The first workspace uses 4318. See Ports and the effective port.
Terminal window
go get go.opentelemetry.io/otel \
go.opentelemetry.io/otel/sdk \
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp \
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp
main.go
func startTracing(ctx context.Context) (func(context.Context) error, error) {
exporter, err := otlptracehttp.New(ctx)
if err != nil {
return nil, err
}
res, err := resource.New(ctx,
resource.WithAttributes(semconv.ServiceName("my-go-service")),
)
if err != nil {
return nil, err
}
tp := trace.NewTracerProvider(
trace.WithBatcher(exporter),
trace.WithResource(res),
)
otel.SetTracerProvider(tp)
return tp.Shutdown, nil
}

otlptracehttp.New reads OTEL_EXPORTER_OTLP_ENDPOINT, so the address stays out of the code.

mux := http.NewServeMux()
mux.Handle("/orders", ordersHandler)
http.ListenAndServe(":8080", otelhttp.NewHandler(mux, "http"))
Terminal window
export OTEL_EXPORTER_OTLP_ENDPOINT=http://127.0.0.1:4318

Run the service and call one route.

Signal State
Traces Every handler you wrap, and every client call you instrument.
Metrics Add the metric SDK and otlpmetrichttp the same way.
Logs Add otelslog to bridge log/slog, or send the output through a branch collector.
Terminal window
export OTEL_RESOURCE_ATTRIBUTES=deployment.environment.name=staging

See Workspaces and environments.

  1. Open Traces in Loggerhead and click a request to open its waterfall.
  2. Connect your coding agent once, and it reads the same data. See Claude Code.