From 716e3d6ef15a6ec25b59068c797b992e8e6f0703 Mon Sep 17 00:00:00 2001 From: GonzaloGuasch Date: Mon, 23 Dec 2024 16:39:14 -0300 Subject: [PATCH 1/4] remove span is_recording method --- .../src/opentelemetry/instrumentation/wsgi/__init__.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-wsgi/src/opentelemetry/instrumentation/wsgi/__init__.py b/instrumentation/opentelemetry-instrumentation-wsgi/src/opentelemetry/instrumentation/wsgi/__init__.py index eb7cbced9c..bd3b2d18db 100644 --- a/instrumentation/opentelemetry-instrumentation-wsgi/src/opentelemetry/instrumentation/wsgi/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-wsgi/src/opentelemetry/instrumentation/wsgi/__init__.py @@ -480,11 +480,7 @@ def add_response_attributes( """Adds HTTP response attributes to span using the arguments passed to a PEP3333-conforming start_response callable. """ - if not span.is_recording(): - return status_code_str, _ = start_response_status.split(" ", 1) - - status_code = 0 try: status_code = int(status_code_str) except ValueError: From 9252debbdefa9ca1c64a3017081586c4e1a55071 Mon Sep 17 00:00:00 2001 From: GonzaloGuasch Date: Mon, 23 Dec 2024 17:03:14 -0300 Subject: [PATCH 2/4] update changelog --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d17c18687c..e3c065dd70 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,7 +19,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#3111](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3111)) - `opentelemetry-instrumentation-falcon` add support version to v4 ([#3086](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3086)) - +- `opentelemetry-instrumentation-wsgi` remove is_recording method to record status code in all metrics + ([#3148](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3148)) ### Fixed From 4d908453e7d50a918a357c5c3ca84bfca064b613 Mon Sep 17 00:00:00 2001 From: GonzaloGuasch Date: Thu, 26 Dec 2024 12:46:12 -0300 Subject: [PATCH 3/4] update changelog && add test --- CHANGELOG.md | 2 +- .../tests/test_wsgi_middleware.py | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e3c065dd70..19a74422c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,7 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#3111](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3111)) - `opentelemetry-instrumentation-falcon` add support version to v4 ([#3086](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3086)) -- `opentelemetry-instrumentation-wsgi` remove is_recording method to record status code in all metrics +- `opentelemetry-instrumentation-wsgi` always record span status code to have it available in metrics ([#3148](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3148)) ### Fixed diff --git a/instrumentation/opentelemetry-instrumentation-wsgi/tests/test_wsgi_middleware.py b/instrumentation/opentelemetry-instrumentation-wsgi/tests/test_wsgi_middleware.py index da1a3c2696..bb1db4908a 100644 --- a/instrumentation/opentelemetry-instrumentation-wsgi/tests/test_wsgi_middleware.py +++ b/instrumentation/opentelemetry-instrumentation-wsgi/tests/test_wsgi_middleware.py @@ -779,6 +779,18 @@ def test_response_attributes(self): self.span.set_attribute.assert_has_calls(expected, any_order=True) self.span.set_attribute.assert_has_calls(expected_new, any_order=True) + def test_response_attributes_noop(self): + mock_span = mock.Mock() + mock_span.is_recording.return_value = False + + attrs = {} + otel_wsgi.add_response_attributes(mock_span, "404 Not Found", {}, duration_attrs=attrs) + + self.assertEqual(mock_span.set_attribute.call_count, 0) + self.assertEqual(mock_span.is_recording.call_count, 2) + self.assertEqual(attrs[SpanAttributes.HTTP_STATUS_CODE], 404) + + def test_credential_removal(self): self.environ["HTTP_HOST"] = "username:password@mock" self.environ["PATH_INFO"] = "/status/200" From adeb7e9068e040a8a554fa2074a1a152a5c14ea5 Mon Sep 17 00:00:00 2001 From: GonzaloGuasch Date: Thu, 26 Dec 2024 12:51:32 -0300 Subject: [PATCH 4/4] reformat test --- .../tests/test_wsgi_middleware.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-wsgi/tests/test_wsgi_middleware.py b/instrumentation/opentelemetry-instrumentation-wsgi/tests/test_wsgi_middleware.py index bb1db4908a..9d7d1240a7 100644 --- a/instrumentation/opentelemetry-instrumentation-wsgi/tests/test_wsgi_middleware.py +++ b/instrumentation/opentelemetry-instrumentation-wsgi/tests/test_wsgi_middleware.py @@ -784,13 +784,14 @@ def test_response_attributes_noop(self): mock_span.is_recording.return_value = False attrs = {} - otel_wsgi.add_response_attributes(mock_span, "404 Not Found", {}, duration_attrs=attrs) + otel_wsgi.add_response_attributes( + mock_span, "404 Not Found", {}, duration_attrs=attrs + ) self.assertEqual(mock_span.set_attribute.call_count, 0) self.assertEqual(mock_span.is_recording.call_count, 2) self.assertEqual(attrs[SpanAttributes.HTTP_STATUS_CODE], 404) - def test_credential_removal(self): self.environ["HTTP_HOST"] = "username:password@mock" self.environ["PATH_INFO"] = "/status/200"