Skip to content

Commit dc42bdd

Browse files
committed
Add http.route to Django duration metrics
1 parent b94c590 commit dc42bdd

File tree

2 files changed

+15
-31
lines changed
  • instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware
  • util/opentelemetry-util-http/src/opentelemetry/util/http

2 files changed

+15
-31
lines changed

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

+14-31
Original file line numberDiff line numberDiff line change
@@ -168,23 +168,23 @@ class _DjangoMiddleware(MiddlewareMixin):
168168
)
169169

170170
@staticmethod
171-
def _get_span_name(request):
171+
def _get_span_name_and_route(request):
172+
span_name = request.method
173+
route = None
172174
try:
173175
if getattr(request, "resolver_match"):
174176
match = request.resolver_match
175177
else:
176178
match = resolve(request.path)
177179

178-
if hasattr(match, "route") and match.route:
179-
return f"{request.method} {match.route}"
180-
181-
if hasattr(match, "url_name") and match.url_name:
182-
return f"{request.method} {match.url_name}"
183-
184-
return request.method
185-
180+
if route := getattr(match, "route", None):
181+
span_name += f" {route}"
182+
elif url_name := getattr(match, "url_name", None):
183+
span_name += f" {url_name}"
186184
except Resolver404:
187-
return request.method
185+
pass
186+
187+
return span_name, route
188188

189189
# pylint: disable=too-many-locals
190190
def process_request(self, request):
@@ -213,9 +213,12 @@ def process_request(self, request):
213213
collect_request_attributes = wsgi_collect_request_attributes
214214

215215
attributes = collect_request_attributes(carrier)
216+
span_name, route = self._get_span_name_and_route(request)
217+
if route:
218+
attributes[SpanAttributes.HTTP_ROUTE] = route
216219
span, token = _start_internal_or_server_span(
217220
tracer=self._tracer,
218-
span_name=self._get_span_name(request),
221+
span_name=span_name,
219222
start_time=request_meta.get(
220223
"opentelemetry-instrumentor-django.starttime_key"
221224
),
@@ -290,26 +293,6 @@ def process_request(self, request):
290293
span, request
291294
)
292295

293-
# pylint: disable=unused-argument
294-
def process_view(self, request, view_func, *args, **kwargs):
295-
# Process view is executed before the view function, here we get the
296-
# route template from request.resolver_match. It is not set yet in process_request
297-
if self._excluded_urls.url_disabled(request.build_absolute_uri("?")):
298-
return
299-
300-
if (
301-
self._environ_activation_key in request.META.keys()
302-
and self._environ_span_key in request.META.keys()
303-
):
304-
span = request.META[self._environ_span_key]
305-
306-
if span.is_recording():
307-
match = getattr(request, "resolver_match", None)
308-
if match:
309-
route = getattr(match, "route", None)
310-
if route:
311-
span.set_attribute(SpanAttributes.HTTP_ROUTE, route)
312-
313296
def process_exception(self, request, exception):
314297
if self._excluded_urls.url_disabled(request.build_absolute_uri("?")):
315298
return

util/opentelemetry-util-http/src/opentelemetry/util/http/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
SpanAttributes.HTTP_STATUS_CODE,
4646
SpanAttributes.HTTP_FLAVOR,
4747
SpanAttributes.HTTP_SERVER_NAME,
48+
SpanAttributes.HTTP_ROUTE,
4849
SpanAttributes.NET_HOST_NAME,
4950
SpanAttributes.NET_HOST_PORT,
5051
}

0 commit comments

Comments
 (0)