Skip to content

Close segment in only _handle_exception in case of Internal Server Error #271

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

Merged
merged 1 commit into from
Feb 1, 2021
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: 3 additions & 0 deletions aws_xray_sdk/ext/flask/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ def _after_request(self, response):
if cont_len:
segment.put_http_meta(http.CONTENT_LENGTH, int(cont_len))

if response.status_code >= 500:
return response

if self.in_lambda_ctx:
self._recorder.end_subsegment()
else:
Expand Down
7 changes: 5 additions & 2 deletions tests/ext/flask/test_flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@ def template():
recorder.configure(service='test', sampling=False, context=Context())
XRayMiddleware(app, recorder)

# enable testing mode
app.config['TESTING'] = True
# We don't need to enable testing mode by doing app.config['TESTING'] = True
# because what it does is disable error catching during request handling,
# so that you get better error reports when performing test requests against the application.
# But this also results in `after_request` method not getting invoked during unhandled exception which we want
# since it is the actual application behavior in our use case.
app = app.test_client()

BASE_URL = 'http://localhost{}'
Expand Down