def test_stacktraces_have_templates(client, django_elasticapm_client):
# only Django 1.9+ have the necessary information stored on Node/Template
# instances when TEMPLATE_DEBUG = False
TEMPLATE_DEBUG = django.VERSION < (1, 9)
with mock.patch("elasticapm.traces.TransactionsStore.should_collect") as should_collect:
should_collect.return_value = False
TEMPLATES_copy = deepcopy(settings.TEMPLATES)
TEMPLATES_copy[0]['OPTIONS']['debug'] = TEMPLATE_DEBUG
with override_settings(
TEMPLATE_DEBUG=TEMPLATE_DEBUG,
TEMPLATES=TEMPLATES_copy,
**middleware_setting(django.VERSION, [
'elasticapm.contrib.django.middleware.TracingMiddleware'
])
):
resp = client.get(reverse("render-heavy-template"))
assert resp.status_code == 200
transactions = django_elasticapm_client.instrumentation_store.get_all()
assert len(transactions) == 1
transaction = transactions[0]
assert transaction['result'] == 'HTTP 2xx'
spans = transaction['spans']
assert len(spans) == 2, [t['name'] for t in spans]
expected_names = {'list_users.html', 'something_expensive'}
assert {t['name'] for t in spans} == expected_names
assert spans[0]['name'] == 'something_expensive'
# Find the template
for frame in spans[0]['stacktrace']:
if frame['lineno'] == 4 and frame['filename'].endswith(
'django/testapp/templates/list_users.html'
):
break
else:
assert False is True, "Template was not found"
评论列表
文章目录