Skip to content

Commit 95f14cd

Browse files
wsgi: always record span status code to have it available in metrics (#3148)
1 parent e5eb524 commit 95f14cd

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2323
([#3133](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3133))
2424
- `opentelemetry-instrumentation-falcon` add support version to v4
2525
([#3086](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3086))
26+
- `opentelemetry-instrumentation-wsgi` always record span status code to have it available in metrics
27+
([#3148](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3148))
2628
- add support to Python 3.13
2729
([#3134](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3134))
2830

instrumentation/opentelemetry-instrumentation-wsgi/src/opentelemetry/instrumentation/wsgi/__init__.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -480,11 +480,7 @@ def add_response_attributes(
480480
"""Adds HTTP response attributes to span using the arguments
481481
passed to a PEP3333-conforming start_response callable.
482482
"""
483-
if not span.is_recording():
484-
return
485483
status_code_str, _ = start_response_status.split(" ", 1)
486-
487-
status_code = 0
488484
try:
489485
status_code = int(status_code_str)
490486
except ValueError:

instrumentation/opentelemetry-instrumentation-wsgi/tests/test_wsgi_middleware.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,19 @@ def test_response_attributes(self):
779779
self.span.set_attribute.assert_has_calls(expected, any_order=True)
780780
self.span.set_attribute.assert_has_calls(expected_new, any_order=True)
781781

782+
def test_response_attributes_noop(self):
783+
mock_span = mock.Mock()
784+
mock_span.is_recording.return_value = False
785+
786+
attrs = {}
787+
otel_wsgi.add_response_attributes(
788+
mock_span, "404 Not Found", {}, duration_attrs=attrs
789+
)
790+
791+
self.assertEqual(mock_span.set_attribute.call_count, 0)
792+
self.assertEqual(mock_span.is_recording.call_count, 2)
793+
self.assertEqual(attrs[SpanAttributes.HTTP_STATUS_CODE], 404)
794+
782795
def test_credential_removal(self):
783796
self.environ["HTTP_HOST"] = "username:password@mock"
784797
self.environ["PATH_INFO"] = "/status/200"

0 commit comments

Comments
 (0)