|
| 1 | +# Copyright 2010 New Relic, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import functools |
| 16 | +import logging |
| 17 | +import sys |
| 18 | +import threading |
| 19 | +import warnings |
| 20 | + |
| 21 | +from newrelic.api.application import register_application |
| 22 | +from newrelic.api.background_task import BackgroundTaskWrapper |
| 23 | +from newrelic.api.error_trace import wrap_error_trace |
| 24 | +from newrelic.api.function_trace import FunctionTrace, FunctionTraceWrapper, wrap_function_trace |
| 25 | +from newrelic.api.html_insertion import insert_html_snippet |
| 26 | +from newrelic.api.time_trace import notice_error |
| 27 | +from newrelic.api.transaction import current_transaction |
| 28 | +from newrelic.api.transaction_name import wrap_transaction_name |
| 29 | +from newrelic.api.wsgi_application import WSGIApplicationWrapper |
| 30 | +from newrelic.common.coroutine import is_asyncio_coroutine, is_coroutine_function |
| 31 | +from newrelic.common.object_names import callable_name |
| 32 | +from newrelic.common.object_wrapper import ( |
| 33 | + FunctionWrapper, |
| 34 | + function_wrapper, |
| 35 | + wrap_function_wrapper, |
| 36 | + wrap_in_function, |
| 37 | + wrap_post_function, |
| 38 | +) |
| 39 | +from newrelic.core.config import global_settings |
| 40 | + |
| 41 | +_logger = logging.getLogger(__name__) |
| 42 | + |
| 43 | +def _nr_wrapper_convert_exception_to_response_(wrapped, instance, args, kwargs): |
| 44 | + def _bind_params(original_middleware, *args, **kwargs): |
| 45 | + return original_middleware |
| 46 | + |
| 47 | + original_middleware = _bind_params(*args, **kwargs) |
| 48 | + converted_middleware = wrapped(*args, **kwargs) |
| 49 | + name = callable_name(original_middleware) |
| 50 | + do_not_wrap = is_denied_middleware(name) |
| 51 | + |
| 52 | + if do_not_wrap: |
| 53 | + return converted_middleware |
| 54 | + else: |
| 55 | + if is_coroutine_function(converted_middleware) or is_asyncio_coroutine(converted_middleware): |
| 56 | + return _nr_wrap_converted_middleware_async_(converted_middleware, name) |
| 57 | + return _nr_wrap_converted_middleware_(converted_middleware, name) |
| 58 | + |
| 59 | + |
| 60 | +def instrument_django_core_handlers_exception(module): |
| 61 | + if hasattr(module, "convert_exception_to_response"): |
| 62 | + wrap_function_wrapper(module, "convert_exception_to_response", _nr_wrapper_convert_exception_to_response_) |
| 63 | + |
| 64 | + if hasattr(module, "handle_uncaught_exception"): |
| 65 | + module.handle_uncaught_exception = wrap_handle_uncaught_exception(module.handle_uncaught_exception) |
0 commit comments