Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add patch for sentry SDK to correct ASGI v2/v3 detection. #680

Merged
merged 4 commits into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions newrelic/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2174,6 +2174,10 @@ def _process_module_builtin_defaults():
"instrument_graphqlserver",
)

_process_module_definition(
"sentry_sdk.integrations.asgi", "newrelic.hooks.component_sentry", "instrument_sentry_sdk_integrations_asgi"
)

# _process_module_definition('web.application',
# 'newrelic.hooks.framework_webpy')
# _process_module_definition('web.template',
Expand Down
41 changes: 41 additions & 0 deletions newrelic/hooks/component_sentry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Copyright 2010 New Relic, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from newrelic.common.object_wrapper import FunctionWrapper, wrap_function_wrapper

# This is NOT a fully-featured instrumentation for the sentry SDK. Instead
# this is a monkey-patch of the sentry SDK to work around a bug that causes
# improper ASGI 2/3 version detection when inspecting our wrappers. We fix this
# by manually unwrapping the application when version detection is run.


def bind__looks_like_asgi3(app):
return app


def wrap__looks_like_asgi3(wrapped, instance, args, kwargs):
try:
app = bind__looks_like_asgi3(*args, **kwargs)
except Exception:
return wrapped(*args, **kwargs)

while isinstance(app, FunctionWrapper) and hasattr(app, "__wrapped__"):
app = app.__wrapped__

return wrapped(app)


def instrument_sentry_sdk_integrations_asgi(module):
if hasattr(module, "_looks_like_asgi3"):
wrap_function_wrapper(module, "_looks_like_asgi3", wrap__looks_like_asgi3)