Skip to content

fix(opentelemetry): handle DroppedSpan when transaction_max_spans is exceeded - #2789

Open
jonatasemidio wants to merge 1 commit into
elastic:mainfrom
jonatasemidio:fix/otel-dropped-span-2245
Open

jonatasemidio wants to merge 1 commit into
elastic:mainfrom
jonatasemidio:fix/otel-dropped-span-2245

Conversation

@jonatasemidio

Copy link
Copy Markdown

What does this pull request do?

Hi team! 👋

When an active transaction exceeds ELASTIC_APM_TRANSACTION_MAX_SPANS, current_transaction.begin_span(...) returns a DroppedSpan (where context and id are None, name is a read-only property, and .transaction is not set).

Previously, creating or ending spans through the OpenTelemetry bridge (elasticapm.contrib.opentelemetry) crashed once the span limit was reached:

  • Tracer.start_span attempted elastic_span.context["otel_spankind"] = spankind (TypeError: 'NoneType' object does not support item assignment).
  • Span.set_attribute attempted "otel_attributes" not in self.elastic_span.context when attributes were passed (TypeError).
  • Span.end called _set_types() on exit (AttributeError on None context), and Span.get_span_context / Span.is_recording / Span.update_name assumed a recorded Span or Transaction.

This PR updates Tracer.start_span and Span so DroppedSpan instances behave as safe non-recording spans—matching how elasticapm.capture_span() handles dropped spans:

  • Skip otel_spankind context assignment in Tracer.start_span and attribute writes in Span.set_attribute when context is None.
  • Finish DroppedSpan directly in Span.end() (preserving execution_context cleanup and repeat-end() idempotency) without calling _set_types().
  • Return an invalid SpanContext (trace_id=0, span_id=0, is_remote=False) from get_span_context(), False from is_recording(), and guard update_name() on DroppedSpan.
  • Add unit tests in tests/contrib/opentelemetry/tests.py covering nested spans, attributes, and Span methods past transaction_max_spans.

Related issues

Closes #2245

@jonatasemidio
jonatasemidio requested a review from a team as a code owner November 19, 2026 03:46
@cla-checker-service

cla-checker-service Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

💚 CLA has been signed

@github-actions

Copy link
Copy Markdown

🤖 GitHub comments

Just comment with:

  • run docs-build : Re-trigger the docs validation. (use unformatted text in the comment!)

@github-actions github-actions Bot added agent-python community Issues opened by the community triage Issues awaiting triage labels Nov 19, 2026
@jonatasemidio
jonatasemidio force-pushed the fix/otel-dropped-span-2245 branch from a02bdfe to cacd6ae Compare November 19, 2026 03:52
@jonatasemidio
jonatasemidio force-pushed the fix/otel-dropped-span-2245 branch from cacd6ae to 0a064d4 Compare November 19, 2026 08:31
@jonatasemidio

Copy link
Copy Markdown
Author

run docs-build : Re-trigger the docs validation.

@xrmx

xrmx commented Nov 19, 2026

Copy link
Copy Markdown
Member

@jonatasemidio Hey Jonatas, thanks for your PR. Did you hit this issue yourself or are you looking after some open source contribution experience?

@jonatasemidio

Copy link
Copy Markdown
Author

Hi @xrmx

Thanks for checking!

To be transparent, it’s a bit of both... We’re validating this tool in POCs for an internal agentic reconciliation pipeline, and we ran into this during testing. Since we’d like to rely on it in our flow without waiting on the normal backlog, I also saw this as a nice opportunity to contribute something useful back.

@xrmx

xrmx commented Nov 19, 2026

Copy link
Copy Markdown
Member

Hi @xrmx

Thanks for checking!

To be transparent, it’s a bit of both... We’re validating this tool in POCs for an internal agentic reconciliation pipeline, and we ran into this during testing. Since we’d like to rely on it in our flow without waiting on the normal backlog, I also saw this as a nice opportunity to contribute something useful back.

As I wrote in the issue if you are starting something new I would go all-in on OpenTelemetry with something like our distribution of OpenTelemetry Python instead https://github.com/elastic/elastic-otel-python

@jonatasemidio

Copy link
Copy Markdown
Author

Hi @xrmx

That makes total sense, and I'm not sure how this slipped past our radar, so thanks for the honest heads-up! I'll pivot our POC to OpenTelemetry with elastic-otel-python instead. Thank you very much!

Also, I have an internal static verification tool that helped me spot this issue, and I suspect other Elastic Python projects might have similar ones. Since I'd love to contribute real fixes with tests, do you have any suggestions?

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

agent-python community Issues opened by the community triage Issues awaiting triage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

opentelemetry.tracer crashes with TypeError in transaction with high number of spans

2 participants