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.
Before you start
Section titled “Before you start”- 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.
Step 1: get the packages
Section titled “Step 1: get the packages”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/otelhttpStep 2: start the tracer provider
Section titled “Step 2: start the tracer provider”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.
Step 3: wrap the handler
Section titled “Step 3: wrap the handler”mux := http.NewServeMux()mux.Handle("/orders", ordersHandler)http.ListenAndServe(":8080", otelhttp.NewHandler(mux, "http"))Step 4: set the endpoint
Section titled “Step 4: set the endpoint”export OTEL_EXPORTER_OTLP_ENDPOINT=http://127.0.0.1:4318Run the service and call one route.
What arrives
Section titled “What arrives”| 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. |
Tag the environment
Section titled “Tag the environment”export OTEL_RESOURCE_ATTRIBUTES=deployment.environment.name=stagingSee Workspaces and environments.
Then read it with your agent
Section titled “Then read it with your agent”- Open Traces in Loggerhead and click a request to open its waterfall.
- Connect your coding agent once, and it reads the same data. See Claude Code.