Skip to content

fix(metrics): use rule as path #150

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

Merged
merged 2 commits into from
Sep 19, 2020
Merged
Changes from all 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
8 changes: 6 additions & 2 deletions pyms/flask/services/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@ def before_request(self): # pylint: disable=R0201
request.start_time = time.time()

def after_request(self, response):
if hasattr(request.url_rule, "rule"):
Copy link
Member Author

Choose a reason for hiding this comment

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

@avara1986 Do you know if there's a real possibility that a request wouldn't have this attribute? I've tested locally and even without using dynamic values for the endpoints works. But the test doesn't, without the conditional. I'm not sure if it really should be like this and have a fallback or is the test_client that's not really complete.

Copy link
Member

Choose a reason for hiding this comment

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

I figure out this weekend :)

Copy link
Member

Choose a reason for hiding this comment

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

@alexppg the problem is not in tests. request.url_rule is None when you get 404 response. It makes sense if you visit a non-exists page therefore, you haven't a url_rule.

PR approved

PD: sorry, one week late... 🙈

path = request.url_rule.rule
else:
path = request.path
request_latency = time.time() - request.start_time
FLASK_REQUEST_LATENCY.labels(self.app_name, request.method, request.path, response.status_code).observe(request_latency)
FLASK_REQUEST_COUNT.labels(self.app_name, request.method, request.path, response.status_code).inc()
FLASK_REQUEST_LATENCY.labels(self.app_name, request.method, path, response.status_code).observe(request_latency)
FLASK_REQUEST_COUNT.labels(self.app_name, request.method, path, response.status_code).inc()

return response

Expand Down