Skip to content

Commit 4ba6214

Browse files
authored
pyramid: Only categorize 400s and 500s exceptions as errors (#1136)
* pyramid: Only categorize 400s and 500s exceptions as errors * Added changelog entry for the fix * Added a test for 204
1 parent 904f2f1 commit 4ba6214

File tree

4 files changed

+53
-2
lines changed

4 files changed

+53
-2
lines changed

Diff for: CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88
## [Unreleased](https://github.com/open-telemetry/opentelemetry-python/compare/v1.12.0rc1-0.31b0...HEAD)
9+
- Pyramid: Only categorize 400s and 500s exceptions as errors
10+
([#1037](https://github.com/open-telemetry/opentelemetry-python-contrib/issues/1037))
911

1012
### Fixed
1113
- Fix bug in system metrics by checking their configuration

Diff for: instrumentation/opentelemetry-instrumentation-pyramid/src/opentelemetry/instrumentation/pyramid/callbacks.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from logging import getLogger
1616

1717
from pyramid.events import BeforeTraversal
18-
from pyramid.httpexceptions import HTTPException
18+
from pyramid.httpexceptions import HTTPError, HTTPException
1919
from pyramid.settings import asbool
2020
from pyramid.tweens import EXCVIEW
2121

@@ -198,7 +198,9 @@ def trace_tween(request):
198198

199199
activation = request.environ.get(_ENVIRON_ACTIVATION_KEY)
200200

201-
if isinstance(response, HTTPException):
201+
# Only considering HTTPClientError and HTTPServerError
202+
# to make sure HTTPRedirection is not reported as error
203+
if isinstance(response, HTTPError):
202204
activation.__exit__(
203205
type(response),
204206
response,

Diff for: instrumentation/opentelemetry-instrumentation-pyramid/tests/pyramid_base_test.py

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ def _hello_endpoint(request):
2424
helloid = int(request.matchdict["helloid"])
2525
if helloid == 500:
2626
raise exc.HTTPInternalServerError()
27+
if helloid == 302:
28+
raise exc.HTTPFound()
29+
if helloid == 204:
30+
raise exc.HTTPNoContent()
2731
if helloid == 900:
2832
raise NotImplementedError()
2933
return Response("Hello: " + str(helloid))

Diff for: instrumentation/opentelemetry-instrumentation-pyramid/tests/test_automatic.py

+43
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from opentelemetry.test.test_base import TestBase
2323
from opentelemetry.test.wsgitestutil import WsgiTestBase
2424
from opentelemetry.trace import SpanKind
25+
from opentelemetry.trace.status import StatusCode
2526
from opentelemetry.util.http import (
2627
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST,
2728
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE,
@@ -93,6 +94,48 @@ def test_registry_name_is_this_module(self):
9394
config.registry.__name__, __name__.rsplit(".", maxsplit=1)[0]
9495
)
9596

97+
def test_redirect_response_is_not_an_error(self):
98+
tween_list = "pyramid.tweens.excview_tween_factory"
99+
config = Configurator(settings={"pyramid.tweens": tween_list})
100+
self._common_initialization(config)
101+
resp = self.client.get("/hello/302")
102+
self.assertEqual(302, resp.status_code)
103+
span_list = self.memory_exporter.get_finished_spans()
104+
self.assertEqual(len(span_list), 1)
105+
self.assertEqual(span_list[0].status.status_code, StatusCode.UNSET)
106+
107+
PyramidInstrumentor().uninstrument()
108+
109+
self.config = Configurator()
110+
111+
self._common_initialization(self.config)
112+
113+
resp = self.client.get("/hello/302")
114+
self.assertEqual(302, resp.status_code)
115+
span_list = self.memory_exporter.get_finished_spans()
116+
self.assertEqual(len(span_list), 1)
117+
118+
def test_204_empty_response_is_not_an_error(self):
119+
tween_list = "pyramid.tweens.excview_tween_factory"
120+
config = Configurator(settings={"pyramid.tweens": tween_list})
121+
self._common_initialization(config)
122+
resp = self.client.get("/hello/204")
123+
self.assertEqual(204, resp.status_code)
124+
span_list = self.memory_exporter.get_finished_spans()
125+
self.assertEqual(len(span_list), 1)
126+
self.assertEqual(span_list[0].status.status_code, StatusCode.UNSET)
127+
128+
PyramidInstrumentor().uninstrument()
129+
130+
self.config = Configurator()
131+
132+
self._common_initialization(self.config)
133+
134+
resp = self.client.get("/hello/204")
135+
self.assertEqual(204, resp.status_code)
136+
span_list = self.memory_exporter.get_finished_spans()
137+
self.assertEqual(len(span_list), 1)
138+
96139

97140
class TestWrappedWithOtherFramework(
98141
InstrumentationTest, TestBase, WsgiTestBase

0 commit comments

Comments
 (0)