Skip to content

Commit e253a3e

Browse files
committed
Cache Page import in global and move to django hook
1 parent e80e1a7 commit e253a3e

3 files changed

Lines changed: 29 additions & 43 deletions

File tree

newrelic/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2995,7 +2995,7 @@ def _process_module_builtin_defaults():
29952995
_process_module_definition("flask_restx.api", "newrelic.hooks.component_flask_rest", "instrument_flask_rest")
29962996

29972997
_process_module_definition(
2998-
"wagtail.models.pages", "newrelic.hooks.framework_wagtail", "instrument_wagtail_models_pages"
2998+
"wagtail.models.pages", "newrelic.hooks.framework_django", "instrument_wagtail_models_pages"
29992999
)
30003000

30013001
_process_module_definition("graphql_server", "newrelic.hooks.component_graphqlserver", "instrument_graphqlserver")

newrelic/hooks/framework_django.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@
5252
"off": False,
5353
}
5454

55+
global WAGTAIL_PAGE
56+
WAGTAIL_PAGE = None
57+
5558

5659
def _setting_boolean(value):
5760
if value.lower() not in _boolean_states:
@@ -490,11 +493,8 @@ def wrapper(wrapped, instance, args, kwargs):
490493
# to override the priority set in other parts of this hook file so that the
491494
# more explicit name takes precedence.
492495
new_name = name
493-
try:
494-
from wagtail.models.pages import Page
495-
except:
496-
Page = None
497-
if instance and isinstance(instance, Page):
496+
global WAGTAIL_PAGE
497+
if WAGTAIL_PAGE and instance and isinstance(instance, WAGTAIL_PAGE):
498498
new_name = f"{callable_name(instance)}.{wrapped.__name__}"
499499
transaction.set_transaction_name(new_name, priority=6)
500500
else:
@@ -1227,6 +1227,21 @@ def _bind_params(original_middleware, *args, **kwargs):
12271227
return _nr_wrap_converted_middleware_(converted_middleware, name)
12281228

12291229

1230+
def _nr_wrapper_route_for_request(wrapped, instance, args, kwargs):
1231+
transaction = current_transaction()
1232+
1233+
if not transaction:
1234+
return wrapped(*args, **kwargs)
1235+
1236+
route_result = wrapped(*args, **kwargs)
1237+
if route_result:
1238+
page, args, kwargs = route_result
1239+
name = callable_name(page.route)
1240+
transaction.set_transaction_name(name, priority=6)
1241+
1242+
return route_result
1243+
1244+
12301245
def instrument_django_core_handlers_exception(module):
12311246
if hasattr(module, "convert_exception_to_response"):
12321247
wrap_function_wrapper(module, "convert_exception_to_response", _nr_wrapper_convert_exception_to_response_)
@@ -1244,3 +1259,11 @@ def instrument_django_core_handlers_asgi(module):
12441259
from newrelic.api.asgi_application import wrap_asgi_application
12451260

12461261
wrap_asgi_application(module, "ASGIHandler.__call__", framework=framework)
1262+
1263+
1264+
def instrument_wagtail_models_pages(module):
1265+
if hasattr(module, "Page"):
1266+
global WAGTAIL_PAGE
1267+
WAGTAIL_PAGE = module.Page
1268+
if hasattr(module.Page, "route_for_request"):
1269+
wrap_function_wrapper(module, "Page.route_for_request", _nr_wrapper_route_for_request)

newrelic/hooks/framework_wagtail.py

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)