|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +# Modifications Copyright The OpenTelemetry Authors. Licensed under the Apache License 2.0 License. |
| 4 | + |
| 5 | +from logging import getLogger |
| 6 | + |
| 7 | +from amazon.opentelemetry.distro.aws_opentelemetry_configurator import get_code_correlation_enabled_status |
| 8 | + |
| 9 | +_logger = getLogger(__name__) |
| 10 | + |
| 11 | + |
| 12 | +def _apply_django_instrumentation_patches() -> None: |
| 13 | + """Django instrumentation patches |
| 14 | +
|
| 15 | + Applies patches to provide code attributes support for Django instrumentation. |
| 16 | + This patches the Django instrumentation to automatically add code attributes |
| 17 | + to spans by modifying the process_view method of the Django middleware. |
| 18 | + Also patches Django's path/re_path functions for URL pattern instrumentation. |
| 19 | + """ |
| 20 | + if get_code_correlation_enabled_status() is True: |
| 21 | + _apply_django_code_attributes_patch() |
| 22 | + |
| 23 | + |
| 24 | +def _apply_django_code_attributes_patch() -> None: # pylint: disable=too-many-statements |
| 25 | + """Django instrumentation patch for code attributes |
| 26 | +
|
| 27 | + This patch modifies the Django middleware's process_view method to automatically add |
| 28 | + code attributes to the current span when a view function is about to be executed. |
| 29 | +
|
| 30 | + The patch includes: |
| 31 | + 1. Support for class-based views by extracting the actual HTTP method handler |
| 32 | + 2. Automatic addition of code.function.name, code.file.path, and code.line.number |
| 33 | + 3. Graceful error handling and cleanup during uninstrument |
| 34 | + """ |
| 35 | + try: |
| 36 | + # Import Django instrumentation classes and AWS code correlation function |
| 37 | + from amazon.opentelemetry.distro.code_correlation import ( # pylint: disable=import-outside-toplevel |
| 38 | + add_code_attributes_to_span, |
| 39 | + ) |
| 40 | + from opentelemetry.instrumentation.django import DjangoInstrumentor # pylint: disable=import-outside-toplevel |
| 41 | + |
| 42 | + # Store the original _instrument and _uninstrument methods |
| 43 | + original_instrument = DjangoInstrumentor._instrument |
| 44 | + original_uninstrument = DjangoInstrumentor._uninstrument |
| 45 | + |
| 46 | + # Store reference to original Django middleware process_view method |
| 47 | + original_process_view = None |
| 48 | + |
| 49 | + def _patch_django_middleware(): |
| 50 | + """Patch Django middleware's process_view method to add code attributes.""" |
| 51 | + try: |
| 52 | + # Import Django middleware class |
| 53 | + # pylint: disable=import-outside-toplevel |
| 54 | + from opentelemetry.instrumentation.django.middleware.otel_middleware import _DjangoMiddleware |
| 55 | + |
| 56 | + nonlocal original_process_view |
| 57 | + if original_process_view is None: |
| 58 | + original_process_view = _DjangoMiddleware.process_view |
| 59 | + |
| 60 | + def patched_process_view( |
| 61 | + self, request, view_func, *args, **kwargs |
| 62 | + ): # pylint: disable=too-many-locals,too-many-nested-blocks,too-many-branches |
| 63 | + """Patched process_view method to add code attributes to the span.""" |
| 64 | + # First call the original process_view method |
| 65 | + result = original_process_view(self, request, view_func, *args, **kwargs) |
| 66 | + |
| 67 | + # Add code attributes if we have a span and view function |
| 68 | + try: |
| 69 | + if ( |
| 70 | + self._environ_activation_key in request.META.keys() |
| 71 | + and self._environ_span_key in request.META.keys() |
| 72 | + ): |
| 73 | + span = request.META[self._environ_span_key] |
| 74 | + if span and view_func and span.is_recording(): |
| 75 | + # Determine the target function/method to analyze |
| 76 | + target = view_func |
| 77 | + |
| 78 | + # If it's a class-based view, get the corresponding HTTP method handler |
| 79 | + view_class = getattr(view_func, "view_class", None) |
| 80 | + if view_class: |
| 81 | + method_name = request.method.lower() |
| 82 | + handler = getattr(view_class, method_name, None) or view_class |
| 83 | + target = handler |
| 84 | + |
| 85 | + # Call the existing add_code_attributes_to_span function |
| 86 | + add_code_attributes_to_span(span, target) |
| 87 | + _logger.debug( |
| 88 | + "Added code attributes to span for Django view: %s", |
| 89 | + getattr(target, "__name__", str(target)), |
| 90 | + ) |
| 91 | + except Exception as exc: # pylint: disable=broad-exception-caught |
| 92 | + # Don't let code attributes addition break the request processing |
| 93 | + _logger.warning("Failed to add code attributes to Django span: %s", exc) |
| 94 | + |
| 95 | + return result |
| 96 | + |
| 97 | + # Apply the patch |
| 98 | + _DjangoMiddleware.process_view = patched_process_view |
| 99 | + _logger.debug("Django middleware process_view patched successfully for code attributes") |
| 100 | + |
| 101 | + except Exception as exc: # pylint: disable=broad-exception-caught |
| 102 | + _logger.warning("Failed to patch Django middleware process_view: %s", exc) |
| 103 | + |
| 104 | + def _unpatch_django_middleware(): |
| 105 | + """Restore original Django middleware process_view method.""" |
| 106 | + try: |
| 107 | + # pylint: disable=import-outside-toplevel |
| 108 | + from opentelemetry.instrumentation.django.middleware.otel_middleware import _DjangoMiddleware |
| 109 | + |
| 110 | + if original_process_view is not None: |
| 111 | + _DjangoMiddleware.process_view = original_process_view |
| 112 | + _logger.debug("Django middleware process_view restored successfully") |
| 113 | + |
| 114 | + except Exception as exc: # pylint: disable=broad-exception-caught |
| 115 | + _logger.warning("Failed to restore Django middleware process_view: %s", exc) |
| 116 | + |
| 117 | + def patched_instrument(self, **kwargs): |
| 118 | + """Patched _instrument method with Django middleware patching""" |
| 119 | + # Apply Django middleware patches |
| 120 | + _patch_django_middleware() |
| 121 | + |
| 122 | + # Call the original _instrument method |
| 123 | + original_instrument(self, **kwargs) |
| 124 | + |
| 125 | + def patched_uninstrument(self, **kwargs): |
| 126 | + """Patched _uninstrument method with Django middleware patch restoration""" |
| 127 | + # Call the original _uninstrument method first |
| 128 | + original_uninstrument(self, **kwargs) |
| 129 | + |
| 130 | + # Restore original Django middleware |
| 131 | + _unpatch_django_middleware() |
| 132 | + |
| 133 | + # Apply the patches to DjangoInstrumentor |
| 134 | + DjangoInstrumentor._instrument = patched_instrument |
| 135 | + DjangoInstrumentor._uninstrument = patched_uninstrument |
| 136 | + |
| 137 | + _logger.debug("Django instrumentation code attributes patch applied successfully") |
| 138 | + |
| 139 | + except Exception as exc: # pylint: disable=broad-exception-caught |
| 140 | + _logger.warning("Failed to apply Django code attributes patch: %s", exc) |
0 commit comments