Skip to content

Commit cc7b6d8

Browse files
committed
feat(node): distributed tracing spans
Adds startSpan, withSpan and getActiveSpan to posthog-node behind a new traces client option, encoding spans as OTLP JSON without an OpenTelemetry dependency.
1 parent 956d252 commit cc7b6d8

37 files changed

Lines changed: 4811 additions & 574 deletions
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
'posthog-node': minor
3+
'@posthog/core': minor
4+
'@posthog/types': minor
5+
---
6+
7+
Add distributed tracing to posthog-node behind a new `traces` client option. `withSpan` / `startSpan` / `getActiveSpan` create OpenTelemetry-shaped spans that export to PostHog with no OpenTelemetry dependency, and spans created inside a request context automatically carry `posthogDistinctId` and `sessionId` so traces link back to people and sessions. Tracing stays off until `traces` is configured, and the API is marked `@experimental` while PostHog's tracing product is in beta.
8+
9+
Spans, logs and metrics now share one OTLP attribute encoder, so a `bigint` attribute is sent as an int64 rather than a plain string.

packages/core/src/__tests__/posthog.flush.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -762,11 +762,12 @@ describe('PostHog Core', () => {
762762
})
763763

764764
describe('OTLP batch senders', () => {
765-
// Both share one `_sendOtlpBatch`; the table pins them to the same
765+
// All three share one `_sendOtlpBatch`; the table pins them to the same
766766
// classification so a wrapper can't reintroduce a per-signal retry policy.
767767
const senders = {
768768
logs: (client: PostHogCoreTestClient) => client._sendLogsBatch({ resourceLogs: [] }),
769769
metrics: (client: PostHogCoreTestClient) => client._sendMetricsBatch({ resourceMetrics: [] }),
770+
traces: (client: PostHogCoreTestClient) => client._sendTracesBatch({ resourceSpans: [] }),
770771
}
771772

772773
const cases: [number, string][] = [

packages/core/src/index.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,8 @@ export {
4242
buildResourceAttributes,
4343
getOtlpSeverityNumber,
4444
getOtlpSeverityText,
45-
toOtlpAnyValue,
46-
toOtlpKeyValueList,
4745
} from './logs/logs-utils'
46+
export { toOtlpAnyValue, toOtlpKeyValueList } from './utils/otlp-any-value'
4847
export { PostHogLogs } from './logs'
4948
export type {
5049
BeforeSendLogFn,
@@ -87,6 +86,27 @@ export type {
8786
Metrics,
8887
MetricsConfig,
8988
} from './metrics/types'
89+
export { PostHogTraces } from './traces'
90+
export { SyncSpanContextManager } from './traces/context'
91+
export { NOOP_SPAN } from './traces/span'
92+
export type {
93+
ResolvedTracesConfig,
94+
SendTracesBatchOutcome,
95+
SpanContextManager,
96+
TraceSdkContext,
97+
TracesHost,
98+
} from './traces/types'
99+
// Same barrel convention as logs and metrics for the user-facing tracing types.
100+
export type {
101+
Span,
102+
SpanAttributes,
103+
SpanAttributeValue,
104+
SpanKind,
105+
SpanStatusCode,
106+
SpanTimeInput,
107+
StartSpanOptions,
108+
TracesConfig,
109+
} from './traces/types'
90110
export { uuidv7 } from './vendor/uuidv7'
91111
export * from './cookie'
92112
export * from './posthog-core'

0 commit comments

Comments
 (0)