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

For ResponseMetrics middleware, improve view tag and add tests #4746

Merged
merged 8 commits into from
Jun 4, 2024

Conversation

jwhitlock
Copy link
Member

The ResponseMetrics middleware emits the timing metric fx.private.relay.response. This gives us the application's view of the response time, bucketed by the hour. We use the view tag to segment by views, to determine trends over time and slow periods.

This PR improve the tag view in a few cases:

  • /api/v1/runtime_data, other API view functions. - The tag was api.views.privaterelay.view. This is now api.views.privaterelay.runtime_data. Other API view functions also had '.view' for the last component, and now use their function name.
  • /__heartbeat__, other Dockerflow views - The tag was <unknown_view>, and is now dockerflow.django.views.heartbeat. Similar changes are made for /__version__ and /__lbheartbeat__.
  • Frontend files - The tag was <unknown_view>, and is now <static_file>.

This PR also includes tests for ResponseMetrics.

How to test

  • I've added a unit test to test for potential regressions of this bug.

@jwhitlock jwhitlock requested a review from say-yawn May 30, 2024 16:07
Copy link
Contributor

@say-yawn say-yawn left a comment

Choose a reason for hiding this comment

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

Thanks @jwhitlock for making changes that help us get more detailed data on responses. I've made some suggestions but are not required.

This uses the logic from whitenoise.middleware.WhiteNoiseMiddleware.__call__:
https://github.com/evansd/whitenoise/blob/220a98894495d407424e80d85d49227a5cf97e1b/src/whitenoise/middleware.py#L117-L124
"""
if self.autorefresh:
Copy link
Contributor

@say-yawn say-yawn Jun 3, 2024

Choose a reason for hiding this comment

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

(suggestion, non-blocking): Since we are using data that can come from user input I recommend using shlex.quote on path_info.

Copy link
Member Author

Choose a reason for hiding this comment

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



MIDDLEWARE = _get_initial_middleware()
MIDDLEWARE = ["privaterelay.middleware.ResponseMetrics"]
Copy link
Contributor

Choose a reason for hiding this comment

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

(suggestion, non-blocking) I agree with more simple logic on initial list of middlewares but not sure if the ResponseMetrics should be used in all environment. STATSD_ENABLED is False in local:

DJANGO_STATSD_ENABLED=False
STATSD_DEBUG=False

Suggested change
MIDDLEWARE = ["privaterelay.middleware.ResponseMetrics"]
if RELAY_CHANNEL == "local":
MIDDLEWARE = []
else:
MIDDLEWARE = ["privaterelay.middleware.ResponseMetrics"]

Copy link
Member Author

Choose a reason for hiding this comment

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

I retained the spirit of the old logic by making the ResponseMetrics middleware do nothing if STATSD_ENABLED is False. This reminds me, I have a TODO to enable STATSD_DEBUG locally..

Copy link
Member Author

@jwhitlock jwhitlock left a comment

Choose a reason for hiding this comment

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

Thanks for the review @say-yawn!

Comment on lines +66 to +67
if not settings.STATSD_ENABLED:
return self.get_response(request)
Copy link
Member Author

Choose a reason for hiding this comment

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

This is where the middleware is disabled at run-time, if STATSD_ENABLED is False.

This uses the logic from whitenoise.middleware.WhiteNoiseMiddleware.__call__:
https://github.com/evansd/whitenoise/blob/220a98894495d407424e80d85d49227a5cf97e1b/src/whitenoise/middleware.py#L117-L124
"""
if self.autorefresh:
Copy link
Member Author

Choose a reason for hiding this comment

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



MIDDLEWARE = _get_initial_middleware()
MIDDLEWARE = ["privaterelay.middleware.ResponseMetrics"]
Copy link
Member Author

Choose a reason for hiding this comment

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

I retained the spirit of the old logic by making the ResponseMetrics middleware do nothing if STATSD_ENABLED is False. This reminds me, I have a TODO to enable STATSD_DEBUG locally..

@jwhitlock jwhitlock added this pull request to the merge queue Jun 4, 2024
Merged via the queue into main with commit a780a0d Jun 4, 2024
29 checks passed
@jwhitlock jwhitlock deleted the better-view-names branch June 4, 2024 18:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants