Skip to content

Commit 91325b4

Browse files
qvalentinxrmx
andauthored
fix(contrib/opentelemetry): set_status should match base signature (#2457)
* fix(contrib/opentelemetry): set_status should match base signature fixes #2455 Signed-off-by: qvalentin <[email protected]> * Apply suggestions from code review --------- Signed-off-by: qvalentin <[email protected]> Co-authored-by: Riccardo Magliocchetti <[email protected]>
1 parent 7d093af commit 91325b4

File tree

2 files changed

+58
-4
lines changed

2 files changed

+58
-4
lines changed

elasticapm/contrib/opentelemetry/span.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import types as python_types
3333
import typing
3434
import urllib.parse
35-
from typing import Optional
35+
from typing import Optional, Union
3636

3737
from opentelemetry.context import Context
3838
from opentelemetry.sdk import trace as oteltrace
@@ -157,13 +157,19 @@ def is_recording(self) -> bool:
157157
"""
158158
return self.elastic_span.transaction.is_sampled and not self.elastic_span.ended_time
159159

160-
def set_status(self, status: Status) -> None:
160+
def set_status(self, status: Union[Status, StatusCode], description: Optional[str] = None) -> None:
161161
"""Sets the Status of the Span. If used, this will override the default
162162
Span status.
163163
"""
164-
if status.status_code == StatusCode.ERROR:
164+
# Handle both Status objects and StatusCode enums
165+
if isinstance(status, Status):
166+
status_code = status.status_code
167+
else:
168+
status_code = status
169+
170+
if status_code == StatusCode.ERROR:
165171
self.elastic_span.outcome = constants.OUTCOME.FAILURE
166-
elif status.status_code == StatusCode.OK:
172+
elif status_code == StatusCode.OK:
167173
self.elastic_span.outcome = constants.OUTCOME.SUCCESS
168174
else:
169175
self.elastic_span.outcome = constants.OUTCOME.UNKNOWN

tests/contrib/opentelemetry/tests.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
from opentelemetry.trace import Link, SpanContext, SpanKind, TraceFlags
3636
from opentelemetry.trace.propagation import _SPAN_KEY
37+
from opentelemetry.trace.status import Status, StatusCode
3738

3839
import elasticapm.contrib.opentelemetry.context as context
3940
import elasticapm.contrib.opentelemetry.trace as trace
@@ -155,4 +156,51 @@ def test_span_links(tracer: Tracer):
155156
assert span["links"][0]["span_id"] == "0011223344556677"
156157

157158

159+
def test_set_status_with_status_object(tracer: Tracer):
160+
with tracer.start_as_current_span("test") as span:
161+
span.set_status(Status(StatusCode.OK))
162+
163+
client = tracer.client
164+
transaction = client.events[constants.TRANSACTION][0]
165+
assert transaction["outcome"] == "success"
166+
167+
168+
def test_set_status_with_status_code(tracer: Tracer):
169+
with tracer.start_as_current_span("test") as span:
170+
span.set_status(StatusCode.ERROR)
171+
172+
client = tracer.client
173+
transaction = client.events[constants.TRANSACTION][0]
174+
assert transaction["outcome"] == "failure"
175+
176+
177+
def test_set_status_with_status_code_and_description(tracer: Tracer):
178+
with tracer.start_as_current_span("test") as span:
179+
span.set_status(StatusCode.OK, "Everything is fine")
180+
181+
client = tracer.client
182+
transaction = client.events[constants.TRANSACTION][0]
183+
assert transaction["outcome"] == "success"
184+
185+
186+
def test_set_status_unset(tracer: Tracer):
187+
with tracer.start_as_current_span("test") as span:
188+
span.set_status(StatusCode.UNSET)
189+
190+
client = tracer.client
191+
transaction = client.events[constants.TRANSACTION][0]
192+
assert transaction["outcome"] == "unknown"
193+
194+
195+
def test_set_status_on_span(tracer: Tracer):
196+
"""Test set_status on a child span (not transaction)"""
197+
with tracer.start_as_current_span("test"):
198+
with tracer.start_as_current_span("testspan") as span:
199+
span.set_status(StatusCode.ERROR)
200+
201+
client = tracer.client
202+
span_event = client.events[constants.SPAN][0]
203+
assert span_event["outcome"] == "failure"
204+
205+
158206
# TODO Add some span subtype testing?

0 commit comments

Comments
 (0)