Commit eec92bf
committed
fix(observability): group AI generations by their real trace, and attribute spend per repo
Two faults made PostHog's AI observability structurally unable to answer the
questions that matter most for a product where AI is the product.
Every generation was its own trace. capturePostHogAiGeneration set
$ai_trace_id to a fresh randomUUID() per call, producing 13,428 AI events
across 13,428 distinct trace ids on the live project. A review fans out across
RAG embeddings, both dual-review legs, N retries per model, and any
self-consistency runs — and each landed as an unrelated single-event trace, so
what a review actually did, and where its time and money went, was not
recoverable.
The fix was already in the file. operationalProperties has called
currentOtelTraceIds() since #8296 and attached the real ids as plain
trace_id/span_id properties; the line below then minted a UUID and PostHog
grouped by that instead. withReviewPipelineSpan already wraps a whole review,
so the ambient trace id is exactly the grouping key wanted — every provider
attempt inside one review now nests under one PostHog trace. $ai_span_id and a
readable $ai_span_name come from the same source.
randomUUID() stays as the fallback rather than being removed: AI_EMBED,
AI_VISION and AI_ADVISORY run outside any review span, so an orphan trace is a
legitimate outcome there — but such an event must not claim a span id it does
not have, so $ai_span_id is omitted rather than faked.
AI spend was unattributable. Every AI event carried distinct_id
"loopover-selfhost" and no $groups at all, so cost per repository was
answerable only by SQL against ai_usage_events, never in PostHog. A repo group
is now stamped on every event. It deliberately reads the already-processed repo
value off operationalProperties rather than the raw context: under the shared
LOOPOVER_CENTRAL_POSTHOG_KEY that value is HMAC-anonymized, and is dropped
entirely when the anon secret has not been injected, so the group inherits that
fail-closed behavior for free and a raw private repo name cannot reach the
group index by this path.
Both branches of both changes are covered: ambient span vs none, and repo
present vs dropped.
Content capture ($ai_input/$ai_output_choices) is deliberately untouched — the
metadata-only policy is documented in self-hosting-operations.mdx and changing
it is a privacy decision, not a wiring one.
Closes #101851 parent 8e4a7fb commit eec92bf
2 files changed
Lines changed: 77 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
417 | 417 | | |
418 | 418 | | |
419 | 419 | | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
420 | 436 | | |
421 | | - | |
422 | | - | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
423 | 443 | | |
424 | 444 | | |
425 | 445 | | |
| |||
440 | 460 | | |
441 | 461 | | |
442 | 462 | | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
443 | 470 | | |
444 | 471 | | |
445 | 472 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
601 | 601 | | |
602 | 602 | | |
603 | 603 | | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
| 620 | + | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
| 624 | + | |
| 625 | + | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
| 629 | + | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
| 651 | + | |
604 | 652 | | |
605 | 653 | | |
606 | 654 | | |
| |||
0 commit comments