diff --git a/CHANGELOG.md b/CHANGELOG.md index d7ca4a32b..0aefdf8fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased](https://github.com/open-telemetry/opentelemetry-python/compare/v1.12.0rc1-0.31b0...HEAD) -- Pyramid: Only categorize 400s and 500s exceptions as errors +- Pyramid: Only categorize 500s server exceptions as errors ([#1037](https://github.com/open-telemetry/opentelemetry-python-contrib/issues/1037)) ### Fixed diff --git a/instrumentation/opentelemetry-instrumentation-pyramid/src/opentelemetry/instrumentation/pyramid/callbacks.py b/instrumentation/opentelemetry-instrumentation-pyramid/src/opentelemetry/instrumentation/pyramid/callbacks.py index e2b362f2c..0f36b17e5 100644 --- a/instrumentation/opentelemetry-instrumentation-pyramid/src/opentelemetry/instrumentation/pyramid/callbacks.py +++ b/instrumentation/opentelemetry-instrumentation-pyramid/src/opentelemetry/instrumentation/pyramid/callbacks.py @@ -15,7 +15,7 @@ from logging import getLogger from pyramid.events import BeforeTraversal -from pyramid.httpexceptions import HTTPError, HTTPException +from pyramid.httpexceptions import HTTPException, HTTPServerError from pyramid.settings import asbool from pyramid.tweens import EXCVIEW @@ -198,9 +198,9 @@ def trace_tween_factory(handler, registry): activation = request.environ.get(_ENVIRON_ACTIVATION_KEY) - # Only considering HTTPClientError and HTTPServerError - # to make sure HTTPRedirection is not reported as error - if isinstance(response, HTTPError): + # Only considering HTTPServerError + # to make sure 200, 300 and 400 exceptions are not reported as error + if isinstance(response, HTTPServerError): activation.__exit__( type(response), response, diff --git a/instrumentation/opentelemetry-instrumentation-pyramid/tests/pyramid_base_test.py b/instrumentation/opentelemetry-instrumentation-pyramid/tests/pyramid_base_test.py index 9804c2f99..8f97cf2db 100644 --- a/instrumentation/opentelemetry-instrumentation-pyramid/tests/pyramid_base_test.py +++ b/instrumentation/opentelemetry-instrumentation-pyramid/tests/pyramid_base_test.py @@ -24,6 +24,8 @@ class InstrumentationTest: helloid = int(request.matchdict["helloid"]) if helloid == 500: raise exc.HTTPInternalServerError() + if helloid == 404: + raise exc.HTTPNotFound() if helloid == 302: raise exc.HTTPFound() if helloid == 204: diff --git a/instrumentation/opentelemetry-instrumentation-pyramid/tests/test_automatic.py b/instrumentation/opentelemetry-instrumentation-pyramid/tests/test_automatic.py index ba6fbec19..b9edd44d7 100644 --- a/instrumentation/opentelemetry-instrumentation-pyramid/tests/test_automatic.py +++ b/instrumentation/opentelemetry-instrumentation-pyramid/tests/test_automatic.py @@ -136,6 +136,27 @@ class TestAutomatic(InstrumentationTest, TestBase, WsgiTestBase): span_list = self.memory_exporter.get_finished_spans() self.assertEqual(len(span_list), 1) + def test_400s_response_is_not_an_error(self): + tween_list = "pyramid.tweens.excview_tween_factory" + config = Configurator(settings={"pyramid.tweens": tween_list}) + self._common_initialization(config) + resp = self.client.get("/hello/404") + self.assertEqual(404, resp.status_code) + span_list = self.memory_exporter.get_finished_spans() + self.assertEqual(len(span_list), 1) + self.assertEqual(span_list[0].status.status_code, StatusCode.UNSET) + + PyramidInstrumentor().uninstrument() + + self.config = Configurator() + + self._common_initialization(self.config) + + resp = self.client.get("/hello/404") + self.assertEqual(404, resp.status_code) + span_list = self.memory_exporter.get_finished_spans() + self.assertEqual(len(span_list), 1) + class TestWrappedWithOtherFramework( InstrumentationTest, TestBase, WsgiTestBase