Skip to content

Commit 3ac4699

Browse files
fix: resolve lint and formatting issues in _semconv.py and tests
1 parent d712f06 commit 3ac4699

2 files changed

Lines changed: 55 additions & 37 deletions

File tree

opentelemetry-instrumentation/src/opentelemetry/instrumentation/_semconv.py

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,26 @@ def _set_db_operation(
610610

611611

612612
# General
613+
def _set_span_status(span: Span, status: StatusCode) -> None:
614+
status_priority = {
615+
StatusCode.UNSET: 0,
616+
StatusCode.OK: 1,
617+
StatusCode.ERROR: 2,
618+
}
619+
current = getattr(span, "status", None)
620+
if current is not None:
621+
if status_priority.get(status, 0) < status_priority.get(
622+
current.status_code, 0
623+
):
624+
return
625+
description = (
626+
current.description
627+
if current.description and status == current.status_code
628+
else None
629+
)
630+
span.set_status(Status(status, description))
631+
else:
632+
span.set_status(Status(status))
613633

614634

615635
def _set_status(
@@ -650,28 +670,7 @@ def _set_status(
650670
span.set_attribute(ERROR_TYPE, status_code_str)
651671
metrics_attributes[ERROR_TYPE] = status_code_str
652672
if span.is_recording():
653-
current_status = getattr(span, "status", None)
654-
if current_status is not None:
655-
_STATUS_PRIORITY = {
656-
StatusCode.UNSET: 0,
657-
StatusCode.OK: 1,
658-
StatusCode.ERROR: 2,
659-
}
660-
current_priority = _STATUS_PRIORITY.get(
661-
current_status.status_code, 0
662-
)
663-
incoming_priority = _STATUS_PRIORITY.get(status, 0)
664-
if incoming_priority < current_priority:
665-
return
666-
description = (
667-
current_status.description
668-
if current_status.description
669-
and status == current_status.status_code
670-
else None
671-
)
672-
span.set_status(Status(status, description))
673-
else:
674-
span.set_status(Status(status))
673+
_set_span_status(span, status)
675674

676675

677676
def _get_schema_url(mode: _StabilityMode) -> str:

opentelemetry-instrumentation/tests/test_semconv_status.py

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88

99
import unittest
1010
from unittest.mock import MagicMock
11-
from opentelemetry.trace.status import Status, StatusCode
11+
1212
from opentelemetry.instrumentation._semconv import (
1313
_set_status,
1414
_StabilityMode,
1515
)
16+
from opentelemetry.trace.status import Status, StatusCode
1617

1718

1819
class TestSetStatus(unittest.TestCase):
19-
2020
def _make_span(self, status_code, description=None):
2121
span = MagicMock()
2222
span.is_recording.return_value = True
@@ -26,9 +26,14 @@ def _make_span(self, status_code, description=None):
2626
def test_does_not_downgrade_error_to_ok(self):
2727
"""ERROR status should not be overridden by a lower priority OK"""
2828
span = self._make_span(StatusCode.ERROR, "original error")
29-
_set_status(span, {}, 200, "200", server_span=True,
30-
sem_conv_opt_in_mode=_StabilityMode.DEFAULT)
31-
# set_status should NOT have been called (no downgrade)
29+
_set_status(
30+
span,
31+
{},
32+
200,
33+
"200",
34+
server_span=True,
35+
sem_conv_opt_in_mode=_StabilityMode.DEFAULT,
36+
)
3237
for call in span.set_status.call_args_list:
3338
args = call[0]
3439
if args:
@@ -37,8 +42,14 @@ def test_does_not_downgrade_error_to_ok(self):
3742
def test_does_not_wipe_description_with_none(self):
3843
"""Same ERROR status should preserve existing description"""
3944
span = self._make_span(StatusCode.ERROR, "keep this message")
40-
_set_status(span, {}, 500, "500", server_span=True,
41-
sem_conv_opt_in_mode=_StabilityMode.DEFAULT)
45+
_set_status(
46+
span,
47+
{},
48+
500,
49+
"500",
50+
server_span=True,
51+
sem_conv_opt_in_mode=_StabilityMode.DEFAULT,
52+
)
4253
last_call = span.set_status.call_args
4354
if last_call:
4455
status_arg = last_call[0][0]
@@ -47,19 +58,27 @@ def test_does_not_wipe_description_with_none(self):
4758
def test_upgrades_unset_to_error(self):
4859
"""UNSET status should be upgraded to ERROR"""
4960
span = self._make_span(StatusCode.UNSET)
50-
_set_status(span, {}, 500, "500", server_span=True,
51-
sem_conv_opt_in_mode=_StabilityMode.DEFAULT)
61+
_set_status(
62+
span,
63+
{},
64+
500,
65+
"500",
66+
server_span=True,
67+
sem_conv_opt_in_mode=_StabilityMode.DEFAULT,
68+
)
5269
span.set_status.assert_called()
5370
last_call = span.set_status.call_args[0][0]
5471
self.assertEqual(last_call.status_code, StatusCode.ERROR)
5572

5673
def test_unset_to_ok(self):
5774
"""UNSET status should be upgraded to OK for 2xx"""
5875
span = self._make_span(StatusCode.UNSET)
59-
_set_status(span, {}, 200, "200", server_span=False,
60-
sem_conv_opt_in_mode=_StabilityMode.DEFAULT)
76+
_set_status(
77+
span,
78+
{},
79+
200,
80+
"200",
81+
server_span=False,
82+
sem_conv_opt_in_mode=_StabilityMode.DEFAULT,
83+
)
6184
span.set_status.assert_called()
62-
63-
64-
if __name__ == "__main__":
65-
unittest.main()

0 commit comments

Comments
 (0)