Skip to content

Commit

Permalink
Not closing segment in after_request in case of internal error (aws#271)
Browse files Browse the repository at this point in the history
  • Loading branch information
srprash authored and Tyler Hargraves committed Mar 22, 2022
1 parent e8c60f2 commit 33445ce
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
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

0 comments on commit 33445ce

Please sign in to comment.