PostHog
PostHog’s Tracing takes OpenTelemetry traces over OTLP. SpanSlice can send the traces it keeps there, next to the product analytics, session replays and errors you already send to PostHog.
PostHog’s Tracing is in beta.
Add PostHog as an exporter
Section titled “Add PostHog as an exporter”- In the app, go to Organization settings › Exporters, choose Add exporter, and pick PostHog.
- Pick the region your PostHog project is in: US Cloud or EU Cloud.
- Paste the project token from your PostHog project settings. It starts with
phc_. Don’t use a personal API key (phx_): PostHog refuses those. - A new exporter is on for every project. To limit it to some services or environments, use Project settings › Exporters.
SpanSlice sends traces to https://us.i.posthog.com/i/v1/traces (or eu.i.posthog.com) with an
Authorization: Bearer <token> header. Only traces are sent, because PostHog’s metrics are in alpha.
PostHog accepts spans sent with a wrong token and returns no error, so Check & save succeeds either way. After your service sends its first traces, search for the service name in PostHog’s Tracing view to confirm they arrived.
Link spans to people and session replays
Section titled “Link spans to people and session replays”PostHog links a span to a person and a session replay through two span attributes: posthogDistinctId and
sessionId. posthog-node’s PostHogContextSpanProcessor sets them. Pass it to SpanSlice with spanProcessors, before
the processor that exports:
import { PostHog } from 'posthog-node'import { PostHogContextSpanProcessor } from 'posthog-node/otel'import { spanSliceInit } from 'spanslice'import { HttpInstrumentation } from '@opentelemetry/instrumentation-http'
export const posthog = new PostHog(process.env.POSTHOG_TOKEN!, { host: 'https://us.i.posthog.com' })
const telemetry = spanSliceInit({ service: 'api', instrumentations: [new HttpInstrumentation()], spanProcessors: (exporting) => [new PostHogContextSpanProcessor(posthog), exporting],})await telemetry.readyThe processor reads the IDs from posthog-node’s request context:
- Express: add posthog-node’s
setupExpressRequestContextbefore your routes. It reads theX-POSTHOG-DISTINCT-IDandX-POSTHOG-SESSION-IDheaders. - Anything else: run the request’s work inside
posthog.withContext({ distinctId, sessionId }, fn).
In the browser, set tracing_headers in posthog-js (1.380.0 or later) to your API’s hostnames. posthog-js then adds
both headers to matching fetch and XMLHttpRequest calls. Your API’s CORS settings must allow them.
posthog.init('phc_…', { api_host: 'https://us.i.posthog.com', tracing_headers: ['api.example.com'],})The processor sets the attributes when a span starts, before SpanSlice’s rules run. A rule can match on them, for example to keep every trace from a session that’s being recorded, or from a particular account.
Errors
Section titled “Errors”posthog-node adds the active span’s trace and span ID to the exceptions it captures, as $trace_id and $span_id. If a
rule drops that trace, the exception points at a trace PostHog never received.
To keep those traces, mark the span as an error when you send the exception to PostHog:
import { SpanStatusCode, trace } from '@opentelemetry/api'
try { await processOrder()} catch (err) { const span = trace.getActiveSpan() span?.recordException(err as Error) span?.setStatus({ code: SpanStatusCode.ERROR }) posthog.captureException(err) throw err}Then add a rule that keeps traces with an error:
When a root span is completed and matches Spans In Trace any of Has Error is true, then Set Sample Rate 1 in 1, Overriding Any Existing Sampling.
Drop Trace beats every sample rate, so a Drop Trace rule that matches the trace still drops it.
Use OpenTelemetry, not posthog-node’s tracing
Section titled “Use OpenTelemetry, not posthog-node’s tracing”posthog-node has a tracing API of its own (posthog.withSpan and the traces option). Those spans skip OpenTelemetry,
so SpanSlice never sees them and its rules don’t apply. Trace with OpenTelemetry instrumentations and let SpanSlice
export the kept spans.