: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
feat(aio): pass $ai_span_name and $groups through vercel-ai otel path (#72395)
Radu Raicea54725e436d85
diff --git a/nodejs/src/ingestion/pipelines/ai/otel/attribute-mapping.test.ts b/nodejs/src/ingestion/pipelines/ai/otel/attribute-mapping.test.tsindex 56e9ae71319d..fc7c80f7e1da 100644--- a/nodejs/src/ingestion/pipelines/ai/otel/attribute-mapping.test.ts+++ b/nodejs/src/ingestion/pipelines/ai/otel/attribute-mapping.test.ts@@ -686,5 +686,11 @@ describe('mapOtelAttributes', () => {mapOtelAttributes(event)expect(event.properties!.$groups).toBeUndefined()})++ it('drops an oversized string $groups instead of parsing it', () => {+ const event = createEvent('$ai_generation', { $groups: `{"organization":"${'x'.repeat(10_001)}"}` })+ mapOtelAttributes(event)+ expect(event.properties!.$groups).toBeUndefined()+ })})})diff --git a/nodejs/src/ingestion/pipelines/ai/otel/attribute-mapping.ts b/nodejs/src/ingestion/pipelines/ai/otel/attribute-mapping.tsindex 20ed5ab2f2df..b6b1509eec61 100644--- a/nodejs/src/ingestion/pipelines/ai/otel/attribute-mapping.ts+++ b/nodejs/src/ingestion/pipelines/ai/otel/attribute-mapping.ts@@ -56,6 +56,11 @@ const OLDER_SPEC_CHOICE_EVENT_NAME = 'gen_ai.choice'// `parseJSON` into multi-megabyte input and pressure the ingestion worker.const MAX_OLDER_SPEC_EVENTS_LENGTH = 500_000+// Upper bound on the serialized size of a string-valued `$groups` attribute we+// will parse. A groups map is only a handful of short group-type → key pairs,+// so this is generous; it keeps a pathological payload out of parseJSON.+const MAX_GROUPS_LENGTH = 10_000+const REQUEST_TYPE_TO_EVENT: Record<string, string> = {chat: '$ai_generation',completion: '$ai_generation',@@ -114,7 +119,7 @@ export function mapOtelAttributes(event: PluginEvent): void {}}-type GroupsOutcome = 'parsed' | 'non_object' | 'malformed'+type GroupsOutcome = 'parsed' | 'non_object' | 'malformed' | 'too_large'function normalizeGroups(event: PluginEvent): void {const props = event.properties!@@ -122,6 +127,12 @@ function normalizeGroups(event: PluginEvent): void {return}+ if (props.$groups.length > MAX_GROUPS_LENGTH) {+ delete props.$groups+ aiOtelGroupsCounter.labels({ outcome: 'too_large' }).inc()+ return+ }+let outcome: GroupsOutcometry {const parsed = parseJSON(props.$groups)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 c2dc78d7dbce..99e2311e1be4 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@@ -306,6 +306,67 @@ describe('vercel-ai middleware', () => {expect(event.properties!['$ai_prompt_version']).toBeUndefined()expect(event.properties!['ai.telemetry.metadata.$ai_prompt_version']).toBeUndefined()})++ it('promotes ai.telemetry.metadata.$groups so the mapper can parse it', () => {+ const event = createEvent('$ai_generation', {+ 'ai.operationId': 'ai.generateText.doGenerate',+ 'ai.telemetry.metadata.$groups': '{"organization":"org-1"}',+ })+ convertOtelEvent(event)++ expect(event.properties!['$groups']).toBe('{"organization":"org-1"}')+ expect(event.properties!['ai.telemetry.metadata.$groups']).toBeUndefined()+ })++ it.each(['', 42, true, ['org-1']])('does not promote invalid groups metadata %p', (metadataGroups) => {+ const event = createEvent('$ai_generation', {+ 'ai.operationId': 'ai.generateText.doGenerate',+ 'ai.telemetry.metadata.$groups': metadataGroups,+ })+ convertOtelEvent(event)++ expect(event.properties!['$groups']).toBeUndefined()+ expect(event.properties!['ai.telemetry.metadata.$groups']).toBeUndefined()+ })++ it('preserves a directly-set $groups attribute over telemetry metadata', () => {+ const directGroups = '{"organization":"direct-org"}'+ const event = createEvent('$ai_generation', {+ 'ai.operationId': 'ai.generateText.doGenerate',+ $groups: directGroups,+ 'ai.telemetry.metadata.$groups': '{"organization":"metadata-org"}',+ })+ convertOtelEvent(event)++ expect(event.properties!['$groups']).toBe(directGroups)+ expect(event.properties!['ai.telemetry.metadata.$groups']).toBeUndefined()+ })++ it.each([+ ['custom-span', 'custom-span'],+ ['', 'my-func'],+ ])('span-name metadata %p overrides the functionId-derived name to %p', (metadataName, expectedSpanName) => {+ const event = createEvent('$ai_trace', {+ 'ai.telemetry.functionId': 'my-func',+ 'ai.telemetry.metadata.$ai_span_name': metadataName,+ })+ convertOtelEvent(event)++ expect(event.properties!['$ai_span_name']).toBe(expectedSpanName)+ expect(event.properties!['ai.telemetry.metadata.$ai_span_name']).toBeUndefined()+ })++ it('does not apply span-name metadata to a provider generation span', () => {+ const event = createEvent('$ai_generation', {+ 'ai.operationId': 'ai.generateText.doGenerate',+ $ai_span_name: 'ai.generateText.doGenerate',+ 'ai.telemetry.metadata.$ai_span_name': 'custom-span',+ })+ convertOtelEvent(event)++ expect(event.properties!['$ai_span_name']).toBe('ai.generateText.doGenerate')+ expect(event.properties!['ai.telemetry.metadata.$ai_span_name']).toBeUndefined()+ })})describe('$ai_trace (top-level span)', () => {diff --git a/nodejs/src/ingestion/pipelines/ai/otel/middleware/vercel-ai.ts b/nodejs/src/ingestion/pipelines/ai/otel/middleware/vercel-ai.tsindex 33984d3601de..fa1985277e63 100644--- a/nodejs/src/ingestion/pipelines/ai/otel/middleware/vercel-ai.ts+++ b/nodejs/src/ingestion/pipelines/ai/otel/middleware/vercel-ai.ts@@ -151,6 +151,14 @@ function process(event: PluginEvent, next: () => void): void {}delete props['ai.response.text']+ // Promote a groups map supplied via telemetry metadata before next(), so the+ // generic mapper's normalizeGroups() can parse the JSON string and attach it.+ // Skip if a $groups span attribute was already set directly.+ const groupsMetadata = props['ai.telemetry.metadata.$groups']+ if (props['$groups'] === undefined && isNonEmptyString(groupsMetadata)) {+ props['$groups'] = groupsMetadata+ }+next()// For trace-level spans (top-level generateText/streamText), set input/output state.@@ -243,6 +251,13 @@ function process(event: PluginEvent, next: () => void): void {props[AI_PROMPT_VERSION_KEY] = promptVersion}+ // Vercel repeats telemetry metadata across provider spans, so only use the+ // custom call name for the top-level event.+ const spanNameOverride = props['ai.telemetry.metadata.$ai_span_name']+ if (isTopLevel && isNonEmptyString(spanNameOverride)) {+ props['$ai_span_name'] = spanNameOverride+ }+// 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)) {