question 479 Observability & Operations · hard
A trace report from span logs
The services log one JSON line per finished span. Combine parsing (L01), spans (L03) and investigation (L05) into one report: for each trace, how many spans it had, how long its root span took, whether anything failed, and which step had the largest self time.
Function
trace_report(lines)Span linea JSON object with
trace, span, parent (null for the root), name, ms and error (null or an error type); other lines are skippedReturns
{trace: {"spans": n, "ms": root ms, "failed": True/False, "hotspot": name}} with traces in sorted orderhotspotthe span with the largest self time (
ms minus its direct children's ms); ties by namefailed
True when any span of the trace has a non-null errorExamples
calltrace_report(['{"trace": "t-7f3a", "span": "s2", "parent": "s1", "name": "check token", "ms": 8, "error": null}', '{"trace": "t-7f3a", "span": "s4", "parent": "s3", "name": "sqlite execute", "ms": 2000, "error": "OperationalError"}', '{"trace": "t-7f3a", "span": "s3", "parent": "s1", "name": "query stars", "ms": 2010, "error": "OperationalError"}', '{"trace": "t-7f3a", "span": "s1", "parent": null, "name": "GET /stars", "ms": 2025, "error": "TimeoutError"}', 'WARNING: connection pool is almost full', '{"trace": "t-91c0", "span": "s1", "parent": null, "name": "GET /stars", "ms": 88, "error": null}', '{"trace": "t-91c0", "span": "s2", "parent": "s1", "name": "render json", "ms": 20, "error": null}'])returns{'t-7f3a': {'spans': 4, 'ms': 2025, 'failed': True, 'hotspot': 'sqlite execute'}, 't-91c0': {'spans': 2, 'ms': 88, 'failed': False, 'hotspot': 'GET /stars'}}
calltrace_report(['{"trace": "a", "span": "1", "parent": null, "name": "job", "ms": 5, "error": "KeyError"}'])returns{'a': {'spans': 1, 'ms': 5, 'failed': True, 'hotspot': 'job'}}
calltrace_report([])returns{}