Skip to content

Commit

Permalink
fix(django): avoid empty span name on empty path
Browse files Browse the repository at this point in the history
when using `path('',...)`, previous code produces spans with an empty
string name. This change corrects this fallback behavior and adds a
test.
  • Loading branch information
muncus committed May 8, 2023
1 parent 890e5dd commit 8254ece
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def _get_span_name(request):
else:
match = resolve(request.path)

if hasattr(match, "route"):
if hasattr(match, "route") and match.route:
return match.route

# Instead of using `view_name`, better to use `_func_name` as some applications can use similar
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@

if DJANGO_2_0:
from django.urls import re_path
from django.urls import path
else:
from django.conf.urls import url as re_path

Expand All @@ -87,6 +88,7 @@
re_path(r"^excluded_noarg/", excluded_noarg),
re_path(r"^excluded_noarg2/", excluded_noarg2),
re_path(r"^span_name/([0-9]{4})/$", route_span_name),
path("", traced, name="empty"),
]
_django_instrumentor = DjangoInstrumentor()

Expand Down Expand Up @@ -207,6 +209,18 @@ def test_not_recording(self):
self.assertFalse(mock_span.set_attribute.called)
self.assertFalse(mock_span.set_status.called)

def test_empty_path(self):
Client().get("/")

spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 1)

span = spans[0]

self.assertEqual(
span.name, "empty"
)

def test_traced_post(self):
Client().post("/traced/")

Expand Down

0 comments on commit 8254ece

Please sign in to comment.