|
| 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 | +from newrelic.common.object_wrapper import FunctionWrapper, wrap_function_wrapper |
| 16 | + |
| 17 | +# This is NOT a fully-featured instrumentation for the sentry SDK. Instead |
| 18 | +# this is a monkey-patch of the sentry SDK to work around a bug that causes |
| 19 | +# improper ASGI 2/3 version detection when inspecting our wrappers. We fix this |
| 20 | +# by manually unwrapping the application when version detection is run. |
| 21 | + |
| 22 | + |
| 23 | +def bind__looks_like_asgi3(app): |
| 24 | + return app |
| 25 | + |
| 26 | + |
| 27 | +def wrap__looks_like_asgi3(wrapped, instance, args, kwargs): |
| 28 | + try: |
| 29 | + app = bind__looks_like_asgi3(*args, **kwargs) |
| 30 | + except Exception: |
| 31 | + return wrapped(*args, **kwargs) |
| 32 | + |
| 33 | + while isinstance(app, FunctionWrapper) and hasattr(app, "__wrapped__"): |
| 34 | + app = app.__wrapped__ |
| 35 | + |
| 36 | + return wrapped(app) |
| 37 | + |
| 38 | + |
| 39 | +def instrument_sentry_sdk_integrations_asgi(module): |
| 40 | + if hasattr(module, "_looks_like_asgi3"): |
| 41 | + wrap_function_wrapper(module, "_looks_like_asgi3", wrap__looks_like_asgi3) |
0 commit comments