Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Django template tag for adding sentry tracing information #2222

Merged
merged 12 commits into from
Jul 4, 2023
6 changes: 6 additions & 0 deletions sentry_sdk/integrations/django/templates.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.template import TemplateSyntaxError
from django.utils.safestring import mark_safe
from django import VERSION as DJANGO_VERSION

from sentry_sdk import _functools, Hub
Expand Down Expand Up @@ -88,6 +89,11 @@ def render(request, template_name, context=None, *args, **kwargs):
if hub.get_integration(DjangoIntegration) is None:
return real_render(request, template_name, context, *args, **kwargs)

# Inject trace meta tags into template context
context = context or {}
if "sentry_trace_meta" not in context:
context["sentry_trace_meta"] = mark_safe(hub.trace_propagation_meta())

with hub.start_span(
op=OP.TEMPLATE_RENDER,
description=_get_template_name_description(template_name),
Expand Down
1 change: 1 addition & 0 deletions tests/integrations/django/myapp/templates/trace_meta.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ sentry_trace_meta }}
1 change: 1 addition & 0 deletions tests/integrations/django/myapp/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def path(path, *args, **kwargs):
path("template-exc", views.template_exc, name="template_exc"),
path("template-test", views.template_test, name="template_test"),
path("template-test2", views.template_test2, name="template_test2"),
path("template-test3", views.template_test3, name="template_test3"),
path("postgres-select", views.postgres_select, name="postgres_select"),
path(
"permission-denied-exc",
Expand Down
9 changes: 9 additions & 0 deletions tests/integrations/django/myapp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,15 @@ def template_test2(request, *args, **kwargs):
)


@csrf_exempt
def template_test3(request, *args, **kwargs):
from sentry_sdk import Hub

hub = Hub.current
capture_message(hub.get_traceparent() + "\n" + hub.get_baggage())
return render(request, "trace_meta.html", {})


@csrf_exempt
def postgres_select(request, *args, **kwargs):
from django.db import connections
Expand Down
20 changes: 20 additions & 0 deletions tests/integrations/django/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,26 @@ def test_read_request(sentry_init, client, capture_events):
assert "data" not in event["request"]


def test_template_tracing_meta(sentry_init, client, capture_events):
sentry_init(integrations=[DjangoIntegration()], traces_sample_rate=1.0)
events = capture_events()

# The view will capture_message the sentry-trace and baggage information
content, _, _ = client.get(reverse("template_test3"))
rendered_meta = b"".join(content).decode("utf-8")

traceparent, baggage = events[0]["message"].split("\n")
expected_meta = (
'<meta name="sentry-trace" content="%s"><meta name="baggage" content="%s">\n'
% (
traceparent,
baggage,
)
)

assert rendered_meta == expected_meta


@pytest.mark.parametrize("with_executing_integration", [[], [ExecutingIntegration()]])
def test_template_exception(
sentry_init, client, capture_events, with_executing_integration
Expand Down