Skip to content

Commit 062e702

Browse files
umaannamalaiTimPansinolrafeeihmstepanek
authored
Add patch for sentry SDK to correct ASGI v2/v3 detection. (#680)
* Add patch for sentry to correct ASGI v2/v3 detection. Co-authored-by: Tim Pansino <[email protected]> Co-authored-by: Lalleh Rafeei <[email protected]> Co-authored-by: Hannah Stepanek <[email protected]> * [Mega-Linter] Apply linters fixes Co-authored-by: Tim Pansino <[email protected]> Co-authored-by: Lalleh Rafeei <[email protected]> Co-authored-by: Hannah Stepanek <[email protected]> Co-authored-by: umaannamalai <[email protected]> Co-authored-by: Timothy Pansino <[email protected]>
1 parent d4ff1ec commit 062e702

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

newrelic/config.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2174,6 +2174,10 @@ def _process_module_builtin_defaults():
21742174
"instrument_graphqlserver",
21752175
)
21762176

2177+
_process_module_definition(
2178+
"sentry_sdk.integrations.asgi", "newrelic.hooks.component_sentry", "instrument_sentry_sdk_integrations_asgi"
2179+
)
2180+
21772181
# _process_module_definition('web.application',
21782182
# 'newrelic.hooks.framework_webpy')
21792183
# _process_module_definition('web.template',

newrelic/hooks/component_sentry.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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

Comments
 (0)