5252 "off" : False ,
5353}
5454
55+ global WAGTAIL_PAGE
56+ WAGTAIL_PAGE = None
57+
5558
5659def _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+
12301245def 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 )
0 commit comments