Pyramid: only categorize 500 exceptions as errors (#1137)

This commit is contained in:
Nina Stawski
2022-06-16 15:07:34 -07:00
committed by GitHub
parent f03bef2579
commit 62e0a31ff9
4 changed files with 28 additions and 5 deletions

View File

@ -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). 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) ## [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)) ([#1037](https://github.com/open-telemetry/opentelemetry-python-contrib/issues/1037))
### Fixed ### Fixed

View File

@ -15,7 +15,7 @@
from logging import getLogger from logging import getLogger
from pyramid.events import BeforeTraversal from pyramid.events import BeforeTraversal
from pyramid.httpexceptions import HTTPError, HTTPException from pyramid.httpexceptions import HTTPException, HTTPServerError
from pyramid.settings import asbool from pyramid.settings import asbool
from pyramid.tweens import EXCVIEW from pyramid.tweens import EXCVIEW
@ -198,9 +198,9 @@ def trace_tween_factory(handler, registry):
activation = request.environ.get(_ENVIRON_ACTIVATION_KEY) activation = request.environ.get(_ENVIRON_ACTIVATION_KEY)
# Only considering HTTPClientError and HTTPServerError # Only considering HTTPServerError
# to make sure HTTPRedirection is not reported as error # to make sure 200, 300 and 400 exceptions are not reported as error
if isinstance(response, HTTPError): if isinstance(response, HTTPServerError):
activation.__exit__( activation.__exit__(
type(response), type(response),
response, response,

View File

@ -24,6 +24,8 @@ class InstrumentationTest:
helloid = int(request.matchdict["helloid"]) helloid = int(request.matchdict["helloid"])
if helloid == 500: if helloid == 500:
raise exc.HTTPInternalServerError() raise exc.HTTPInternalServerError()
if helloid == 404:
raise exc.HTTPNotFound()
if helloid == 302: if helloid == 302:
raise exc.HTTPFound() raise exc.HTTPFound()
if helloid == 204: if helloid == 204:

View File

@ -136,6 +136,27 @@ class TestAutomatic(InstrumentationTest, TestBase, WsgiTestBase):
span_list = self.memory_exporter.get_finished_spans() span_list = self.memory_exporter.get_finished_spans()
self.assertEqual(len(span_list), 1) 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( class TestWrappedWithOtherFramework(
InstrumentationTest, TestBase, WsgiTestBase InstrumentationTest, TestBase, WsgiTestBase