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

Fix Tornado errors mapping to 500 #1048

Merged
merged 2 commits into from
Apr 13, 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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased](https://github.com/open-telemetry/opentelemetry-python/compare/v1.10.0-0.29b0...HEAD)

### Fixed

- `opentelemetry-instrumentation-tornado` Fix Tornado errors mapping to 500
([#1048])(https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1048)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move this to Fixed section.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move this to Fixed section.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

- `opentelemetry-instrumentation-urllib` make span attributes available to sampler
([1014](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1014))
- `opentelemetry-instrumentation-flask` Fix non-recording span bug
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,14 +420,15 @@ def _finish_span(tracer, handler, error=None):
status_code = error.status_code
if not ctx and status_code == 404:
ctx = _start_span(tracer, handler, _time_ns())
if status_code != 404:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@owais
Was there a reason for explicitly checking for non-404s previously?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I undertood right, it was needed for not setting traceback if status equals 404, in other case do it. I've changed this behavior, and now traceback will be setted only for server errors - 500s

else:
status_code = 500
reason = None
if status_code >= 500:
finish_args = (
type(error),
error,
getattr(error, "__traceback__", None),
)
status_code = 500
reason = None

if not ctx:
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,41 @@ def test_404(self):
},
)

def test_http_error(self):
response = self.fetch("/raise_403")
self.assertEqual(response.code, 403)

spans = self.sorted_spans(self.memory_exporter.get_finished_spans())
self.assertEqual(len(spans), 2)
server, client = spans

self.assertEqual(server.name, "RaiseHTTPErrorHandler.get")
self.assertEqual(server.kind, SpanKind.SERVER)
self.assertSpanHasAttributes(
server,
{
SpanAttributes.HTTP_METHOD: "GET",
SpanAttributes.HTTP_SCHEME: "http",
SpanAttributes.HTTP_HOST: "127.0.0.1:"
+ str(self.get_http_port()),
SpanAttributes.HTTP_TARGET: "/raise_403",
SpanAttributes.HTTP_CLIENT_IP: "127.0.0.1",
SpanAttributes.HTTP_STATUS_CODE: 403,
"tornado.handler": "tests.tornado_test_app.RaiseHTTPErrorHandler",
},
)

self.assertEqual(client.name, "GET")
self.assertEqual(client.kind, SpanKind.CLIENT)
self.assertSpanHasAttributes(
client,
{
SpanAttributes.HTTP_URL: self.get_url("/raise_403"),
SpanAttributes.HTTP_METHOD: "GET",
SpanAttributes.HTTP_STATUS_CODE: 403,
},
)

def test_dynamic_handler(self):
response = self.fetch("/dyna")
self.assertEqual(response.code, 404)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ def get(self):
self.set_status(200)


class RaiseHTTPErrorHandler(tornado.web.RequestHandler):
def get(self):
raise tornado.web.HTTPError(403)


def make_app(tracer):
app = tornado.web.Application(
[
Expand All @@ -116,6 +121,7 @@ def make_app(tracer):
(r"/healthz", HealthCheckHandler),
(r"/ping", HealthCheckHandler),
(r"/test_custom_response_headers", CustomResponseHeaderHandler),
(r"/raise_403", RaiseHTTPErrorHandler),
]
)
app.tracer = tracer
Expand Down