Skip to content
This repository has been archived by the owner on Sep 12, 2023. It is now read-only.

Commit

Permalink
feat(logs): add duration measures to worker logs.
Browse files Browse the repository at this point in the history
Closes #180
  • Loading branch information
peterschutt committed Jan 11, 2023
1 parent f20209b commit b209a23
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/starlite_saqlalchemy/log/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ async def after_process(ctx: Context) -> None:
# parse log context from `ctx`
job: Job = ctx["job"]
log_ctx = {k: getattr(job, k) for k in settings.log.JOB_FIELDS}
# add duration measures
log_ctx["pickup_time_ms"] = job.started - job.queued
log_ctx["completed_time_ms"] = job.completed - job.started
log_ctx["total_time_ms"] = job.completed - job.queued
# emit the log
if job.error:
level = logging.ERROR
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/test_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,9 @@ async def test_after_process(job: Job, cap_logger: CapturingLogger) -> None:
"event": "Worker",
"level": "info",
"timestamp": ANY,
"pickup_time_ms": 0,
"completed_time_ms": 0,
"total_time_ms": 0,
},
)
] == cap_logger.calls
Expand Down Expand Up @@ -373,6 +376,9 @@ async def test_after_process_logs_at_error(job: Job, cap_logger: CapturingLogger
"event": "Worker",
"level": "error",
"timestamp": ANY,
"pickup_time_ms": 0,
"completed_time_ms": 0,
"total_time_ms": 0,
},
)
] == cap_logger.calls
Expand Down

0 comments on commit b209a23

Please sign in to comment.