From aa3e91fca3862f9a029f68c961c850a7b7b247bc Mon Sep 17 00:00:00 2001 From: Fahad Date: Tue, 17 Nov 2026 04:41:04 +0500 Subject: [PATCH] fix(ai): keep dropped counts on spans copied for PostHog context opentelemetry-sdk 1.20 and 1.21 read `.dropped` from a span's attributes, events and links during OTLP export. The copy made to add the context's distinct ID, session ID and tags held plain dicts and tuples, so export failed with AttributeError. Use the SDK's bounded collections and carry over the counts the SDK dropped under its span limits. --- .../fix-otel-context-export-older-sdk.md | 5 +++ posthog/ai/otel/_request_context.py | 16 ++++++-- posthog/test/ai/otel/test_processor.py | 37 ++++++++++++++++++- 3 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 .sampo/changesets/fix-otel-context-export-older-sdk.md diff --git a/.sampo/changesets/fix-otel-context-export-older-sdk.md b/.sampo/changesets/fix-otel-context-export-older-sdk.md new file mode 100644 index 000000000..75d4bc148 --- /dev/null +++ b/.sampo/changesets/fix-otel-context-export-older-sdk.md @@ -0,0 +1,5 @@ +--- +pypi/posthog: patch +--- + +`PostHogSpanProcessor` no longer breaks export of spans started inside a PostHog context on opentelemetry-sdk 1.20 and 1.21. The span copy that carries the context's distinct ID, session ID and tags held plain dicts and tuples, but those SDK versions' OTLP encoder reads `.dropped` from its attributes, events and links and failed with `AttributeError`. The copy now also keeps the counts of attributes, events and links the SDK dropped under its span limits, which it previously reported as 0. diff --git a/posthog/ai/otel/_request_context.py b/posthog/ai/otel/_request_context.py index dc0018c5a..4e415c67d 100644 --- a/posthog/ai/otel/_request_context.py +++ b/posthog/ai/otel/_request_context.py @@ -9,7 +9,9 @@ from dataclasses import dataclass from typing import Any, Dict, Optional, Sequence +from opentelemetry.attributes import BoundedAttributes from opentelemetry.sdk.trace import ReadableSpan +from opentelemetry.sdk.util import BoundedList from opentelemetry.util.types import AttributeValue from ...contexts import get_context_distinct_id, get_context_session_id, get_tags @@ -84,14 +86,22 @@ def with_request_context(span: ReadableSpan, context: RequestContext) -> Readabl } if not extra: return span + # opentelemetry-sdk before 1.22 reads `.dropped` from these collections, so plain + # dicts and tuples break export there. + attributes = BoundedAttributes(attributes={**own, **extra}) + events = BoundedList.from_seq(None, span.events) + links = BoundedList.from_seq(None, span.links) + attributes.dropped = span.dropped_attributes + events.dropped = span.dropped_events + links.dropped = span.dropped_links return ReadableSpan( name=span.name, context=span.get_span_context(), parent=span.parent, resource=span.resource, - attributes={**own, **extra}, - events=span.events, - links=span.links, + attributes=attributes, + events=events, + links=links, kind=span.kind, status=span.status, start_time=span.start_time, diff --git a/posthog/test/ai/otel/test_processor.py b/posthog/test/ai/otel/test_processor.py index 06deb61a2..5368555b4 100644 --- a/posthog/test/ai/otel/test_processor.py +++ b/posthog/test/ai/otel/test_processor.py @@ -8,7 +8,8 @@ from concurrent.futures import ThreadPoolExecutor from unittest.mock import MagicMock, patch -from opentelemetry.sdk.trace import TracerProvider +from opentelemetry.sdk.trace import SpanLimits, TracerProvider +from opentelemetry.trace import Link from posthog import contexts from posthog.ai.otel.processor import PostHogSpanProcessor, _reset_processors_after_fork @@ -392,6 +393,40 @@ def test_adds_nothing_without_a_context(self): self.assertEqual(self._exported_attributes(), [{}]) + def test_exported_copy_reports_dropped_counts(self): + with new_request_context(): + contexts.identify_context("alice") + self._emit_span() + + exported = self.inner.on_end.call_args.args[0] + self.assertEqual(exported.dropped_attributes, 0) + self.assertEqual(exported.dropped_events, 0) + self.assertEqual(exported.dropped_links, 0) + + def test_exported_copy_keeps_counts_dropped_by_span_limits(self): + provider = TracerProvider( + span_limits=SpanLimits(max_attributes=2, max_events=1, max_links=1) + ) + provider.add_span_processor(self.processor) + tracer = provider.get_tracer("test") + linked = [ + Link(tracer.start_span(f"linked-{i}").get_span_context()) for i in range(3) + ] + + with new_request_context(): + contexts.identify_context("alice") + with tracer.start_as_current_span( + "gen_ai.chat", attributes={"a": 1, "b": 2, "c": 3}, links=linked + ) as span: + span.add_event("first") + span.add_event("second") + + exported = self.inner.on_end.call_args.args[0] + self.assertEqual(exported.attributes["posthog.distinct_id"], "alice") + self.assertEqual(exported.dropped_attributes, 1) + self.assertEqual(exported.dropped_events, 1) + self.assertEqual(exported.dropped_links, 2) + def test_overlapping_requests_export_their_own_values(self): both_started = threading.Barrier(2)