:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(aio): stop dropping posthog_ custom properties on otel paths (#73578)
Carlos Marchalfc211d02543c
diff --git a/nodejs/src/ingestion/pipelines/ai/otel/middleware/custom-metadata.test.ts b/nodejs/src/ingestion/pipelines/ai/otel/middleware/custom-metadata.test.tsnew file mode 100644index 000000000000..efb52f52c9e4--- /dev/null+++ b/nodejs/src/ingestion/pipelines/ai/otel/middleware/custom-metadata.test.ts@@ -0,0 +1,35 @@+import { promotePosthogCustomMetadata } from './custom-metadata'++describe('promotePosthogCustomMetadata', () => {+ it.each([+ ['posthog_tags', 'tags', ['beta', 'internal']],+ ['posthog_environment', 'environment', 'prod'],+ ['posthog_constructor', 'constructor', 'ctor-value'],+ ])('promotes <namespace>%s to %s with the prefix stripped', (suffix, name, value) => {+ const props: Record<string, unknown> = { [`ns.${suffix}`]: value }+ promotePosthogCustomMetadata(props, 'ns.')+ expect(props[name]).toEqual(value)+ })++ it.each([+ ['a non-posthog key', 'ns.org_id', 'org_id'],+ ['a reserved $-prefixed name', 'ns.posthog_$ai_model', '$ai_model'],+ ['the reserved distinct_id name', 'ns.posthog_distinct_id', 'distinct_id'],+ ])('does not promote %s', (_label, key, resultName) => {+ const props: Record<string, unknown> = { [key]: 'value' }+ promotePosthogCustomMetadata(props, 'ns.')+ expect(props[resultName]).toBeUndefined()+ })++ it('does not overwrite an existing property', () => {+ const props: Record<string, unknown> = { tags: ['keep'], 'ns.posthog_tags': ['drop'] }+ promotePosthogCustomMetadata(props, 'ns.')+ expect(props['tags']).toEqual(['keep'])+ })++ it('skips __proto__ instead of polluting the prototype', () => {+ const props: Record<string, unknown> = { 'ns.posthog___proto__': { polluted: true } }+ promotePosthogCustomMetadata(props, 'ns.')+ expect(Object.getPrototypeOf(props)).toBe(Object.prototype)+ })+})diff --git a/nodejs/src/ingestion/pipelines/ai/otel/middleware/custom-metadata.ts b/nodejs/src/ingestion/pipelines/ai/otel/middleware/custom-metadata.tsnew file mode 100644index 000000000000..7ba5d1581315--- /dev/null+++ b/nodejs/src/ingestion/pipelines/ai/otel/middleware/custom-metadata.ts@@ -0,0 +1,18 @@+const CUSTOM_METADATA_MARKER = 'posthog_'+const RESERVED_CUSTOM_NAMES = new Set(['distinct_id', '__proto__'])++export function promotePosthogCustomMetadata(props: Record<string, unknown>, namespacePrefix: string): void {+ const prefix = `${namespacePrefix}${CUSTOM_METADATA_MARKER}`+ for (const key of Object.keys(props)) {+ if (!key.startsWith(prefix)) {+ continue+ }+ const name = key.slice(prefix.length)+ if (!name || name.startsWith('$') || RESERVED_CUSTOM_NAMES.has(name)) {+ continue+ }+ if (!Object.prototype.hasOwnProperty.call(props, name)) {+ props[name] = props[key]+ }+ }+}diff --git a/nodejs/src/ingestion/pipelines/ai/otel/middleware/traceloop.test.ts b/nodejs/src/ingestion/pipelines/ai/otel/middleware/traceloop.test.tsindex 6c4ac368c3c5..b71928ecd42d 100644--- a/nodejs/src/ingestion/pipelines/ai/otel/middleware/traceloop.test.ts+++ b/nodejs/src/ingestion/pipelines/ai/otel/middleware/traceloop.test.ts@@ -209,6 +209,17 @@ describe('traceloop middleware', () => {expect(event.properties!['$ai_stop_reason']).toBe('end_turn')})+ it('promotes posthog_-prefixed association properties as custom properties', () => {+ const event = createEvent('$ai_generation', {+ 'llm.request.type': 'chat',+ 'traceloop.association.properties.posthog_env': 'prod',+ })+ traceloop.process(event, () => mapOtelAttributes(event))++ expect(event.properties!['env']).toBe('prod')+ expect(event.properties!['traceloop.association.properties.posthog_env']).toBeUndefined()+ })+it('maps finish_reason to $ai_stop_reason when stop_reason is absent', () => {const event = createEvent('$ai_generation', {'llm.request.type': 'chat',diff --git a/nodejs/src/ingestion/pipelines/ai/otel/middleware/traceloop.ts b/nodejs/src/ingestion/pipelines/ai/otel/middleware/traceloop.tsindex d072c5c3f1bf..eb65425e3edd 100644--- a/nodejs/src/ingestion/pipelines/ai/otel/middleware/traceloop.ts+++ b/nodejs/src/ingestion/pipelines/ai/otel/middleware/traceloop.ts@@ -1,5 +1,6 @@import { PluginEvent } from '~/plugin-scaffold'+import { promotePosthogCustomMetadata } from './custom-metadata'import { OtelLibraryMiddleware } from './types'const STRIP_KEYS = [@@ -170,6 +171,7 @@ function process(event: PluginEvent, next: () => void): void {for (const key of STRIP_KEYS) {delete props[key]}+ promotePosthogCustomMetadata(props, 'traceloop.association.properties.')for (const key of Object.keys(props)) {if (key.startsWith('traceloop.association.properties.')) {delete props[key]diff --git a/nodejs/src/ingestion/pipelines/ai/otel/middleware/vercel-ai.test.ts b/nodejs/src/ingestion/pipelines/ai/otel/middleware/vercel-ai.test.tsindex 99e2311e1be4..ce6ed59b8158 100644--- a/nodejs/src/ingestion/pipelines/ai/otel/middleware/vercel-ai.test.ts+++ b/nodejs/src/ingestion/pipelines/ai/otel/middleware/vercel-ai.test.ts@@ -261,7 +261,18 @@ describe('vercel-ai middleware', () => {expect(event.properties![`ai.telemetry.metadata.${key}`]).toBeUndefined()})- it('uses posthog_distinct_id as the event distinct_id', () => {+ it('promotes posthog_-prefixed telemetry metadata as a custom property', () => {+ const event = createEvent('$ai_generation', {+ 'ai.operationId': 'ai.generateText.doGenerate',+ 'ai.telemetry.metadata.posthog_tags': ['beta'],+ })+ convertOtelEvent(event)++ expect(event.properties!['tags']).toEqual(['beta'])+ expect(event.properties!['ai.telemetry.metadata.posthog_tags']).toBeUndefined()+ })++ it('uses posthog_distinct_id as the event distinct_id without creating a bare distinct_id property', () => {const event = createEvent('$ai_generation', {'ai.operationId': 'ai.generateText.doGenerate','ai.telemetry.metadata.posthog_distinct_id': 'user-1',@@ -269,6 +280,7 @@ describe('vercel-ai middleware', () => {convertOtelEvent(event)expect(event.distinct_id).toBe('user-1')+ expect(event.properties!['distinct_id']).toBeUndefined()})it('ignores empty posthog_distinct_id metadata', () => {diff --git a/nodejs/src/ingestion/pipelines/ai/otel/middleware/vercel-ai.ts b/nodejs/src/ingestion/pipelines/ai/otel/middleware/vercel-ai.tsindex fa1985277e63..a1642890ebdc 100644--- a/nodejs/src/ingestion/pipelines/ai/otel/middleware/vercel-ai.ts+++ b/nodejs/src/ingestion/pipelines/ai/otel/middleware/vercel-ai.ts@@ -2,6 +2,7 @@ import { parseJSON } from '~/common/utils/json-parse'import { mustAddReasoningCost } from '~/ingestion/pipelines/ai/costs/output-costs'import { PluginEvent } from '~/plugin-scaffold'+import { promotePosthogCustomMetadata } from './custom-metadata'import { OtelLibraryMiddleware } from './types'// Vercel AI SDK attributes to strip after processing. Includes both@@ -258,6 +259,8 @@ function process(event: PluginEvent, next: () => void): void {props['$ai_span_name'] = spanNameOverride}+ promotePosthogCustomMetadata(props, 'ai.telemetry.metadata.')+// Strip Vercel-specific telemetry metadata and request headers after preserving// the PostHog identifiers we rely on for event linkage and session grouping.for (const key of Object.keys(props)) {