Skip to content

Commit

Permalink
Add capture of http.route to DjangoInstrumentor middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
stschenk committed Oct 8, 2020
1 parent affe911 commit 12da4f6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ def _get_span_name(request):

except Resolver404:
return "HTTP {}".format(request.method)

@staticmethod
def _set_http_route(span, request):
# Use the resolved fuction path for the http.route
# Note: resolved_match is not available during the process_request phase.
if request.resolver_match is not None:
span.set_attribute("http.route", request.resolver_match._func_path)

def process_request(self, request):
# request.META is a dictionary containing all available HTTP headers
Expand Down Expand Up @@ -133,6 +140,7 @@ def process_exception(self, request, exception):
return

if self._environ_activation_key in request.META.keys():
self._set_http_route(request.META[self._environ_span_key], request)
request.META[self._environ_activation_key].__exit__(
type(exception),
exception,
Expand All @@ -151,6 +159,7 @@ def process_response(self, request, response):
self._environ_activation_key in request.META.keys()
and self._environ_span_key in request.META.keys()
):
self._set_http_route(request.META[self._environ_span_key], request)
add_response_attributes(
request.META[self._environ_span_key],
"{} {}".format(response.status_code, response.reason_phrase),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ def test_traced_get(self):
self.assertEqual(
span.attributes["http.url"], "http://testserver/traced/"
)
self.assertEqual(
span.attributes["http.route"], "tests.views.traced"
)
self.assertEqual(span.attributes["http.scheme"], "http")
self.assertEqual(span.attributes["http.status_code"], 200)
self.assertEqual(span.attributes["http.status_text"], "OK")
Expand Down Expand Up @@ -121,6 +124,9 @@ def test_traced_post(self):
self.assertEqual(
span.attributes["http.url"], "http://testserver/traced/"
)
self.assertEqual(
span.attributes["http.route"], "tests.views.traced"
)
self.assertEqual(span.attributes["http.scheme"], "http")
self.assertEqual(span.attributes["http.status_code"], 200)
self.assertEqual(span.attributes["http.status_text"], "OK")
Expand All @@ -145,6 +151,9 @@ def test_error(self):
self.assertEqual(
span.attributes["http.url"], "http://testserver/error/"
)
self.assertEqual(
span.attributes["http.route"], "tests.views.error"
)
self.assertEqual(span.attributes["http.scheme"], "http")

@patch(
Expand Down

0 comments on commit 12da4f6

Please sign in to comment.