The question
A shopper clicks Pay. The request crosses four services: web, orders, payments and the database. Monitoring shows checkout got slow, but not why. Logs and traces answer where the time went and what went wrong.
Logs
A log line is one timestamped record of one event, written by the code as it runs. Payments calls log.info("charge start"). Python's logging module adds the level and the logger name, and prints the line.
Every line has a level. Python's are DEBUG 10, INFO 20, WARNING 30, ERROR 40 and CRITICAL 50. This logger is set to INFO, so log.debug is dropped at the source and log.warning is kept.
Each program writes its lines to standard output. In Kubernetes the node saves them per container, and kubectl logs reads them back. A log agent on every node ships all the lines to one central store you can search.
Lines with named fields, like a level and trace=4bf9…, are structured logs. A machine can filter them without guessing at free text. Here extra=ctx stamps the same trace id on every payments line.
Traces
Logs belong to one program; a trace follows one request through all of them. Web picks a random 32-hex-digit trace id and passes it on in a traceparent header (the W3C Trace Context standard). Every service forwards it.
Each service records a span: one timed piece of work with a name, a start, a duration and its parent span. Payments' span is a child of orders' span. The four spans form a tree, drawn here as a waterfall.
Read the waterfall. The request took 1,250 ms and the database query alone took 950 ms. Self time is a span's duration minus its children's: the database has 950 ms, payments only 170 ms. Fix the query first.
Now join the two. Search the log store for this trace id and you get exactly this request's lines, including payments' WARNING "db slow". The trace says where the time went; the log line says what happened there.
Cost
Storing everything costs money. Teams log at INFO, not DEBUG, in production, delete old logs after a set time, and sample traces: keep, say, 1 trace in 10, decided at the first service and carried in the sampled flag.
What happens
;; step 1/10 · request web → orders → payments → db, and back monitoring: checkout is slow (a number) logs: what happened inside each program traces: where the time went, end to end
The checkout, its four services and its timings are an illustration, not a measurement. The arithmetic holds: the spans nest, and their self times add up to the whole 1,250 ms. In the W3C Trace Context header the trace id is 16 bytes (32 hex digits) and the parent id 8 bytes (16 hex digits); the last field holds flags, where 01 means sampled. The trace id used here is the example from the specification.