Go tracing installation
- 1
Install OpenTelemetry packages
RequiredFor the complete SDK reference, see the OpenTelemetry Go docs.
Terminalotlptracehttpis the OTLP HTTP/protobuf trace exporter. Theotlptracegrpcmodule sends gRPC, so pickotlptracehttpto match this guide. - 2
Get your project token
RequiredYou'll need your PostHog project token to authenticate trace requests. This is the same token you use for capturing events with the PostHog SDK.
Important: Use your project token which starts with
phc_. Do not use a personal API key (which starts withphx_).You can find your project token in Project settings.
- 3
Configure the SDK
RequiredSet up the OpenTelemetry SDK to export spans to PostHog over OTLP HTTP. Use
WithEndpointURLso the full/i/v1/tracespath is sent as-is.GoCall
defer tp.Shutdown(ctx)inmainso buffered spans flush on exit.Alternatively, configure the exporter with environment variables and call
otlptracehttp.New(ctx)with no options:TerminalNote:
WithEndpointsets only the host and defaults the path to/v1/traces. To send to the full/i/v1/tracespath, useWithEndpointURL(or the per-signalOTEL_EXPORTER_OTLP_TRACES_ENDPOINT), as shown. - 4
Create spans
RequiredWrap the operations you want to measure in spans, and attach attributes for context.
Go - 5
Link spans to people and sessions
RecommendedInstall the optional PostHog OpenTelemetry module:
TerminalRegister the context processor on your existing provider before requests start, then wrap your HTTP handler:
GoThe wrapper accepts the core request middleware options. Configure
tracing_headersfor your backend hostname in the browser SDK. Put OpenTelemetry HTTP middleware outside the PostHog wrapper to enrich its existing request span. Create child spans withr.Context()so the processor can read the request IDs.The bridge adds missing
sessionIdandposthogDistinctIdattributes. ExistingsessionIdandposthogDistinctIdattributes take precedence. Set authenticated application identity withposthogDistinctId. Missing IDs stay absent. The optional module keeps your existing provider, exporter, and sampler. It does not change the core SDK dependencies.Request context expires when the handler returns or panics. Spans that already started keep their attributes. New spans from an ended request context omit its IDs. For a separate background job, start from a fresh non-request context and set the job's IDs explicitly. Manual
WithRequestContextuse has no automatic HTTP lifetime management. - 6
Next steps
CheckpointWhat you can do with your tracesAction Description Why you need distributed tracing What a trace shows you that nothing else does Explore traces Read a trace as a waterfall to see where time goes Filter spans Narrow down by service, status, duration, and attributes Propagate context Pass trace context across services so spans join the same trace











