Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .sampo/changesets/fix-otel-context-export-older-sdk.md
Original file line number Diff line number Diff line change
@@ -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.
16 changes: 13 additions & 3 deletions posthog/ai/otel/_request_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
37 changes: 36 additions & 1 deletion posthog/test/ai/otel/test_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
Loading