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 capture of http.route to DjangoInstrumentor middleware #1226

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Changed span name extraction from request to comply semantic convention ([#992](https://github.com/open-telemetry/opentelemetry-python/pull/992))
- Added support for `OTEL_PYTHON_DJANGO_TRACED_REQUEST_ATTRS` ([#1154](https://github.com/open-telemetry/opentelemetry-python/pull/1154))
- Added capture of http.route ([#1213](https://github.com/open-telemetry/opentelemetry-python/issues/1213))
stschenk marked this conversation as resolved.
Show resolved Hide resolved

## Version 0.13b0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,23 @@ 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:
func = request.resolver_match.func
func_path = None
if not hasattr(func, "__name__"):
# A class-based view
func_path = (
func.__class__.__module__ + "." + func.__class__.__name__
)
else:
# A function-based view
func_path = func.__module__ + "." + func.__name__
span.set_attribute("http.route", func_path)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

http.route is supposed to be a route (usually templated or named) from an HTTP router. In django's case, that would be the string representation of a URL from urls.py. Looks like we are using the Python function/handler name. I'm not sure how useful this tag is but irrespective of whether we add it or not, it shouldn't be named http.route as it means something else entirely unless I'm mistaken.
Spec: https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/semantic_conventions/http.md#http-server-semantic-conventions

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What we want is the http.route to be in the span attribute. But you are correct, the value should be populated from the request path (which will be a string from urls.py). I believe it should be within the request parameter.

Copy link
Contributor

@lzchen lzchen Oct 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@stschenk stschenk Oct 12, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@owais / @lzchen Thank you for your comments.

I chose to use the class/function name that the request is being routed to so that a developer would be able to go directly from the http.route as reported in AppInsights( and other tools) to the code. But I do see that this is not in accordance with the spec so I will make the change so that http.route is set to the "matched route (path template)".

However, I do not agree that the "matched route (path template)" is the request.path as opencensus uses. This is because the request.path is the actual path of the request, not the route or path template. One path template, you could see multiple paths because the parameters in the path will vary. For instance, the following paths from request.path:
/api/1/financial-institutions/111/plaid_details/
/api/1/financial-institutions/444/plaid_details/
/api/1/financial-institutions/554/plaid_details/
would all map to one route:
^api/1/financial-institutions/(?P[^/.]+)/plaid_details/$'

I have only been able to find this route as being available from request.resolver_match.route and request.resolver_match is none during process_request() but become available during process_response and process_exception. So I think this value cannot be set just in process_request().

I will go ahead and make the change to setting http.route to request.resolver_match.route and push up the change.

Please let me know what you think. Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, I do not agree that the "matched route (path template)" is the request.path as opencensus uses.

If you are trying to add this to enable your AppInsights scenario, I suggest doing it the same way OpenCensus is doing it. OpenCensus Python SDK is Microsoft's current solution for Python SDK to send telemetry to App Insights and so being consistent with it is important for back compat and to not break users. If you want to propose a new design, I think submitting a separate feature request (after merging this PR) would be good.

Copy link
Contributor Author

@stschenk stschenk Oct 12, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lzchen - I have change the code so that it uses request.path for the http.route value. I think this make the middleware equivalent to Opencensus.

As you suggested, I will submit a separate feature request around using the route template instead of the url path

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should use templated path/route for this and stick with OpenTelemetry spec. If we decide to have same behavior of OC, we'll have same information duplicated in http.target and http.route attributes. I don't think (may be I'm wrong) the breakage here is big enough to warrant going against Otel semantic conventions. OpenCensus users can still find their old value for http.route in OpenTelemetry's http.target so no data is lost after the migration, only renamed. WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@owais
I thought request.path represented the templated path/route, but if it isn't then that is my misunderstanding. But yes, I agree with what you are saying.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Glad we are arriving at a consensus. I have switched the code back to using request.resolver_match.route which is the templated route. I also realized from looking at the OpenCensus implementation that though resolver_match is not set for process_request, it is available in process_view so I added that method.

Also http.path was being set in the OpenCensus implementation by not in the OpenTelemetry implementation so I also added that.


def process_request(self, request):
# request.META is a dictionary containing all available HTTP headers
# Read more about request.META here:
Expand Down Expand Up @@ -133,6 +150,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 +169,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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would doing this in process_request be enough and take care of both success/error cases?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have only been able to find this route as being available from request.resolver_match.route and request.resolver_match is none during process_request() but become available during process_response and process_exception. So I think this value cannot be set just in process_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,7 @@ 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 +122,7 @@ 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 +147,7 @@ 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