| 1 |
import logging |
| 2 |
import traceback |
| 3 |
|
| 4 |
from aws_xray_sdk.core import xray_recorder |
| 5 |
from aws_xray_sdk.core.models import http |
| 6 |
from aws_xray_sdk.ext.util import calculate_sampling_decision, \ |
| 7 |
calculate_segment_name, construct_xray_header |
| 8 |
|
| 9 |
|
| 10 |
log = logging.getLogger(__name__) |
| 11 |
|
| 12 |
# Django will rewrite some http request headers. |
| 13 |
USER_AGENT_KEY = 'HTTP_USER_AGENT' |
| 14 |
X_FORWARDED_KEY = 'HTTP_X_FORWARDED_FOR' |
| 15 |
REMOTE_ADDR_KEY = 'REMOTE_ADDR' |
| 16 |
HOST_KEY = 'HTTP_HOST' |
| 17 |
CONTENT_LENGTH_KEY = 'content-length' |
| 18 |
|
| 19 |
|
| 20 |
class XRayMiddleware(object): |
| 21 |
""" |
| 22 |
Middleware that wraps each incoming request to a segment. |
| 23 |
""" |
| 24 |
def __init__(self, get_response): |
| 25 |
|
| 26 |
self.get_response = get_response |
| 27 |
|
| 28 |
# hooks for django version >= 1.10 |
| 29 |
def __call__(self, request): |
| 30 |
|
| 31 |
sampling_decision = None |
| 32 |
meta = request.META |
| 33 |
xray_header = construct_xray_header(meta) |
| 34 |
# a segment name is required |
| 35 |
name = calculate_segment_name(meta.get(HOST_KEY), xray_recorder) |
| 36 |
|
| 37 |
sampling_decision = calculate_sampling_decision( |
| 38 |
trace_header=xray_header, |
| 39 |
recorder=xray_recorder, |
| 40 |
service_name=meta.get(HOST_KEY), |
| 41 |
method=request.method, |
| 42 |
path=request.path, |
| 43 |
) |
| 44 |
|
| 45 |
segment = xray_recorder.begin_segment( |
| 46 |
name=name, |
| 47 |
traceid=xray_header.root, |
| 48 |
parent_id=xray_header.parent, |
| 49 |
sampling=sampling_decision, |
| 50 |
) |
| 51 |
|
| 52 |
segment.put_http_meta(http.URL, request.build_absolute_uri()) |
| 53 |
segment.put_http_meta(http.METHOD, request.method) |
| 54 |
|
| 55 |
if meta.get(USER_AGENT_KEY): |
| 56 |
segment.put_http_meta(http.USER_AGENT, meta.get(USER_AGENT_KEY)) |
| 57 |
if meta.get(X_FORWARDED_KEY): |
| 58 |
# X_FORWARDED_FOR may come from untrusted source so we |
| 59 |
# need to set the flag to true as additional information |
| 60 |
segment.put_http_meta(http.CLIENT_IP, meta.get(X_FORWARDED_KEY)) |
| 61 |
segment.put_http_meta(http.X_FORWARDED_FOR, True) |
| 62 |
elif meta.get(REMOTE_ADDR_KEY): |
| 63 |
segment.put_http_meta(http.CLIENT_IP, meta.get(REMOTE_ADDR_KEY)) |
| 64 |
|
| 65 |
response = self.get_response(request) |
| 66 |
segment.put_http_meta(http.STATUS, response.status_code) |
| 67 |
|
| 68 |
if response.has_header(CONTENT_LENGTH_KEY): |
| 69 |
length = int(response[CONTENT_LENGTH_KEY]) |
| 70 |
segment.put_http_meta(http.CONTENT_LENGTH, length) |
| 71 |
|
| 72 |
xray_recorder.end_segment() |
| 73 |
|
| 74 |
return response |
| 75 |
|
| 76 |
def process_exception(self, request, exception): |
| 77 |
""" |
| 78 |
Add exception information and fault flag to the |
| 79 |
current segment. |
| 80 |
""" |
| 81 |
segment = xray_recorder.current_segment() |
| 82 |
segment.put_http_meta(http.STATUS, 500) |
| 83 |
|
| 84 |
stack = traceback.extract_stack(limit=xray_recorder._max_trace_back) |
| 85 |
segment.add_exception(exception, stack) |