feat(ai): add OpenTelemetry integration for AI span export#494
Open
richardsolomou wants to merge 3 commits intomasterfrom
Open
feat(ai): add OpenTelemetry integration for AI span export#494richardsolomou wants to merge 3 commits intomasterfrom
richardsolomou wants to merge 3 commits intomasterfrom
Conversation
Add PostHogSpanProcessor and PostHogTraceExporter that filter AI-related OTel spans and forward them to PostHog's OTLP endpoint. Generated-By: PostHog Code Task-Id: 1ba1f07a-1453-4162-90a8-665958c5fe46
Contributor
posthog-python Compliance ReportDate: 2026-04-09 08:45:50 UTC ✅ All Tests Passed!0/0 tests passed |
Contributor
|
Aligns with the endpoint used by the OTel examples in PR #482. Generated-By: PostHog Code Task-Id: 1ba1f07a-1453-4162-90a8-665958c5fe46
Move DEFAULT_HOST to spans.py, extract shared make_span helper to conftest.py, and parameterize span filter tests. Generated-By: PostHog Code Task-Id: 1ba1f07a-1453-4162-90a8-665958c5fe46
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Users instrumenting their AI/LLM applications with OpenTelemetry have no way to route AI spans to PostHog without writing custom glue code. This is the Python equivalent of PostHog/posthog-js#3358.
Changes
Adds a
posthog.ai.otelmodule with two integration patterns:PostHogSpanProcessor(recommended) — self-containedSpanProcessorthat wrapsBatchSpanProcessor+OTLPSpanExporter, filtering to only forward AI spans to PostHog's OTLP endpoint at/i/v0/ai/otelPostHogTraceExporter—SpanExporterthat filters AI spans before forwarding, for setups that only accept an exporter (e.g. passed toSimpleSpanProcessor)is_ai_span()— shared predicate matchinggen_ai.*,llm.*,ai.*,traceloop.*prefixes on span names and attribute keysAlso adds
posthog[otel]optional dependency group foropentelemetry-sdkandopentelemetry-exporter-otlp-proto-http.Usage
Consistency with #482
Uses the same OTLP endpoint (
/i/v0/ai/otel) and auth header format (Authorization: Bearer {api_key}) as the migrated examples in #482.How did you test this code?
31 unit tests covering span filtering, processor forwarding/dropping, exporter filtering, endpoint configuration, and lifecycle delegation.