Skip to content

Commit

Permalink
Assign HttpRequest.resolver_match for dd-trace (#5)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
noelleleigh authored Oct 26, 2022
1 parent 6ee8f7e commit 19cccf2
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions django_middleware/healthchecks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 19cccf2

Please sign in to comment.