|
| 1 | +""" |
| 2 | +DRF Automatic Instrumentation. |
| 3 | +
|
| 4 | +Monkey-patches Django REST Framework to automatically trace key lifecycle methods. |
| 5 | +This enables bottleneck identification without modifying existing view code. |
| 6 | +""" |
| 7 | +import functools |
| 8 | +from typing import Callable, Any |
| 9 | + |
| 10 | +from .tracing import trace_segment |
| 11 | + |
| 12 | +# Flag to ensure we only install once |
| 13 | +_installed = False |
| 14 | + |
| 15 | + |
| 16 | +def _wrap_method(original_method: Callable, segment_name: str) -> Callable: |
| 17 | + """ |
| 18 | + Wrap a method to trace its execution time. |
| 19 | +
|
| 20 | + Args: |
| 21 | + original_method: The original method to wrap |
| 22 | + segment_name: Name for the trace segment |
| 23 | +
|
| 24 | + Returns: |
| 25 | + Wrapped method that traces execution time |
| 26 | + """ |
| 27 | + @functools.wraps(original_method) |
| 28 | + def wrapper(*args, **kwargs) -> Any: |
| 29 | + with trace_segment(segment_name): |
| 30 | + return original_method(*args, **kwargs) |
| 31 | + return wrapper |
| 32 | + |
| 33 | + |
| 34 | +def _wrap_method_with_dynamic_name(original_method: Callable, name_prefix: str) -> Callable: |
| 35 | + """ |
| 36 | + Wrap a method with a dynamic segment name based on the class. |
| 37 | +
|
| 38 | + Args: |
| 39 | + original_method: The original method to wrap |
| 40 | + name_prefix: Prefix for the segment name (class name will be appended) |
| 41 | +
|
| 42 | + Returns: |
| 43 | + Wrapped method that traces execution time |
| 44 | + """ |
| 45 | + @functools.wraps(original_method) |
| 46 | + def wrapper(self, *args, **kwargs) -> Any: |
| 47 | + class_name = self.__class__.__name__ |
| 48 | + segment_name = f"{name_prefix}:{class_name}" |
| 49 | + with trace_segment(segment_name): |
| 50 | + return original_method(self, *args, **kwargs) |
| 51 | + return wrapper |
| 52 | + |
| 53 | + |
| 54 | +def install_drf_tracing() -> None: |
| 55 | + """ |
| 56 | + Install automatic tracing on DRF classes. |
| 57 | +
|
| 58 | + Should be called once at application startup (e.g., in AppConfig.ready()). |
| 59 | + Instruments the following DRF methods: |
| 60 | + - APIView.check_permissions() -> "permissions" |
| 61 | + - APIView.check_throttles() -> "throttles" |
| 62 | + - GenericAPIView.get_serializer() -> "serializer:init" |
| 63 | + - GenericAPIView.get_queryset() -> "queryset:{ClassName}" |
| 64 | + - Serializer.to_representation() -> "serializer:render" |
| 65 | + - Serializer.to_internal_value() -> "serializer:parse" |
| 66 | + """ |
| 67 | + global _installed |
| 68 | + if _installed: |
| 69 | + return |
| 70 | + |
| 71 | + try: |
| 72 | + from rest_framework.views import APIView |
| 73 | + from rest_framework.generics import GenericAPIView |
| 74 | + from rest_framework.serializers import Serializer |
| 75 | + |
| 76 | + # Instrument APIView methods |
| 77 | + if hasattr(APIView, 'check_permissions'): |
| 78 | + APIView.check_permissions = _wrap_method( |
| 79 | + APIView.check_permissions, |
| 80 | + 'permissions' |
| 81 | + ) |
| 82 | + |
| 83 | + if hasattr(APIView, 'check_throttles'): |
| 84 | + APIView.check_throttles = _wrap_method( |
| 85 | + APIView.check_throttles, |
| 86 | + 'throttles' |
| 87 | + ) |
| 88 | + |
| 89 | + # Instrument GenericAPIView methods |
| 90 | + if hasattr(GenericAPIView, 'get_queryset'): |
| 91 | + GenericAPIView.get_queryset = _wrap_method_with_dynamic_name( |
| 92 | + GenericAPIView.get_queryset, |
| 93 | + 'queryset' |
| 94 | + ) |
| 95 | + |
| 96 | + if hasattr(GenericAPIView, 'get_serializer'): |
| 97 | + GenericAPIView.get_serializer = _wrap_method( |
| 98 | + GenericAPIView.get_serializer, |
| 99 | + 'serializer:init' |
| 100 | + ) |
| 101 | + |
| 102 | + # Instrument Serializer methods |
| 103 | + if hasattr(Serializer, 'to_representation'): |
| 104 | + original_to_representation = Serializer.to_representation |
| 105 | + |
| 106 | + @functools.wraps(original_to_representation) |
| 107 | + def traced_to_representation(self, instance): |
| 108 | + # Only trace if this is the top-level serializer call |
| 109 | + # to avoid tracing nested serializer calls |
| 110 | + if not getattr(self, '_tracing_render', False): |
| 111 | + self._tracing_render = True |
| 112 | + try: |
| 113 | + with trace_segment('serializer:render'): |
| 114 | + return original_to_representation(self, instance) |
| 115 | + finally: |
| 116 | + self._tracing_render = False |
| 117 | + return original_to_representation(self, instance) |
| 118 | + |
| 119 | + Serializer.to_representation = traced_to_representation |
| 120 | + |
| 121 | + if hasattr(Serializer, 'to_internal_value'): |
| 122 | + original_to_internal = Serializer.to_internal_value |
| 123 | + |
| 124 | + @functools.wraps(original_to_internal) |
| 125 | + def traced_to_internal_value(self, data): |
| 126 | + # Only trace if this is the top-level serializer call |
| 127 | + if not getattr(self, '_tracing_parse', False): |
| 128 | + self._tracing_parse = True |
| 129 | + try: |
| 130 | + with trace_segment('serializer:parse'): |
| 131 | + return original_to_internal(self, data) |
| 132 | + finally: |
| 133 | + self._tracing_parse = False |
| 134 | + return original_to_internal(self, data) |
| 135 | + |
| 136 | + Serializer.to_internal_value = traced_to_internal_value |
| 137 | + |
| 138 | + _installed = True |
| 139 | + |
| 140 | + except ImportError: |
| 141 | + # DRF not installed, skip instrumentation |
| 142 | + pass |
0 commit comments