Skip to content
This repository was archived by the owner on Sep 17, 2025. It is now read-only.

Fixes value for http.route in Flask middleware#759

Merged
reyang merged 2 commits intocensus-instrumentation:masterfrom
olance:fix/flask_http.route
Aug 13, 2019
Merged

Fixes value for http.route in Flask middleware#759
reyang merged 2 commits intocensus-instrumentation:masterfrom
olance:fix/flask_http.route

Conversation

@olance
Copy link
Copy Markdown
Contributor

@olance olance commented Aug 10, 2019

The OpenCensus specs suggest that the http.route attribute should contain the URL template/route that was matched for the current request.
Here, the request's path is used, making it a duplicate of http.path.

Having the http.route correctly reported is quite useful to easily filter for all traces of a same endpoint.
On Stackdriver Trace for instance, one can only filter by prefix or exact match, making it impossible to filter for endpoints like /resources/<resource_id>/whatever unless you've got a common denominator such as the route itself.

This PR fixes the reported attribute value for http.route in the Flask middleware by moving the addition of the attribute to the after_request handler, where flask.request.url_rule is available.

Tests are fixed/improved accordingly

olance and others added 2 commits August 11, 2019 01:10
The [OpenCensus specs](https://github.com/census-instrumentation/opencensus-specs/blob/master/trace/HTTP.md#attributes) suggest that the `http.route` attribute should contain the URL **template**/route that was matched for the current request.
Here, the request's path is used, making it a duplicate of `http.path`.

Having the `http.route` correctly reported is quite useful to easily filter for all traces of a same endpoint.
On Stackdriver Trace for instance, one can only filter by prefix or exact match, making it impossible to filter for endpoints like `/resources/<resource_id>/whatever` unless you've got a common denominator such as the route itself.
@olance olance requested review from a team, c24t, reyang and songy23 as code owners August 10, 2019 23:13
@googlebot
Copy link
Copy Markdown

We found a Contributor License Agreement for you (the sender of this pull request), but were unable to find agreements for all the commit author(s) or Co-authors. If you authored these, maybe you used a different email address in the git commits than was used to sign the CLA (login here to double check)? If these were authored by someone else, then they will need to sign a CLA as well, and confirm that they're okay with these being contributed to Google.
In order to pass this check, please resolve this problem and then comment @googlebot I fixed it.. If the bot doesn't comment, it means it doesn't think anything has changed.

ℹ️ Googlers: Go here for more info.

@olance
Copy link
Copy Markdown
Contributor Author

olance commented Aug 10, 2019

@googlebot I fixed it.

@googlebot
Copy link
Copy Markdown

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

flask_middleware.FlaskMiddleware(app=app)
flask_middleware.FlaskMiddleware(
app=app,
sampler=samplers.AlwaysOnSampler()
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Note: Adding the AlwaysOnSampler made sure the request was always sampled, which did not seem to be the case before

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@reyang don't know if we should do this, this was changed by @c24t here. Waiting for his opinion on this.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

See comments here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@victoraugustolls I’m aligned with @c24t comments regarding sampling on a production app, however I’m not sure how that’s a problem in tests?

The thing is, until I did this the tests wouldn’t pass because the default sampler has such a low sampling probability that the test span would actually never be created. I do think in the case of unit tests, we’d rather have deterministic execution than chance-driven tests 🙂

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Even being tests, I would change for 100% Probability Sampler because:

AlwaysOnSampler is redundant though, and AFAIK just exists for completeness.

So who knows, in the future it may change, although I don't think it will!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@victoraugustolls I’m aligned with @c24t comments regarding sampling on a production app, however I’m not sure how that’s a problem in tests?

The thing is, until I did this the tests wouldn’t pass because the default sampler has such a low sampling probability that the test span would actually never be created. I do think in the case of unit tests, we’d rather have deterministic execution than chance-driven tests slightly_smiling_face

Wow, what a luck :D, never happened to me!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

+1 on having more deterministic execution in test case.

@reyang
Copy link
Copy Markdown
Contributor

reyang commented Aug 12, 2019

@victoraugustolls please help to review this change.

@victoraugustolls
Copy link
Copy Markdown
Contributor

Other than my comment on the sampler changes, everything looks great.

Copy link
Copy Markdown
Contributor

@reyang reyang left a comment

Choose a reason for hiding this comment

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

LGTM, thanks @olance!

try:
tracer = execution_context.get_opencensus_tracer()
tracer.add_attribute_to_current_span(
HTTP_ROUTE, flask.request.url_rule.rule
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Note that this will throw if url_rule is None, which it will for any 404 route. I have raised #777 for this

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants