Skip to content

Commit

Permalink
Revert changes to getting route
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmojaki committed Jul 16, 2024
1 parent 1bc4146 commit 00bf613
Showing 1 changed file with 35 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,23 +168,23 @@ class _DjangoMiddleware(MiddlewareMixin):
)

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

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:
pass
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 span_name, route
return request.method

except Resolver404:
return request.method

# pylint: disable=too-many-locals
# pylint: disable=too-many-branches
Expand Down Expand Up @@ -214,12 +214,9 @@ 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=span_name,
span_name=self._get_span_name(request),
start_time=request_meta.get(
"opentelemetry-instrumentor-django.starttime_key"
),
Expand All @@ -232,8 +229,6 @@ def process_request(self, request):
attributes
)
duration_attrs = _parse_duration_attrs(attributes)
if route:
duration_attrs[SpanAttributes.HTTP_TARGET] = route

request.META[self._environ_active_request_attr_key] = (
active_requests_count_attrs
Expand Down Expand Up @@ -301,6 +296,30 @@ def process_request(self, request):
# would not be called. Log the exception instead.
_logger.exception("Exception raised by request_hook")

# pylint: disable=unused-argument
def process_view(self, request, view_func, *args, **kwargs):
# 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)
duration_attrs = request.META[
self._environ_duration_attr_key
]
duration_attrs[SpanAttributes.HTTP_TARGET] = route

def process_exception(self, request, exception):
if self._excluded_urls.url_disabled(request.build_absolute_uri("?")):
return
Expand Down

0 comments on commit 00bf613

Please sign in to comment.