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

fix(django): avoid empty span name on empty path #1788

Merged
merged 18 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Fixed

- `opentelemetry-instrumentation-django` Fix empty span name when using
`path("", ...)` ([#1788](https:/open-telemetry/opentelemetry-python-contrib/pull/1788)
- Fix falcon instrumentation's usage of Span Status to only set the description if the status code is ERROR.
([#1840](https:/open-telemetry/opentelemetry-python-contrib/pull/1840))
- Instrument all httpx versions >= 0.18. ([#1748](https:/open-telemetry/opentelemetry-python-contrib/pull/1748))
Expand Down
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 f"{request.method} {match.route}"

return request.method
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,11 @@

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
def path(p, *args, **kwargs):
return re_path(r"^%s$" % p, *args, **kwargs)

urlpatterns = [
re_path(r"^traced/", traced),
Expand All @@ -87,6 +90,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 +209,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, "empty")

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

Expand Down