Skip to content

Commit 44c6b20

Browse files
srprashTyler Hargraves
authored and
Tyler Hargraves
committed
BugFix: Always close segment in teardown_request handler (aws#272)
* always closing segment if present in teardown_request handler * close segment in teardown_request only. rename teardown method
1 parent 33445ce commit 44c6b20

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

aws_xray_sdk/ext/flask/middleware.py

+7-14
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def __init__(self, app, recorder):
1717
self._recorder = recorder
1818
self.app.before_request(self._before_request)
1919
self.app.after_request(self._after_request)
20-
self.app.teardown_request(self._handle_exception)
20+
self.app.teardown_request(self._teardown_request)
2121
self.in_lambda_ctx = False
2222

2323
if check_in_lambda() and type(self._recorder.context) == LambdaContext:
@@ -81,18 +81,9 @@ def _after_request(self, response):
8181
if cont_len:
8282
segment.put_http_meta(http.CONTENT_LENGTH, int(cont_len))
8383

84-
if response.status_code >= 500:
85-
return response
86-
87-
if self.in_lambda_ctx:
88-
self._recorder.end_subsegment()
89-
else:
90-
self._recorder.end_segment()
9184
return response
9285

93-
def _handle_exception(self, exception):
94-
if not exception:
95-
return
86+
def _teardown_request(self, exception):
9687
segment = None
9788
try:
9889
if self.in_lambda_ctx:
@@ -104,9 +95,11 @@ def _handle_exception(self, exception):
10495
if not segment:
10596
return
10697

107-
segment.put_http_meta(http.STATUS, 500)
108-
stack = stacktrace.get_stacktrace(limit=self._recorder._max_trace_back)
109-
segment.add_exception(exception, stack)
98+
if exception:
99+
segment.put_http_meta(http.STATUS, 500)
100+
stack = stacktrace.get_stacktrace(limit=self._recorder._max_trace_back)
101+
segment.add_exception(exception, stack)
102+
110103
if self.in_lambda_ctx:
111104
self._recorder.end_subsegment()
112105
else:

tests/ext/flask/test_flask.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from tests.util import get_new_stubbed_recorder
1010
import os
1111

12-
1312
# define a flask app for testing purpose
1413
app = Flask(__name__)
1514

@@ -29,6 +28,11 @@ def fault():
2928
return {}['key']
3029

3130

31+
@app.route('/fault_no_exception')
32+
def fault_no_exception():
33+
return "SomeException", 500
34+
35+
3236
@app.route('/template')
3337
def template():
3438
return render_template_string('hello template')
@@ -108,6 +112,18 @@ def test_fault():
108112
assert exception.type == 'KeyError'
109113

110114

115+
def test_fault_no_exception():
116+
path = '/fault_no_exception'
117+
app.get(path)
118+
segment = recorder.emitter.pop()
119+
assert not segment.in_progress
120+
assert segment.fault
121+
122+
response = segment.http['response']
123+
assert response['status'] == 500
124+
assert segment.cause == {}
125+
126+
111127
def test_render_template():
112128
path = '/template'
113129
app.get(path)

0 commit comments

Comments
 (0)