-
Notifications
You must be signed in to change notification settings - Fork 1
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
[WIP] gateway: Use structlog for structured logging #18
base: master
Are you sure you want to change the base?
Conversation
Makes it possible to filter logs for a particular door or request, and to monitor processing times for HTTP requests and MQTT messages
|
||
key_order = ['door', 'method', 'path', 'user', 'request_id', 'topic', 'payload'] | ||
|
||
# FIXME: add timestamping |
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.
Timestamping is added by journald/docker so it might not be needed.
) | ||
|
||
|
||
log_path = os.environ.get('DLOCK_LOG_PATH', 'logs') |
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.
Is the log file rotated with this?
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.
No but there is a handler in the standard library which has rotation. Can switch to that
## Logging helpers | ||
def log_request(sender, **extra): | ||
r, g = flask.request, flask.g | ||
|
||
# add info | ||
g.request_id = r.headers.get('X-Request-Id', str(uuid.uuid4())) | ||
g.request_start_time = time.time() | ||
|
||
log_params = request_log_params() | ||
log_params.update(dict( | ||
)) | ||
|
||
log.info('http-request-start', **log_params) | ||
|
||
def log_response(sender, response, **extra): | ||
r, g = flask.request, flask.g | ||
|
||
g.request_end_time = time.time() | ||
duration = 1000.0 * (flask.g.request_end_time - flask.g.request_start_time) | ||
|
||
log_params = request_log_params() | ||
log_params.update(dict( | ||
status_code=response.status_code, | ||
duration_ms=duration, | ||
)) | ||
|
||
log.info('http-request-end', **log_params) |
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.
If these could push at least the request path to a thread local dict that would be useful. makes it easier to correlate stuff. https://www.structlog.org/en/stable/thread-local.html
If it could add a "request_id" field which is just an uuid we can trace all logging from a specific request which is also nice.
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.
request path and request_id is included, see the request_log_param() helper. Though not all logged events have it yet. Will try to use the threadlocal mechanism instead of doing it explicitly, to get it everywhere
Makes it possible to filter logs for a particular door or request,
and to monitor processing times for HTTP requests and MQTT messages
For now the log events are quite centered around the HTTP and MQTT loops.
Would be possible to add more "application logic specific" events, if desired.
Feedback on what more information is desirable to log is welcomed.
Right now
key=value
rendering is for the output. It is possible to change this to JSON, at least for the file log.