Skip to content

Commit 1f1d628

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

3 files changed

Lines changed: 25 additions & 43 deletions

File tree

newrelic/config.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2995,10 +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"
2999-
)
3000-
_process_module_definition(
3001-
"wagtail.models.pages". "newrelic.hooks.framework_django", "instrument_wagtail_models_pages"
2998+
"wagtail.models.pages", "newrelic.hooks.framework_django", "instrument_wagtail_models_pages"
30022999
)
30033000

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

newrelic/hooks/framework_django.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151
"false": False,
5252
"off": False,
5353
}
54+
55+
global WAGTAIL_PAGE
5456
WAGTAIL_PAGE = None
5557

5658

@@ -491,7 +493,8 @@ def wrapper(wrapped, instance, args, kwargs):
491493
# to override the priority set in other parts of this hook file so that the
492494
# more explicit name takes precedence.
493495
new_name = name
494-
if WAGTAIL_PAGE and instance and isinstance(instance, Page):
496+
global WAGTAIL_PAGE
497+
if WAGTAIL_PAGE and instance and isinstance(instance, WAGTAIL_PAGE):
495498
new_name = f"{callable_name(instance)}.{wrapped.__name__}"
496499
transaction.set_transaction_name(new_name, priority=6)
497500
else:
@@ -1224,6 +1227,21 @@ def _bind_params(original_middleware, *args, **kwargs):
12241227
return _nr_wrap_converted_middleware_(converted_middleware, name)
12251228

12261229

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+
12271245
def instrument_django_core_handlers_exception(module):
12281246
if hasattr(module, "convert_exception_to_response"):
12291247
wrap_function_wrapper(module, "convert_exception_to_response", _nr_wrapper_convert_exception_to_response_)
@@ -1242,6 +1260,10 @@ def instrument_django_core_handlers_asgi(module):
12421260

12431261
wrap_asgi_application(module, "ASGIHandler.__call__", framework=framework)
12441262

1263+
12451264
def instrument_wagtail_models_pages(module):
1246-
if hasattr(module, "Page")
1265+
if hasattr(module, "Page"):
1266+
global WAGTAIL_PAGE
12471267
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)