Skip to content

Commit b54540e

Browse files
authored
Add capture of http.route to DjangoInstrumentor middleware (open-telemetry#1226)
1 parent f669b67 commit b54540e

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

instrumentation/opentelemetry-instrumentation-django/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Released 2020-10-13
1010

1111
- Changed span name extraction from request to comply semantic convention ([#992](https://github.com/open-telemetry/opentelemetry-python/pull/992))
1212
- Added support for `OTEL_PYTHON_DJANGO_TRACED_REQUEST_ATTRS` ([#1154](https://github.com/open-telemetry/opentelemetry-python/pull/1154))
13+
- Added capture of http.route ([#1226](https://github.com/open-telemetry/opentelemetry-python/issues/1226))
1314

1415
## Version 0.13b0
1516

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,26 @@ def process_request(self, request):
125125
request.META[self._environ_span_key] = span
126126
request.META[self._environ_token] = token
127127

128+
# pylint: disable=unused-argument
129+
def process_view(self, request, view_func, *args, **kwargs):
130+
# Process view is executed before the view function, here we get the
131+
# route template from request.resolver_match. It is not set yet in process_request
132+
if self._excluded_urls.url_disabled(request.build_absolute_uri("?")):
133+
return
134+
135+
if (
136+
self._environ_activation_key in request.META.keys()
137+
and self._environ_span_key in request.META.keys()
138+
):
139+
span = request.META[self._environ_span_key]
140+
141+
if span.is_recording():
142+
match = getattr(request, "resolver_match")
143+
if match:
144+
route = getattr(match, "route")
145+
if route:
146+
span.set_attribute("http.route", route)
147+
128148
def process_exception(self, request, exception):
129149
# Django can call this method and process_response later. In order
130150
# to avoid __exit__ and detach from being called twice then, the

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@
3636
excluded_noarg2,
3737
route_span_name,
3838
traced,
39+
traced_template,
3940
)
4041

4142
DJANGO_2_2 = VERSION >= (2, 2)
4243

4344
urlpatterns = [
4445
url(r"^traced/", traced),
46+
url(r"^route/(?P<year>[0-9]{4})/template/$", traced_template),
4547
url(r"^error/", error),
4648
url(r"^excluded_arg/", excluded),
4749
url(r"^excluded_noarg/", excluded_noarg),
@@ -68,6 +70,35 @@ def tearDown(self):
6870
teardown_test_environment()
6971
_django_instrumentor.uninstrument()
7072

73+
def test_templated_route_get(self):
74+
Client().get("/route/2020/template/")
75+
76+
spans = self.memory_exporter.get_finished_spans()
77+
self.assertEqual(len(spans), 1)
78+
79+
span = spans[0]
80+
81+
self.assertEqual(
82+
span.name,
83+
"^route/(?P<year>[0-9]{4})/template/$"
84+
if DJANGO_2_2
85+
else "tests.views.traced",
86+
)
87+
self.assertEqual(span.kind, SpanKind.SERVER)
88+
self.assertEqual(span.status.canonical_code, StatusCanonicalCode.OK)
89+
self.assertEqual(span.attributes["http.method"], "GET")
90+
self.assertEqual(
91+
span.attributes["http.url"],
92+
"http://testserver/route/2020/template/",
93+
)
94+
self.assertEqual(
95+
span.attributes["http.route"],
96+
"^route/(?P<year>[0-9]{4})/template/$",
97+
)
98+
self.assertEqual(span.attributes["http.scheme"], "http")
99+
self.assertEqual(span.attributes["http.status_code"], 200)
100+
self.assertEqual(span.attributes["http.status_text"], "OK")
101+
71102
def test_traced_get(self):
72103
Client().get("/traced/")
73104

@@ -85,6 +116,7 @@ def test_traced_get(self):
85116
self.assertEqual(
86117
span.attributes["http.url"], "http://testserver/traced/"
87118
)
119+
self.assertEqual(span.attributes["http.route"], "^traced/")
88120
self.assertEqual(span.attributes["http.scheme"], "http")
89121
self.assertEqual(span.attributes["http.status_code"], 200)
90122
self.assertEqual(span.attributes["http.status_text"], "OK")
@@ -121,6 +153,7 @@ def test_traced_post(self):
121153
self.assertEqual(
122154
span.attributes["http.url"], "http://testserver/traced/"
123155
)
156+
self.assertEqual(span.attributes["http.route"], "^traced/")
124157
self.assertEqual(span.attributes["http.scheme"], "http")
125158
self.assertEqual(span.attributes["http.status_code"], 200)
126159
self.assertEqual(span.attributes["http.status_text"], "OK")
@@ -145,6 +178,7 @@ def test_error(self):
145178
self.assertEqual(
146179
span.attributes["http.url"], "http://testserver/error/"
147180
)
181+
self.assertEqual(span.attributes["http.route"], "^error/")
148182
self.assertEqual(span.attributes["http.scheme"], "http")
149183

150184
@patch(

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ def traced(request): # pylint: disable=unused-argument
55
return HttpResponse()
66

77

8+
def traced_template(request, year): # pylint: disable=unused-argument
9+
return HttpResponse()
10+
11+
812
def error(request): # pylint: disable=unused-argument
913
raise ValueError("error")
1014

0 commit comments

Comments
 (0)