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 exception processing in Django running in Lambda #86 #145

Merged
merged 1 commit into from
Mar 4, 2019
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
5 changes: 4 additions & 1 deletion aws_xray_sdk/ext/django/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ def process_exception(self, request, exception):
Add exception information and fault flag to the
current segment.
"""
segment = xray_recorder.current_segment()
if self.in_lambda_ctx:
segment = xray_recorder.current_subsegment()
else:
segment = xray_recorder.current_segment()
segment.put_http_meta(http.STATUS, 500)

stack = stacktrace.get_stacktrace(limit=xray_recorder._max_trace_back)
Expand Down
19 changes: 19 additions & 0 deletions tests/ext/django/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,25 @@ def test_lambda_serverless(self):
segment = new_recorder.emitter.pop()
assert not segment

# Test Fault in Lambda
url = reverse('500fault')
try:
self.client.get(url)
except Exception:
pass
segment = xray_recorder.emitter.pop()
assert segment.fault

request = segment.http['request']
response = segment.http['response']

assert request['method'] == 'GET'
assert request['client_ip'] == '127.0.0.1'
assert response['status'] == 500

exception = segment.cause['exceptions'][0]
assert exception.type == 'KeyError'

def test_lambda_default_ctx(self):
# Track to make sure that Django will default to generating segments if context is not the lambda context
url = reverse('200ok')
Expand Down