Skip to content

Commit 8e7f1a8

Browse files
authored
[Core] Add method for setting span status (#40703)
Spans started by `OpenTelemetryTracer` in core return an OpenTelemetry span. The set_status on this method requires that a user imports `opentelemetry` types for `StatusCode` enums. We want to avoid SDK developers needing to import `opentelemetry` in order to set a span status, however in all cases so far where a user is setting a status, it's to set it to ERROR. Here, we are adding a utility method on the `OpenTelemetryTracer` class to enable setting an error status on a provided span. Signed-off-by: Paul Van Eck <[email protected]>
1 parent 08e0cd8 commit 8e7f1a8

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

sdk/core/azure-core/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
### Features Added
66

7+
- Added a `set_span_error_status` method to the `OpenTelemetryTracer` class. This method allows users to set the status of a span to `ERROR` after it has been created. #40703
8+
79
### Breaking Changes
810

911
### Bugs Fixed

sdk/core/azure-core/azure/core/tracing/opentelemetry.py

+12
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
Span,
1313
SpanKind as OpenTelemetrySpanKind,
1414
Link as OpenTelemetryLink,
15+
StatusCode,
1516
)
1617
from opentelemetry.trace.propagation import get_current_span as get_current_span_otel
1718
from opentelemetry.propagate import extract, inject
@@ -161,6 +162,17 @@ def use_span(cls, span: Span, *, end_on_exit: bool = True) -> Iterator[Span]:
161162
) as active_span:
162163
yield active_span
163164

165+
@staticmethod
166+
def set_span_error_status(span: Span, description: Optional[str] = None) -> None:
167+
"""Set the status of a span to ERROR with the provided description, if any.
168+
169+
:param span: The span to set the ERROR status on.
170+
:type span: ~opentelemetry.trace.Span
171+
:param description: An optional description of the error.
172+
:type description: str
173+
"""
174+
span.set_status(StatusCode.ERROR, description=description)
175+
164176
def _parse_links(self, links: Optional[Sequence[Link]]) -> Optional[Sequence[OpenTelemetryLink]]:
165177
if not links:
166178
return None

sdk/core/azure-core/tests/test_tracer_otel.py

+22
Original file line numberDiff line numberDiff line change
@@ -354,3 +354,25 @@ def test_tracer_caching_different_args():
354354

355355
assert tracer1 is tracer2
356356
assert tracer1 is not tracer3
357+
358+
359+
def test_tracer_set_span_error(tracing_helper):
360+
"""Test that the tracer's set_span_status method works correctly."""
361+
tracer = get_tracer()
362+
assert tracer
363+
364+
with tracer.start_as_current_span(name="ok-span") as span:
365+
tracer.set_span_error_status(span)
366+
367+
with tracer.start_as_current_span(name="ok-span") as span:
368+
tracer.set_span_error_status(span, "This is an error")
369+
370+
# Verify status on finished spans
371+
finished_spans = tracing_helper.exporter.get_finished_spans()
372+
assert len(finished_spans) == 2
373+
374+
assert finished_spans[0].status.status_code == OtelStatusCode.ERROR
375+
assert finished_spans[0].status.description is None
376+
377+
assert finished_spans[1].status.status_code == OtelStatusCode.ERROR
378+
assert finished_spans[1].status.description == "This is an error"

0 commit comments

Comments
 (0)