From 19cccf223e6fa477548cf9dd4d61edf613af1297 Mon Sep 17 00:00:00 2001 From: Noelle Leigh <5957867+noelleleigh@users.noreply.github.com> Date: Wed, 26 Oct 2022 09:07:54 -0400 Subject: [PATCH] Assign HttpRequest.resolver_match for dd-trace (#5) Once a request is determined to be a health or readiness check, its [`resolver_match`][0] attribute is assigned so that [ddtrace][1] can give its span a proper resource name ("GET /healthz" or "GET /readiness"). Referenced ddtrace implementation: [`/ddtrace/contrib/django/utils.py`][2] [0]: https://docs.djangoproject.com/en/3.2/ref/request-response/#django.http.HttpRequest.resolver_match [1]: https://pypi.org/project/ddtrace/ [2]: https://github.com/DataDog/dd-trace-py/blob/bf9587d2f4afa1485e9dd5232b23f91b941b206f/ddtrace/contrib/django/utils.py#L182-L201 --- django_middleware/healthchecks.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/django_middleware/healthchecks.py b/django_middleware/healthchecks.py index 28014ba..6e78b60 100644 --- a/django_middleware/healthchecks.py +++ b/django_middleware/healthchecks.py @@ -118,10 +118,15 @@ def __init__(self, get_response): self.get_response = get_response def __call__(self, request) -> HttpResponse: + from django.urls import ResolverMatch + if request.method != "GET" or request.path not in self.endpoints: # Passthrough if we aren't accessing health checks. return self.get_response(request) + # Add a resolver_match so that dd-trace can give the span a proper resource. + request.resolver_match = ResolverMatch(self, (request,), {}, route=request.path) + if request.path == READINESS_ENDPOINT: # Throw an exception if checks don't pass. return check_readiness()