-
Notifications
You must be signed in to change notification settings - Fork 623
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
Add capture of http.route to DjangoInstrumentor middleware #1226
Conversation
@@ -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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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().
else: | ||
# A function-based view | ||
func_path = func.__module__ + "." + func.__name__ | ||
span.set_attribute("http.route", func_path) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See Opencensus
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
instrumentation/opentelemetry-instrumentation-django/CHANGELOG.md
Outdated
Show resolved
Hide resolved
Address #1213 |
Co-authored-by: Leighton Chen <[email protected]>
span = request.META[self._environ_span_key] | ||
|
||
if span.is_recording(): | ||
if getattr(request, "resolver_match") and getattr( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: hasattr
might be better for this if we aren't using the return val directly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used getattr because I also don't want to capture the value if it is None.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
match = getattr(request, "resolved_match")
if match:
route = getattr(match, "route")
if route:
set_attribute()
Perhaps use this? I know it's usually not a big deal but we never know where the library will be used and better to reduce attribute look ups if we can in case the code ends up on a very hot path.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
makes sense... I have not thought about reducing attribute lookups before. Thanks!
@@ -121,6 +122,7 @@ def test_traced_post(self): | |||
self.assertEqual( | |||
span.attributes["http.url"], "http://testserver/traced/" | |||
) | |||
self.assertEqual(span.attributes["http.route"], "^traced/") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would be nice to add a templated route so tests cover that and catch any regressions in future.
@@ -117,6 +117,7 @@ def process_request(self, request): | |||
) | |||
for key, value in attributes.items(): | |||
span.set_attribute(key, value) | |||
span.set_attribute("http.path", request.path) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be http.target
according to the spec. https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/semantic_conventions/http.md#common-attributes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sound good.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't http.target
already being populated in collect_request_attributes
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point @lzchen! I will remove the setting of the attribute from the django middleware.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One last comment about using http.target
instead of http.path
…//github.com/stschenk/opentelemetry-python into add_http.route_capture_to_DjangoInstrumentor
Description DjangoInstrumentor middleware so that it captures the resolved function path for the route as the span attribute http.route. This will allow the function path to be shown used as an organizing operation name in AppInsights.
Extends
Fixes # (issue)
Type of change
Please delete options that are not relevant.
How Has This Been Tested?
Extended instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py to cover this change
Checklist: