Skip to content

Commit 9fd7f38

Browse files
committed
Fix wagtail transaction names
1 parent 1e0a131 commit 9fd7f38

2 files changed

Lines changed: 80 additions & 1 deletion

File tree

newrelic/hooks/framework_django.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,21 @@ def wrapper(wrapped, instance, args, kwargs):
484484
if transaction is None:
485485
return wrapped(*args, **kwargs)
486486

487-
transaction.set_transaction_name(name, priority=priority)
487+
# Wagtail is built on top of Django. It uses metaclasses where the route method
488+
# is located on the base class which results in transaction having the same
489+
# name. Use the child class instead of the base class name. Set the priority=6
490+
# to override the priority set in other parts of this hook file so that the
491+
# more explicit name takes precedence.
492+
new_name = name
493+
try:
494+
from wagtail.models.pages import Page
495+
except:
496+
Page = None
497+
if instance and isinstance(instance, Page):
498+
new_name = f"{callable_name(instance)}.{wrapped.__name__}"
499+
transaction.set_transaction_name(new_name, priority=6)
500+
else:
501+
transaction.set_transaction_name(new_name, priority=priority)
488502
with FunctionTrace(name=name, source=wrapped):
489503
try:
490504
return wrapped(*args, **kwargs)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
import functools
16+
import logging
17+
import sys
18+
import threading
19+
import warnings
20+
21+
from newrelic.api.application import register_application
22+
from newrelic.api.background_task import BackgroundTaskWrapper
23+
from newrelic.api.error_trace import wrap_error_trace
24+
from newrelic.api.function_trace import FunctionTrace, FunctionTraceWrapper, wrap_function_trace
25+
from newrelic.api.html_insertion import insert_html_snippet
26+
from newrelic.api.time_trace import notice_error
27+
from newrelic.api.transaction import current_transaction
28+
from newrelic.api.transaction_name import wrap_transaction_name
29+
from newrelic.api.wsgi_application import WSGIApplicationWrapper
30+
from newrelic.common.coroutine import is_asyncio_coroutine, is_coroutine_function
31+
from newrelic.common.object_names import callable_name
32+
from newrelic.common.object_wrapper import (
33+
FunctionWrapper,
34+
function_wrapper,
35+
wrap_function_wrapper,
36+
wrap_in_function,
37+
wrap_post_function,
38+
)
39+
from newrelic.core.config import global_settings
40+
41+
_logger = logging.getLogger(__name__)
42+
43+
def _nr_wrapper_convert_exception_to_response_(wrapped, instance, args, kwargs):
44+
def _bind_params(original_middleware, *args, **kwargs):
45+
return original_middleware
46+
47+
original_middleware = _bind_params(*args, **kwargs)
48+
converted_middleware = wrapped(*args, **kwargs)
49+
name = callable_name(original_middleware)
50+
do_not_wrap = is_denied_middleware(name)
51+
52+
if do_not_wrap:
53+
return converted_middleware
54+
else:
55+
if is_coroutine_function(converted_middleware) or is_asyncio_coroutine(converted_middleware):
56+
return _nr_wrap_converted_middleware_async_(converted_middleware, name)
57+
return _nr_wrap_converted_middleware_(converted_middleware, name)
58+
59+
60+
def instrument_django_core_handlers_exception(module):
61+
if hasattr(module, "convert_exception_to_response"):
62+
wrap_function_wrapper(module, "convert_exception_to_response", _nr_wrapper_convert_exception_to_response_)
63+
64+
if hasattr(module, "handle_uncaught_exception"):
65+
module.handle_uncaught_exception = wrap_handle_uncaught_exception(module.handle_uncaught_exception)

0 commit comments

Comments
 (0)