Skip to content

Commit 8373aa9

Browse files
committed
fix(contrib/opentelemetry): set_status should match base signature
fixes #2455 Signed-off-by: qvalentin <valentin.theodor@web.de>
1 parent a42741f commit 8373aa9

File tree

2 files changed

+60
-5
lines changed

2 files changed

+60
-5
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: 50 additions & 1 deletion
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,52 @@ def test_span_links(tracer: Tracer):
155156
assert span["links"][0]["span_id"] == "0011223344556677"
156157

157158

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

0 commit comments

Comments
 (0)