Reporting and Observing Agent Runs Honestly
Lesson 3 of 4
One Correlation Id Per Run
Estimated time: 10 minutes
One correlation id per run
A log line with no identifier is a sentence with no speaker. When a run fails, the useful question is which other lines belong to it, and free text cannot answer that.
Mint one correlation id at the entry point of each logical run and attach it to every line and every downstream call.
import uuid, logging
def run_with_id(job, handler):
run_id = str(uuid.uuid4())[:8]
log = logging.getLogger("agent")
log.info("start", extra={"run_id": run_id, "job": job})
try:
return handler(job, run_id)
finally:
log.info("end", extra={"run_id": run_id, "job": job})
Emit fields, not prose. A sentence is readable by a person and useless to a group by clause; the same data as level, run id, component and message serves both.