.NET tracing installation

  1. Install OpenTelemetry packages

    Required

    For the complete SDK reference, see the OpenTelemetry .NET docs.

    Terminal
    dotnet add package OpenTelemetry
    dotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocol
    dotnet add package OpenTelemetry.Extensions.Hosting

    OpenTelemetry.Extensions.Hosting wires OpenTelemetry into the host builder. For a plain console app using Sdk.CreateTracerProviderBuilder(), you can leave it out.

  2. Get your project token

    Required

    You'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 with phx_).

    You can find your project token in Project settings.

  3. Configure the SDK

    Required

    Set up OpenTelemetry to export spans to PostHog over OTLP HTTP. The AddSource name must match the ActivitySource you create in the next step.

    C#
    using OpenTelemetry;
    using OpenTelemetry.Exporter;
    using OpenTelemetry.Resources;
    using OpenTelemetry.Trace;
    var builder = WebApplication.CreateBuilder(args);
    builder.Services.AddOpenTelemetry()
    .ConfigureResource(resource => resource.AddService("my-service"))
    .WithTracing(tracing => tracing
    .AddSource("MyCompany.MyApp")
    .AddOtlpExporter(options =>
    {
    options.Endpoint = new Uri("https://us.i.posthog.com/i/v1/traces");
    options.Protocol = OtlpExportProtocol.HttpProtobuf;
    options.Headers = "Authorization=Bearer <ph_project_token>";
    }));

    Alternatively, configure the exporter with environment variables and call AddOtlpExporter() with no arguments:

    Terminal
    OTEL_EXPORTER_OTLP_TRACES_ENDPOINT="https://us.i.posthog.com/i/v1/traces"
    OTEL_EXPORTER_OTLP_TRACES_HEADERS="Authorization=Bearer <ph_project_token>"
    OTEL_EXPORTER_OTLP_TRACES_PROTOCOL="http/protobuf"
    OTEL_SERVICE_NAME="my-service"

    Note: With HttpProtobuf, the Endpoint is used as-is, so include the full /i/v1/traces path. Don't use the base OTEL_EXPORTER_OTLP_ENDPOINT variable, which appends its own /v1/traces.

  4. Create spans

    Required

    .NET uses the built-in ActivitySource API for tracing. The source name must match the AddSource call above.

    C#
    using System.Diagnostics;
    static readonly ActivitySource MyActivitySource = new("MyCompany.MyApp");
    public void ProcessOrder(string orderId)
    {
    using Activity? activity = MyActivitySource.StartActivity("process-order");
    activity?.SetTag("order.id", orderId);
    // ... do work ...
    }

    StartActivity returns null when nothing is listening to the source, so use the null-conditional activity?. operator.

  5. Test your setup

    Recommended

    Once everything is configured, confirm spans are reaching PostHog:

    1. Run your application and trigger the instrumented code
    2. Open the PostHog Tracing interface
    3. Confirm your spans and traces appear
    View your traces in PostHog
  6. Next steps

    Checkpoint
    What you can do with your traces

    ActionDescription
    Why you need distributed tracingWhat a trace shows you that nothing else does
    Explore tracesRead a trace as a waterfall to see where time goes
    Filter spansNarrow down by service, status, duration, and attributes
    Propagate contextPass trace context across services so spans join the same trace

    View your traces in PostHog

Community questions

Was this page useful?

Questions about this page? or post a community question.