From 0a064d4e2b1f01510c04ec5fe18310d55697e7d9 Mon Sep 17 00:00:00 2001 From: Jonatas Emidio Date: Thu, 19 Nov 2026 08:31:39 +0000 Subject: [PATCH] fix(opentelemetry): handle DroppedSpan when transaction_max_spans is exceeded (#2245) --- elasticapm/contrib/opentelemetry/span.py | 36 ++++++++++++++++------- elasticapm/contrib/opentelemetry/trace.py | 4 ++- tests/contrib/opentelemetry/tests.py | 28 ++++++++++++++++++ 3 files changed, 56 insertions(+), 12 deletions(-) diff --git a/elasticapm/contrib/opentelemetry/span.py b/elasticapm/contrib/opentelemetry/span.py index 7917070d5..b17075353 100644 --- a/elasticapm/contrib/opentelemetry/span.py +++ b/elasticapm/contrib/opentelemetry/span.py @@ -73,7 +73,12 @@ def end(self, end_time: Optional[int] = None) -> None: Only the first call to `end` should modify the span, and implementations are free to ignore or raise on further calls. """ - is_transaction = isinstance(self.elastic_span, elasticapm.traces.Transaction) + elastic_span = self.elastic_span + if isinstance(elastic_span, elasticapm.traces.DroppedSpan): + if not elastic_span.ended_time: + elastic_span.end() + return + is_transaction = isinstance(elastic_span, elasticapm.traces.Transaction) if self.elastic_span.ended_time: # Already ended return @@ -100,14 +105,15 @@ def get_span_context(self) -> "SpanContext": Returns: A :class:`opentelemetry.trace.SpanContext` with a copy of this span's immutable state. """ + elastic_span = self.elastic_span + if isinstance(elastic_span, elasticapm.traces.DroppedSpan): + return SpanContext(trace_id=0, span_id=0, is_remote=False) return SpanContext( - trace_id=int(self.elastic_span.transaction.trace_parent.trace_id, base=16), - span_id=int(self.elastic_span.id, base=16), + trace_id=int(elastic_span.transaction.trace_parent.trace_id, base=16), + span_id=int(elastic_span.id, base=16), is_remote=False, - trace_flags=TraceFlags( - TraceFlags.SAMPLED if self.elastic_span.transaction.is_sampled else TraceFlags.DEFAULT - ), - trace_state=TraceState(list(self.elastic_span.transaction.trace_parent.tracestate_dict.items())), + trace_flags=TraceFlags(TraceFlags.SAMPLED if elastic_span.transaction.is_sampled else TraceFlags.DEFAULT), + trace_state=TraceState(list(elastic_span.transaction.trace_parent.tracestate_dict.items())), ) def set_attributes(self, attributes: typing.Dict[str, types.AttributeValue]) -> None: @@ -125,8 +131,11 @@ def set_attribute(self, key: str, value: types.AttributeValue) -> None: Sets a single Attribute with the key and value passed as arguments. Note: The behavior of `None` value attributes is undefined, and hence strongly discouraged. """ - if "otel_attributes" not in self.elastic_span.context: - self.elastic_span.context["otel_attributes"] = {} + span_context = self.elastic_span.context + if span_context is None: + return + if "otel_attributes" not in span_context: + span_context["otel_attributes"] = {} self.elastic_span.context["otel_attributes"][key] = value def add_event( @@ -148,14 +157,19 @@ def update_name(self, name: str) -> None: Upon this update, any sampling behavior based on Span name will depend on the implementation. """ - self.elastic_span.name = name + elastic_span = self.elastic_span + if not isinstance(elastic_span, elasticapm.traces.DroppedSpan): + elastic_span.name = name def is_recording(self) -> bool: """Returns whether this span will be recorded. Returns true if this Span is active and recording information like events with the add_event operation and attributes using set_attribute. """ - return self.elastic_span.transaction.is_sampled and not self.elastic_span.ended_time + elastic_span = self.elastic_span + if isinstance(elastic_span, elasticapm.traces.DroppedSpan): + return False + return elastic_span.transaction.is_sampled and not elastic_span.ended_time def set_status(self, status: Union[Status, StatusCode], description: Optional[str] = None) -> None: """Sets the Status of the Span. If used, this will override the default diff --git a/elasticapm/contrib/opentelemetry/trace.py b/elasticapm/contrib/opentelemetry/trace.py index 4f04310ef..8960ec352 100644 --- a/elasticapm/contrib/opentelemetry/trace.py +++ b/elasticapm/contrib/opentelemetry/trace.py @@ -166,7 +166,9 @@ def start_span( ) span.set_attributes(attributes) spankind = get_span_kind(kind) - elastic_span.context["otel_spankind"] = spankind + span_context = elastic_span.context + if span_context is not None: + span_context["otel_spankind"] = spankind return span diff --git a/tests/contrib/opentelemetry/tests.py b/tests/contrib/opentelemetry/tests.py index aa0160b74..21b7423bc 100755 --- a/tests/contrib/opentelemetry/tests.py +++ b/tests/contrib/opentelemetry/tests.py @@ -203,4 +203,32 @@ def test_set_status_on_span(tracer: Tracer): assert span_event["outcome"] == "failure" +@pytest.mark.parametrize("elasticapm_client", [{"transaction_max_spans": 5}], indirect=True) +def test_ot_dropped_spans_exceeding_transaction_max_spans(elasticapm_client, tracer: Tracer): + def capture_nested(steps: int) -> None: + if steps == 0: + return + with tracer.start_as_current_span( + f"step-{steps}", + kind=SpanKind.CLIENT, + attributes={"http.url": "http://example.com"}, + ) as span: + if steps <= 3: + assert not span.is_recording() + assert not span.get_span_context().is_valid + span.set_attribute("extra", "ignored") + span.update_name("renamed") + span.set_status(StatusCode.ERROR) + capture_nested(steps - 1) + + elasticapm_client.begin_transaction("processors") + capture_nested(8) + elasticapm_client.end_transaction("myapp.billing_process") + + assert len(elasticapm_client.events[constants.SPAN]) == 5 + tx = elasticapm_client.events[constants.TRANSACTION][0] + assert tx["span_count"]["started"] == 5 + assert tx["span_count"]["dropped"] == 3 + + # TODO Add some span subtype testing?