-
Notifications
You must be signed in to change notification settings - Fork 0
Split logging features of fastapi_log #8
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
base: 16.0-add-fastapi_log
Are you sure you want to change the base?
Split logging features of fastapi_log #8
Conversation
Hi @paradoxxxzero, |
1e08ade
to
ff9d6af
Compare
ff9d6af
to
1e08ade
Compare
Regarding the multi-slash PR maybe we should wait to include it in its entirety until we receive some feedback (and I implement some tests). I could extract the What do you think? |
Yes that's a good conservative solution. Then OCA#515 will build on the new PR to edit the I will then include the new refactoring PR instead of OCA#515. |
I moved the common method to this PR |
6632ff8
to
641d4df
Compare
@SirPyTech the commit 018f44d is missing. |
That is not how PRs are included in OCA, see https://github.com/OCA/maintainer-tools/wiki/Use-temporary-reference(s)-to-another-pull-request(s). |
Oh thanks, I didn't know |
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.
Code review: issue when processing requests with consumable bodies
api_log/models/api_log.py
Outdated
def log_request(self, request): | ||
log_request_values = { | ||
"request_url": request.url, | ||
"request_method": request.method, | ||
"request_headers": self._headers_to_dict(request.headers), | ||
"request_body": request.data, | ||
"request_date": fields.Datetime.now(), | ||
"request_time": self._current_time(), | ||
} |
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.
issue: we should check if we can read request.data
because if we don't and it is consumed then we get stuck into an infinite loop later on when calling ASGIMiddleware.__call__()
(from https://github.com/OCA/rest-framework/blob/16.0/fastapi/middleware.py)
We could solve this like that
We change the method's signature to allow other parameters, then we move part of fastapi_log.models.api_log
's implementation into the original method
def log_request(self, request): | |
log_request_values = { | |
"request_url": request.url, | |
"request_method": request.method, | |
"request_headers": self._headers_to_dict(request.headers), | |
"request_body": request.data, | |
"request_date": fields.Datetime.now(), | |
"request_time": self._current_time(), | |
} | |
def log_request(self, request, **kwargs): | |
log_request_values = { | |
"request_url": request.url, | |
"request_method": request.method, | |
"request_headers": self._headers_to_dict(request.headers), | |
"request_date": fields.Datetime.now(), | |
"request_time": self._current_time(), | |
} | |
stream = kwargs.get("fastapi_environ", {}).get("wsgi.input") | |
if stream and stream.seekable(): | |
log_request_values["request_body"] = stream.read() | |
stream.seek(0) | |
else: | |
log_request_values["request_body"] = request.data |
An easier solution would have been to copy the request
object but it doesn't have access to such a method
EDIT: nvm, request
should have access to a method called deepcopy
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.
Thanks for the proposal, I prefer to avoid adding fastapi
code in the generic module api_log
so I have added a couple of hooks, please check
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.
praise: that's actually a better way to do it, gj
641d4df
to
4e86b39
Compare
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.
Code review: good work! A couple of points
<field name="name">Fastapi Log: Read access</field> | ||
<field name="model_id" ref="model_fastapi_log" /> | ||
<field name="group_id" ref="group_fastapi_log" /> | ||
<record id="access_api_log" model="ir.model.access"> |
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.
question: does this change require a migration?
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.
Since fastapi_log
isn't merged yet, I wouldn't bother doing migrations in general.
If we want to keep the user's edits on fastapi_log
's record then a migration is needed, otherwise Odoo will remove the old record and create a new one; since this is not a record usually edited by the user I wouldn't do a migration.
@@ -6,8 +6,8 @@ | |||
--> | |||
<odoo> | |||
|
|||
<record id="group_fastapi_log" model="res.groups"> | |||
<field name="name">Fastapi Log Access</field> | |||
<record id="group_api_log" model="res.groups"> |
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.
question: (same as above)
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 response for the question above.
request = requests.Request( | ||
headers={ | ||
"Api-Key": secret_api_key, | ||
"Cookie": secret_cookie, |
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.
non-blocking: let's add another header, we should check that it does not get deleted when logged. For completeness
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.
Right, done
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.
We changed a few things in the multi-slash PR. Let's adapt to them
fastapi_endpoint = ( | ||
self.request.env["fastapi.endpoint"] | ||
.sudo() | ||
.search([("root_path", "=", root_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.
issue: the latest changes from @lmignon renamed this method to _get_endpoint
, we should adapt its use
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.
Done!
Actually, you did it 😉
Thanks
fastapi_log/models/api_log.py
Outdated
log.fastapi_endpoint_id = ( | ||
request.env["fastapi.endpoint"] | ||
.sudo() | ||
.get_endpoint(environ["PATH_INFO"]) |
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.
issue: same as above
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.
Done!
Actually, you did it 😉
Thanks
49e7872
to
11defea
Compare
This way other APIs might use the new module `api_log` to store logs.
11defea
to
05fb39b
Compare
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.
Code and functional review: LGTM
As suggested in
Originally posted by @simahawk in OCA#501 (comment)
Also add the module
fastapi_log_mail
to optionally send an email when an exception occurs.Depends on: