Skip to content

Commit

Permalink
fix(django): avoid empty span name on empty path (#1788)
Browse files Browse the repository at this point in the history
Co-authored-by: Srikanth Chekuri <srikanth.chekuri92@gmail.com>
Co-authored-by: Shalev Roda <65566801+shalevr@users.noreply.github.com>
Co-authored-by: Diego Hurtado <ocelotl@users.noreply.github.com>
  • Loading branch information
4 people authored Jul 12, 2023
1 parent db90ce3 commit acfe932
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- `opentelemetry-instrumentation-django` Fix empty span name when using
`path("", ...)` ([#1788](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1788)
- Fix elastic-search instrumentation sanitization to support bulk queries
([#1870](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1870))
- Update falcon instrumentation to follow semantic conventions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,12 @@ def _get_span_name(request):
else:
match = resolve(request.path)

if hasattr(match, "route"):
if hasattr(match, "route") and match.route:
return f"{request.method} {match.route}"

if hasattr(match, "url_name") and match.url_name:
return f"{request.method} {match.url_name}"

return request.method

except Resolver404:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,14 @@
DJANGO_3_0 = VERSION >= (3, 0)

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

def path(path_argument, *args, **kwargs):
return re_path(rf"^{path_argument}$", *args, **kwargs)


urlpatterns = [
re_path(r"^traced/", traced),
re_path(r"^traced_custom_header/", response_with_custom_header),
Expand All @@ -87,6 +91,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 @@ -205,6 +210,16 @@ 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, "GET empty")

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

Expand Down

0 comments on commit acfe932

Please sign in to comment.