.NET tracing installation
- 1
Install OpenTelemetry packages
RequiredFor the complete SDK reference, see the OpenTelemetry .NET docs.
TerminalOpenTelemetry.Extensions.Hostingwires OpenTelemetry into the host builder. For a plain console app usingSdk.CreateTracerProviderBuilder(), you can leave it out. - 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 OpenTelemetry to export spans to PostHog over OTLP HTTP. The
AddSourcename must match theActivitySourceyou create in the next step.C#Alternatively, configure the exporter with environment variables and call
AddOtlpExporter()with no arguments:TerminalNote: With
HttpProtobuf, theEndpointis used as-is, so include the full/i/v1/tracespath. Don't use the baseOTEL_EXPORTER_OTLP_ENDPOINTvariable, which appends its own/v1/traces. - 4
Create spans
Required.NET uses the built-in
ActivitySourceAPI for tracing. The source name must match theAddSourcecall above.C#StartActivityreturnsnullwhen nothing is listening to the source, so use the null-conditionalactivity?.operator. - 5
Link spans to people and sessions
RecommendedFor ASP.NET Core on .NET 8 or later, use the PostHog request-context integration. Add PostHog to your existing host builder and put the middleware before your routes:
C#Configure
tracing_headersfor your backend hostname in the browser SDK. The middleware adds missingsessionIdandposthogDistinctIdtags to the active requestActivity. The host-owned listener adds those tags to child activities when they start. Keep your existing OpenTelemetry provider, exporter, and sampling configuration.Existing
sessionIdandposthogDistinctIdtags take precedence. Set authenticated application identity withposthogDistinctId. Missing IDs stay absent. Tracing headers are untrusted analytics context. Use authenticated application identity for access decisions.Activities that already started keep their tags after the request ends. New activities in detached work do not receive the ended request's IDs. Background jobs need their own context transfer. The core PostHog SDK does not provide a native span API or a trace exporter.
- 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











