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

Add http.target to Django duration metric attributes #2624

Merged
merged 8 commits into from
Jul 16, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -168,23 +168,23 @@ class _DjangoMiddleware(MiddlewareMixin):
)

@staticmethod
def _get_span_name(request):
def _get_span_name_and_route(request):
span_name = request.method
route = None
try:
if getattr(request, "resolver_match"):
match = request.resolver_match
else:
match = resolve(request.path)

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

if route := getattr(match, "route", None):
span_name += f" {route}"
elif url_name := getattr(match, "url_name", None):
span_name += f" {url_name}"
except Resolver404:
return request.method
pass

return span_name, route

# pylint: disable=too-many-locals
def process_request(self, request):
Expand Down Expand Up @@ -213,9 +213,12 @@ def process_request(self, request):
collect_request_attributes = wsgi_collect_request_attributes

attributes = collect_request_attributes(carrier)
span_name, route = self._get_span_name_and_route(request)
if route:
attributes[SpanAttributes.HTTP_ROUTE] = route
span, token = _start_internal_or_server_span(
tracer=self._tracer,
span_name=self._get_span_name(request),
span_name=span_name,
start_time=request_meta.get(
"opentelemetry-instrumentor-django.starttime_key"
),
Expand Down Expand Up @@ -290,26 +293,6 @@ def process_request(self, request):
span, request
)

# pylint: disable=unused-argument
def process_view(self, request, view_func, *args, **kwargs):
aabmass marked this conversation as resolved.
Show resolved Hide resolved
# Process view is executed before the view function, here we get the
# route template from request.resolver_match. It is not set yet in process_request
if self._excluded_urls.url_disabled(request.build_absolute_uri("?")):
return

if (
self._environ_activation_key in request.META.keys()
and self._environ_span_key in request.META.keys()
):
span = request.META[self._environ_span_key]

if span.is_recording():
match = getattr(request, "resolver_match", None)
if match:
route = getattr(match, "route", None)
if route:
span.set_attribute(SpanAttributes.HTTP_ROUTE, route)
alexmojaki marked this conversation as resolved.
Show resolved Hide resolved

def process_exception(self, request, exception):
if self._excluded_urls.url_disabled(request.build_absolute_uri("?")):
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ def test_basic_metric_success(self):
"net.host.port": 443,
"http.status_code": 200,
"http.target": "/foobar",
"http.route": "/foobar",
alexmojaki marked this conversation as resolved.
Show resolved Hide resolved
}
expected_requests_count_attributes = {
"http.method": "GET",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ def test_basic_post_request_metric_success(self):
"http.flavor": "1.1",
"http.host": "testserver",
"http.method": "POST",
"http.route": "/foobar",
"http.scheme": "http",
"http.server_name": "testserver",
"http.status_code": 405,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
SpanAttributes.HTTP_STATUS_CODE,
SpanAttributes.HTTP_FLAVOR,
SpanAttributes.HTTP_SERVER_NAME,
SpanAttributes.HTTP_ROUTE,
SpanAttributes.NET_HOST_NAME,
SpanAttributes.NET_HOST_PORT,
}
Expand Down