def patch_restframework(tracer): """ Patches rest_framework app. To trace exceptions occuring during view processing we currently use a TraceExceptionMiddleware. However the rest_framework handles exceptions before they come to our middleware. So we need to manually patch the rest_framework exception handler to set the exception stack trace in the current span. """ def _traced_handle_exception(wrapped, instance, args, kwargs): """ Sets the error message, error type and exception stack trace to the current span before calling the original exception handler. """ span = tracer.current_span() if span is not None: span.set_traceback() return wrapped(*args, **kwargs) # do not patch if already patched if getattr(APIView, '_datadog_patch', False): return else: setattr(APIView, '_datadog_patch', True) # trace the handle_exception method wrap('rest_framework.views', 'APIView.handle_exception', _traced_handle_exception)
def _patch_httplib(): """ Patch the Python built-in `httplib` (Python 2) or `http.client` (Python 3) module. """ global _httplib_patched if not _httplib_patched: _httplib_patched = True wrap(httplib_module, 'HTTPConnection.request', _wrap_httplib_request) logger.debug('Patched %s', httplib_module)
def _patch_for_integration_tests(): """ Patch `requests` to log the outgoing requests for integration tests. """ global _integration_tests_patched is_in_tests = os.environ.get("DD_INTEGRATION_TEST", "false").lower() == "true" if not _integration_tests_patched and is_in_tests: wrap("requests", "Session.send", _log_request) _integration_tests_patched = True
def _patch_requests(): """ Patch the high-level HTTP client module `requests` if it's installed. """ global _requests_patched if not _requests_patched: _requests_patched = True try: wrap('requests', 'Session.request', _wrap_requests_request) logger.debug('Patched requests') except Exception: logger.debug('Failed to patch requests', exc_info=True)
def _patch_requests(module): """ Patch the high-level HTTP client module `requests` if it's installed. """ global _requests_patched if not _requests_patched: _requests_patched = True try: wrap("requests", "Session.request", _wrap_requests_request) logger.debug("Patched requests") except Exception: logger.debug("Failed to patch requests", exc_info=True)