Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pyramid: Only categorize 400s and 500s exceptions as errors #1136

Merged
merged 3 commits into from
Jun 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ 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
([#1037](https://github.com/open-telemetry/opentelemetry-python-contrib/issues/1037))

### Fixed
- Fix bug in system metrics by checking their configuration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from logging import getLogger

from pyramid.events import BeforeTraversal
from pyramid.httpexceptions import HTTPException
from pyramid.httpexceptions import HTTPError, HTTPException
from pyramid.settings import asbool
from pyramid.tweens import EXCVIEW

Expand Down Expand Up @@ -198,7 +198,9 @@ def trace_tween(request):

activation = request.environ.get(_ENVIRON_ACTIVATION_KEY)

if isinstance(response, HTTPException):
# Only considering HTTPClientError and HTTPServerError
# to make sure HTTPRedirection is not reported as error
if isinstance(response, HTTPError):
activation.__exit__(
type(response),
response,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ def _hello_endpoint(request):
helloid = int(request.matchdict["helloid"])
if helloid == 500:
raise exc.HTTPInternalServerError()
if helloid == 302:
raise exc.HTTPFound()
if helloid == 204:
raise exc.HTTPNoContent()
if helloid == 900:
raise NotImplementedError()
return Response("Hello: " + str(helloid))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from opentelemetry.test.test_base import TestBase
from opentelemetry.test.wsgitestutil import WsgiTestBase
from opentelemetry.trace import SpanKind
from opentelemetry.trace.status import StatusCode
from opentelemetry.util.http import (
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST,
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE,
Expand Down Expand Up @@ -93,6 +94,48 @@ def test_registry_name_is_this_module(self):
config.registry.__name__, __name__.rsplit(".", maxsplit=1)[0]
)

def test_redirect_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/302")
self.assertEqual(302, 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/302")
self.assertEqual(302, resp.status_code)
span_list = self.memory_exporter.get_finished_spans()
self.assertEqual(len(span_list), 1)

def test_204_empty_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/204")
self.assertEqual(204, 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/204")
self.assertEqual(204, resp.status_code)
span_list = self.memory_exporter.get_finished_spans()
self.assertEqual(len(span_list), 1)


class TestWrappedWithOtherFramework(
InstrumentationTest, TestBase, WsgiTestBase
Expand Down