Skip to content

Commit 202c65b

Browse files
authored
Merge branch 'main' into intrument-mysqlclient
2 parents 3df2b6b + acfe932 commit 202c65b

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
### Fixed
1313

14+
- `opentelemetry-instrumentation-django` Fix empty span name when using
15+
`path("", ...)` ([#1788](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1788)
1416
- Fix elastic-search instrumentation sanitization to support bulk queries
1517
([#1870](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1870))
1618
- Update falcon instrumentation to follow semantic conventions

instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware/otel_middleware.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,12 @@ def _get_span_name(request):
172172
else:
173173
match = resolve(request.path)
174174

175-
if hasattr(match, "route"):
175+
if hasattr(match, "route") and match.route:
176176
return f"{request.method} {match.route}"
177177

178+
if hasattr(match, "url_name") and match.url_name:
179+
return f"{request.method} {match.url_name}"
180+
178181
return request.method
179182

180183
except Resolver404:

instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,14 @@
7474
DJANGO_3_0 = VERSION >= (3, 0)
7575

7676
if DJANGO_2_0:
77-
from django.urls import re_path
77+
from django.urls import path, re_path
7878
else:
7979
from django.conf.urls import url as re_path
8080

81+
def path(path_argument, *args, **kwargs):
82+
return re_path(rf"^{path_argument}$", *args, **kwargs)
83+
84+
8185
urlpatterns = [
8286
re_path(r"^traced/", traced),
8387
re_path(r"^traced_custom_header/", response_with_custom_header),
@@ -87,6 +91,7 @@
8791
re_path(r"^excluded_noarg/", excluded_noarg),
8892
re_path(r"^excluded_noarg2/", excluded_noarg2),
8993
re_path(r"^span_name/([0-9]{4})/$", route_span_name),
94+
path("", traced, name="empty"),
9095
]
9196
_django_instrumentor = DjangoInstrumentor()
9297

@@ -205,6 +210,16 @@ def test_not_recording(self):
205210
self.assertFalse(mock_span.set_attribute.called)
206211
self.assertFalse(mock_span.set_status.called)
207212

213+
def test_empty_path(self):
214+
Client().get("/")
215+
216+
spans = self.memory_exporter.get_finished_spans()
217+
self.assertEqual(len(spans), 1)
218+
219+
span = spans[0]
220+
221+
self.assertEqual(span.name, "GET empty")
222+
208223
def test_traced_post(self):
209224
Client().post("/traced/")
210225

0 commit comments

Comments
 (0)