Skip to content

fix: keep dropped counts on spans copied for PostHog context - #1266

Open
fahad2932 wants to merge 1 commit into
PostHog:mainfrom
fahad2932:fix/otel-context-copy-older-encoders
Open

fahad2932 wants to merge 1 commit into
PostHog:mainfrom
fahad2932:fix/otel-context-copy-older-encoders

Conversation

@fahad2932

Copy link
Copy Markdown

When a span ends inside a PostHog context (identify_context, set_context_session, tag), with_request_context rebuilds it as a ReadableSpan to add the context values. The copy was given a plain dict for attributes and the original tuples for events and links.

On opentelemetry-sdk 1.20 and 1.21, which pyproject.toml still allows (opentelemetry-sdk>=1.20.0), the OTLP encoder reads .dropped from those collections, so export fails and the batch is lost:

AttributeError: 'dict' object has no attribute 'dropped'

On newer SDKs export works, but the copy reports dropped_attributes, dropped_events and dropped_links as 0 even when the SDK dropped some under its span limits.

The copy now uses BoundedAttributes and BoundedList, without limits so nothing is truncated again, and carries the original span's dropped counts over. Spans with nothing to add are still returned untouched.

💚 How did you test it?

  • Two tests in test_processor.py: the exported copy reports dropped counts, and keeps the counts the SDK dropped under SpanLimits. Both fail without the change.
  • pytest posthog/test/ai/otel on opentelemetry-sdk 1.20.0 and 1.45.2.
  • Exported a span started inside a context to a local OTLP endpoint on 1.20.0 and 1.45.2. Before the change, 1.20.0 sent nothing; after, both deliver it.
  • pytest --timeout=30: 7332 passed, 64 skipped.
  • ruff format --check, ruff check, and mypy --no-site-packages --config-file mypy.ini . | mypy-baseline filter: clean.

📝 Checklist

  • All commits are signed (see Commit signing)
  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change or entry added to the changelog.

If releasing new changes

  • Ran sampo add to generate a changeset file

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

  • Claude Code helped reproduce the failure across SDK versions and draft the tests.
  • Considered only wrapping the attributes; events and links hit the same .dropped read, so all three are wrapped, and the dropped counts are copied so the newer-SDK case doesn't report 0.

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.
@fahad2932
fahad2932 requested a review from a team as a code owner November 16, 2026 23:45

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant