|
31 | 31 |
|
32 | 32 |
|
33 | 33 | def install(): |
34 | | - try: |
35 | | - from django.core.handlers.base import BaseHandler |
36 | | - from django.core.handlers import exception |
37 | | - |
38 | | - _get_response = BaseHandler.get_response |
39 | | - _handle_uncaught_exception = exception.handle_uncaught_exception |
40 | | - |
41 | | - def _sw_get_response(this, request): |
42 | | - if request is None: |
43 | | - resp = _get_response(this, request) |
44 | | - return resp |
45 | | - |
46 | | - context = get_context() |
47 | | - carrier = Carrier() |
48 | | - for item in carrier: |
49 | | - # Any HTTP headers in the request are converted to META keys by converting all characters to uppercase, |
50 | | - # replacing any hyphens with underscores and adding an HTTP_ prefix to the name. |
51 | | - # https://docs.djangoproject.com/en/3.0/ref/request-response/#django.http.HttpRequest.META |
52 | | - sw_http_header_key = 'HTTP_%s' % item.key.upper().replace('-', '_') |
53 | | - if sw_http_header_key in request.META: |
54 | | - item.val = request.META[sw_http_header_key] |
55 | | - |
56 | | - with context.new_entry_span(op=request.path, carrier=carrier) as span: |
57 | | - span.layer = Layer.Http |
58 | | - span.component = Component.Django |
59 | | - span.peer = '%s:%s' % (request.META.get('REMOTE_ADDR'), request.META.get('REMOTE_PORT') or "80") |
60 | | - |
61 | | - span.tag(Tag(key=tags.HttpMethod, val=request.method)) |
62 | | - span.tag(Tag(key=tags.HttpUrl, val=request.build_absolute_uri().split("?")[0])) |
63 | | - |
64 | | - # you can get request parameters by `request.GET` even though client are using POST or other methods |
65 | | - if config.django_collect_http_params and request.GET: |
66 | | - span.tag(Tag(key=tags.HttpParams, |
67 | | - val=params_tostring(request.GET)[0:config.http_params_length_threshold])) |
68 | | - |
69 | | - resp = _get_response(this, request) |
70 | | - span.tag(Tag(key=tags.HttpStatus, val=resp.status_code)) |
71 | | - if resp.status_code >= 400: |
72 | | - span.error_occurred = True |
73 | | - return resp |
74 | | - |
75 | | - def _sw_handle_uncaught_exception(request, resolver, exc_info): |
76 | | - if exc_info is not None: |
77 | | - entry_span = get_context().active_span() |
78 | | - if entry_span is not None: |
79 | | - entry_span.raised() |
80 | | - |
81 | | - return _handle_uncaught_exception(request, resolver, exc_info) |
82 | | - |
83 | | - BaseHandler.get_response = _sw_get_response |
84 | | - exception.handle_uncaught_exception = _sw_handle_uncaught_exception |
85 | | - except Exception: |
86 | | - logger.warning('failed to install plugin %s', __name__) |
| 34 | + from django.core.handlers.base import BaseHandler |
| 35 | + from django.core.handlers import exception |
| 36 | + |
| 37 | + _get_response = BaseHandler.get_response |
| 38 | + _handle_uncaught_exception = exception.handle_uncaught_exception |
| 39 | + |
| 40 | + def _sw_get_response(this, request): |
| 41 | + if request is None: |
| 42 | + resp = _get_response(this, request) |
| 43 | + return resp |
| 44 | + |
| 45 | + context = get_context() |
| 46 | + carrier = Carrier() |
| 47 | + for item in carrier: |
| 48 | + # Any HTTP headers in the request are converted to META keys by converting all characters to uppercase, |
| 49 | + # replacing any hyphens with underscores and adding an HTTP_ prefix to the name. |
| 50 | + # https://docs.djangoproject.com/en/3.0/ref/request-response/#django.http.HttpRequest.META |
| 51 | + sw_http_header_key = 'HTTP_%s' % item.key.upper().replace('-', '_') |
| 52 | + if sw_http_header_key in request.META: |
| 53 | + item.val = request.META[sw_http_header_key] |
| 54 | + |
| 55 | + with context.new_entry_span(op=request.path, carrier=carrier) as span: |
| 56 | + span.layer = Layer.Http |
| 57 | + span.component = Component.Django |
| 58 | + span.peer = '%s:%s' % (request.META.get('REMOTE_ADDR'), request.META.get('REMOTE_PORT') or "80") |
| 59 | + |
| 60 | + span.tag(Tag(key=tags.HttpMethod, val=request.method)) |
| 61 | + span.tag(Tag(key=tags.HttpUrl, val=request.build_absolute_uri().split("?")[0])) |
| 62 | + |
| 63 | + # you can get request parameters by `request.GET` even though client are using POST or other methods |
| 64 | + if config.django_collect_http_params and request.GET: |
| 65 | + span.tag(Tag(key=tags.HttpParams, |
| 66 | + val=params_tostring(request.GET)[0:config.http_params_length_threshold])) |
| 67 | + |
| 68 | + resp = _get_response(this, request) |
| 69 | + span.tag(Tag(key=tags.HttpStatus, val=resp.status_code)) |
| 70 | + if resp.status_code >= 400: |
| 71 | + span.error_occurred = True |
| 72 | + return resp |
| 73 | + |
| 74 | + def _sw_handle_uncaught_exception(request, resolver, exc_info): |
| 75 | + if exc_info is not None: |
| 76 | + entry_span = get_context().active_span() |
| 77 | + if entry_span is not None: |
| 78 | + entry_span.raised() |
| 79 | + |
| 80 | + return _handle_uncaught_exception(request, resolver, exc_info) |
| 81 | + |
| 82 | + BaseHandler.get_response = _sw_get_response |
| 83 | + exception.handle_uncaught_exception = _sw_handle_uncaught_exception |
87 | 84 |
|
88 | 85 |
|
89 | 86 | def params_tostring(params): |
|
0 commit comments