-
Notifications
You must be signed in to change notification settings - Fork 215
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 middleware to log application name and verification status #3369
Conversation
Also add authorization, app name and verified status to nginx logs
Based on the medium urgency of this PR, the following reviewers are being gently reminded to review this PR: @obulat Excluding weekend1 days, this PR was ready for review 4 day(s) ago. PRs labelled with medium urgency are expected to be reviewed within 4 weekday(s)2. @sarayourfriend, if this PR is not ready for a review, please draft it to prevent reviewers from getting further unnecessary pings. Footnotes
|
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.
LGTM, the code works as expected! I added a small note about more docs but it's not blocking.
from api.utils.oauth2_helper import get_token_info | ||
|
||
|
||
def client_application_middleware(get_response): |
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.
A few docstrings about this middleware would be nice! Just what's in the PR description is good enough.
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 idea. I'll rename this to add "headers" in there as well. The name I've chosen to commit is bafflingly obscure 😅
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.
Works well locally, and has great tests.
A question not related to this PR: what is request_id
for? It's different every time I request something. ('"request_id":"$sent_http_x_request_id",'
).
Seeing these x_
headers here, I thought that we could also add some x_
header to all the client frontend requests to identify them instead of trying to set the header in Axios (which is not allowed as it's not safe).
|
||
@dataclass | ||
class TokenInfo: |
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.
Nice!
The ID assigned to the request is meant to help us trace an individual requests logs. In a context where logs relevant to different requests can interweave (either in an async context or a multi-worker context), it helps us determine which request caused what logs. Take the following scenario, for example, considering the same working in an async context. Request A comes in at timestamp 0 Request A will run until it reaches an At that point, Request B will run. Imagine that request B finishes completely before request A's
Without a request ID, it wouldn't be either impossible or very difficult to determine whether the error log caused by request b was caused by request B or some other request. If the two requests are totally different code paths, then that makes it possible to deduce which request it's for. But if they're for the same endpoint? It's impossible without reading further logs, and even then, only if further logs are clearly tied to a specific request via the request's context (e.g., by logging the query). Request ID eliminates the need to do this kind of mental juggling and reliance on deep knowledge of the code when trying to trace the lifecycle of a request in logs and also eliminates the possibility of two requests' logs getting mixed up altogether. |
We can identify frontend client requests in two ways:
Would that cover what you're looking to find, @obulat? |
Also add authorization, app name and verified status to nginx logs
Fixes
Closes #2246 by @sarayourfriend
Fixes #3368 by @sarayourfriend
Description
Add logging as described in the issue.
To add the application name and verified status, I added a new middleware. The tests for this are in the
test_auth
suite.Testing Instructions
You need to rebuild the nginx image to get the new logging:
just build nginx
. Afterwards, make requests to your local API via the nginx container:localhost:50270
. Follow that container's logs (just logs nginx
) and confirm that you see the following JSON log properties:request_id
client_application_name
client_application_verified
These come from the following headers, respectively, which you should also see on responses:
x-request-id
x-ov-client-application-name
x-ov-client-application-verified
Note that the last two are only present on authorized requests. Nginx will log an empty string when those headers are not present, but if you make an authorized request locally (by following the auth flow), you'll be able to see these populated both in your response headers and in Nginx's logs.
Checklist
Update index.md
).main
) or a parent feature branch.Developer Certificate of Origin
Developer Certificate of Origin