Skip to content
This repository has been archived by the owner on Jun 11, 2018. It is now read-only.

Commit

Permalink
make it possible to override the transaction name used by Opbeat (#111)
Browse files Browse the repository at this point in the history
make it possible to override the transaction name used by Opbeat
  • Loading branch information
beniwohli committed May 9, 2016
1 parent 42d2a15 commit 832dea2
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 10 deletions.
24 changes: 14 additions & 10 deletions opbeat/contrib/django/middleware/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,19 +152,23 @@ def process_view(self, request, view_func, view_args, view_kwargs):
def process_response(self, request, response):
try:
if hasattr(response, 'status_code'):
if getattr(request, '_opbeat_view_func', False):
view_func = get_name_from_func(
request._opbeat_view_func)
else:
view_func = getattr(
request,
'_opbeat_transaction_name',
''
# check if _opbeat_transaction_name is set
if hasattr(request, '_opbeat_transaction_name'):
transaction_name = request._opbeat_transaction_name
elif getattr(request, '_opbeat_view_func', False):
transaction_name = get_name_from_func(
request._opbeat_view_func
)
else:
transaction_name = ''

status_code = response.status_code
view_func = build_name_with_http_method_prefix(view_func, request)
transaction_name = build_name_with_http_method_prefix(
transaction_name,
request
)

self.client.end_transaction(view_func, status_code)
self.client.end_transaction(transaction_name, status_code)
except Exception:
self.client.error_logger.error(
'Exception during timing of request',
Expand Down
31 changes: 31 additions & 0 deletions tests/contrib/django/django_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,37 @@ def test_ASYNC_config_raises_deprecation(self):
with self.settings(OPBEAT=config):
pytest.deprecated_call(get_client_config)

def test_request_metrics_name_override(self):
self.opbeat.instrumentation_store.get_all() # clear the store
with self.settings(
MIDDLEWARE_CLASSES=[
'opbeat.contrib.django.middleware.OpbeatAPMMiddleware',
'tests.contrib.django.testapp.middleware.MetricsNameOverrideMiddleware',
]
):
self.client.get(reverse('opbeat-no-error'))
timed_requests, _traces = self.opbeat.instrumentation_store.get_all()
timing = timed_requests[0]
self.assertEqual(
timing['transaction'],
'GET foobar'
)

def test_request_metrics_404_resolve_error(self):
self.opbeat.instrumentation_store.get_all() # clear the store
with self.settings(
MIDDLEWARE_CLASSES=[
'opbeat.contrib.django.middleware.OpbeatAPMMiddleware',
]
):
self.client.get('/i-dont-exist/')
timed_requests, _traces = self.opbeat.instrumentation_store.get_all()
timing = timed_requests[0]
self.assertEqual(
timing['transaction'],
''
)


class DjangoClientNoTempTest(TestCase):
def setUp(self):
Expand Down
6 changes: 6 additions & 0 deletions tests/contrib/django/testapp/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@ def process_response(self, request, response):
class BrokenViewMiddleware(object):
def process_view(self, request, func, args, kwargs):
raise ImportError('view')


class MetricsNameOverrideMiddleware(object):
def process_response(self, request, response):
request._opbeat_transaction_name = 'foobar'
return response

0 comments on commit 832dea2

Please sign in to comment.