Skip to content
Open
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
36 changes: 25 additions & 11 deletions elasticapm/contrib/opentelemetry/span.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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(
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion elasticapm/contrib/opentelemetry/trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
28 changes: 28 additions & 0 deletions tests/contrib/opentelemetry/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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?