Node.js tracing installation
Contents
There are two ways to send spans from Node.js.
posthog-node | OpenTelemetry | |
|---|---|---|
| Packages | The SDK you already use for analytics | Six @opentelemetry/* packages |
| Instrumentation | Manual – you wrap the operations you care about | Manual, plus auto-instrumentation for HTTP, Express, databases, and more |
| Person and session join | Automatic inside a PostHog request context | Set the attributes yourself |
Pick OpenTelemetry if you already run it, or if you want spans from your HTTP server and database driver without writing them yourself. Pick posthog-node if PostHog is your only tracing backend and you'd rather instrument a handful of operations by hand than add an exporter pipeline.
Both routes send OTLP spans to the same endpoint, so you can start with one and switch later without losing your traces.
With posthog-node
Minimum version:
posthog-node@5.52.0or later.
- 1
Install posthog-node
Required - 2
Enable tracing
RequiredTracing is off until you set the
tracesoption. There's no OpenTelemetry dependency to add.JavaScriptOption Description serviceNameIdentifies the service in the Tracing UI. Maps to service.nameserviceVersionRelease version. Maps to service.versionenvironmentDeployment environment, e.g. production. Maps todeployment.environmentresourceAttributesAdditional OpenTelemetry resource attributes Use your project token (the same one you use for capturing events), not a personal API key.
See the Node.js SDK docs for batching, queue and span-limit options, and
beforeSpanSendfor scrubbing attributes or dropping spans before they're exported. - 3
Create spans
RequiredwithSpanruns a callback with a span active for its duration and ends the span for you. Spans created inside the callback nest underneath it automatically.JavaScriptIf the callback throws or rejects, the span records the exception, its status is set to
error, and your original error propagates unchanged.Span names should be low-cardinality operation names –
GET /users/:id, notGET /users/123. Variable values belong in attributes.For work that can't wrap a callback,
startSpanreturns a span you end yourself. See the Node.js SDK docs for the full span API and for continuing a trace across services with W3Ctraceparentheaders. - 4
Link spans to people and sessions
RecommendedSpans created inside a PostHog request context carry
posthogDistinctIdandsessionIdattributes, which is what makes a trace reachable from a person or a Session Replay recording.JavaScriptIf you use Express, the PostHog middleware sets this up for every request, and reads the
X-POSTHOG-DISTINCT-IDandX-POSTHOG-SESSION-IDheaders thattracing_headerssends from the browser. - 5
Flush before the process exits
RecommendedQueued spans are exported on an interval, so a short-lived process can exit before they're sent. Both
flush()andshutdown()export spans that have already ended.JavaScriptIn a serverless handler, call
flush()rather thanshutdown(): the container is reused across invocations, soshutdown()would throw away the connection pool and the flag cache. Callshutdown()when the process is genuinely exiting.
With OpenTelemetry
- 1
Install OpenTelemetry packages
RequiredFor the complete SDK reference, see the OpenTelemetry JavaScript docs.
Terminal@opentelemetry/exporter-trace-otlp-protois the OTLP HTTP/protobuf trace exporter. The similarly named-otlp-httppackage sends HTTP/JSON and-otlp-grpcsends gRPC, so pick-prototo 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.
JavaScriptAlternatively, configure the exporter with environment variables:
TerminalNote: Pass the full
/i/v1/tracespath to the traces endpoint. Don't use the baseOTEL_EXPORTER_OTLP_ENDPOINTvariable, which appends its own/v1/traces. - 4
Create spans
RequiredWrap the operations you want to measure in spans, and attach attributes for context.
JavaScriptTo join these spans to a person or a Session Replay recording, set
posthogDistinctIdandsessionIdattributes yourself, from theX-POSTHOG-DISTINCT-IDandX-POSTHOG-SESSION-IDheaders thattracing_headerssends from the browser.
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