question 474 Observability & Operations · medium
The slowest requests in the log
During a latency incident you want the slowest requests first, with their trace ids, so you can open their traces. Read the API's JSON log lines and list the n slowest completed requests.
Function
slowest(lines, n)Requesta JSON object line with
message == "request finished" and numeric ms, trace and path fields; other lines (other messages, non-JSON text) are ignoredReturnsup to
n tuples (trace, path, ms), slowest first; equal times by trace idExamples
callslowest(['{"message":"request finished","trace":"t-7f3a","path":"/repos/lumen-labs/pyauth/stars","ms":2105}', '{"message":"request started","trace":"t-91c0","path":"/repos/tidewater/easystore"}', '{"message":"request finished","trace":"t-91c0","path":"/repos/tidewater/easystore","ms":88}', 'WARNING: connection pool is almost full', '{"message":"request finished","trace":"t-2b55","path":"/repos/nimbus-io/cloudplot","ms":1840}'], 2)returns[('t-7f3a', '/repos/lumen-labs/pyauth/stars', 2105), ('t-2b55', '/repos/nimbus-io/cloudplot', 1840)]
callslowest(['{"message":"request finished","trace":"a","path":"/x","ms":5}'], 3)returns[('a', '/x', 5)]
callslowest([], 1)returns[]