: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(llma): populate $group_0 for otel-ingested ai events (#65172)
Carlos Marchal4dd00e2458f2
diff --git a/nodejs/src/ingestion/pipelines/ai/metrics.ts b/nodejs/src/ingestion/pipelines/ai/metrics.tsindex 16c2616db939..03aa51273542 100644--- a/nodejs/src/ingestion/pipelines/ai/metrics.ts+++ b/nodejs/src/ingestion/pipelines/ai/metrics.ts@@ -53,3 +53,9 @@ export const aiOtelSystemInstructionsCounter = new Counter({help: 'Outcome of promoting `gen_ai.system_instructions` into a leading $ai_input system message',labelNames: ['outcome'],})++export const aiOtelGroupsCounter = new Counter({+ name: 'llma_ai_otel_groups_total',+ help: 'Outcome of decoding a string-valued $groups attribute back into an object',+ labelNames: ['outcome'],+})diff --git a/nodejs/src/ingestion/pipelines/ai/otel/attribute-mapping.test.ts b/nodejs/src/ingestion/pipelines/ai/otel/attribute-mapping.test.tsindex 9a85e5cbfa68..7297ff88b110 100644--- a/nodejs/src/ingestion/pipelines/ai/otel/attribute-mapping.test.ts+++ b/nodejs/src/ingestion/pipelines/ai/otel/attribute-mapping.test.ts@@ -652,4 +652,39 @@ describe('mapOtelAttributes', () => {expect(event.properties!.events).toBeUndefined()})})++ describe('$groups normalization', () => {+ it('parses a JSON-string $groups into an object', () => {+ const event = createEvent('$ai_generation', {+ $groups: '{"organization":"8576566a-d932-48a7-a25b-d815619cdc48"}',+ })+ mapOtelAttributes(event)+ expect(event.properties!.$groups).toEqual({ organization: '8576566a-d932-48a7-a25b-d815619cdc48' })+ })++ it('leaves an already-object $groups untouched', () => {+ const groups = { organization: 'org-1' }+ const event = createEvent('$ai_generation', { $groups: groups })+ mapOtelAttributes(event)+ expect(event.properties!.$groups).toEqual({ organization: 'org-1' })+ })++ it('keeps the original string when $groups is not valid JSON', () => {+ const event = createEvent('$ai_generation', { $groups: 'not-json' })+ mapOtelAttributes(event)+ expect(event.properties!.$groups).toBe('not-json')+ })++ it.each(['"org-1"', '42', '["org-1"]'])('keeps a non-object JSON $groups (%s) as-is', (raw) => {+ const event = createEvent('$ai_generation', { $groups: raw })+ mapOtelAttributes(event)+ expect(event.properties!.$groups).toBe(raw)+ })++ it('does nothing when $groups is absent', () => {+ const event = createEvent('$ai_generation', { 'gen_ai.response.model': 'gpt-4' })+ 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 50027fa56182..192b5c8deb2a 100644--- a/nodejs/src/ingestion/pipelines/ai/otel/attribute-mapping.ts+++ b/nodejs/src/ingestion/pipelines/ai/otel/attribute-mapping.ts@@ -1,4 +1,8 @@-import { aiOtelOlderSpecEventsCounter, aiOtelSystemInstructionsCounter } from '~/ingestion/pipelines/ai/metrics'+import {+ aiOtelGroupsCounter,+ aiOtelOlderSpecEventsCounter,+ aiOtelSystemInstructionsCounter,+} from '~/ingestion/pipelines/ai/metrics'import { PluginEvent } from '~/plugin-scaffold'import { parseJSON } from '~/utils/json-parse'@@ -100,6 +104,7 @@ export function mapOtelAttributes(event: PluginEvent): void {convertOlderSpecEvents(event)convertSystemInstructions(event)+ normalizeGroups(event)computeLatency(event)promoteRootSpanToTrace(event)@@ -109,6 +114,29 @@ export function mapOtelAttributes(event: PluginEvent): void {}}+type GroupsOutcome = 'parsed' | 'non_object' | 'malformed'++function normalizeGroups(event: PluginEvent): void {+ const props = event.properties!+ if (typeof props.$groups !== 'string') {+ return+ }++ let outcome: GroupsOutcome+ try {+ const parsed = parseJSON(props.$groups)+ if (typeof parsed === 'object' && parsed !== null && !Array.isArray(parsed)) {+ props.$groups = parsed+ outcome = 'parsed'+ } else {+ outcome = 'non_object'+ }+ } catch {+ outcome = 'malformed'+ }+ aiOtelGroupsCounter.labels({ outcome }).inc()+}+function computeLatency(event: PluginEvent): void {const props = event.properties!const startStr = props['$otel_start_time_unix_nano']