ENع
→Logs & traces
الدرس 18 · التشغيل

Logs and traces

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

14 دقيقة10 خطوات5 أسئلةAdvanced
عن هذا الدرس

Monitoring tells you that checkout got slow. Logs and traces tell you where and why. Follow one checkout request through four services: each program writes log lines that are shipped to one searchable store, a single trace id rides along in the traceparent header, and every service records a span. The span waterfall shows the database query took 950 of 1,250 ms, and searching the logs by that trace id finds the line that explains it. Finally, see how levels, retention and sampling keep the bill under control.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 18 · تفاعلي
01 / 10

One request, four services

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.

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
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. One request, four services

    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.

    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
  2. A log line

    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.

    log.info("charge start", extra=ctx)
    
    prints (stdout):
    INFO pay trace=4bf92f3577b34da6a3ce929d0e0e4736 charge start
    
    add %(asctime)s to the format for the time
  3. Levels decide what is kept

    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.

    DEBUG 10 · INFO 20 · WARNING 30 · ERROR 40 · CRITICAL 50
    level=logging.INFO: keep 20 and up
    
    log.debug("card ok", extra=ctx)    dropped (10 < 20)
    log.warning("db slow", extra=ctx)  kept (30 ≥ 20)
  4. Ship every line to one store

    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.

    program → stdout → node saves per container
    kubectl logs <pod>       one pod at a time
    agent on each node → central log store
    
    payments INFO    charge start
    payments WARNING db slow
    web      INFO    GET /checkout
    orders   INFO    order created
    web      INFO    200 in 1250 ms
    orders   INFO    charge sent
    db       LOG     duration: 950 ms
  5. Structured logs: named fields

    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.

    ctx = {"trace_id": tid}
    every line now carries trace=<id>
    
    query: level = WARNING
      payments  WARNING  db slow    (1 match)
    
    many teams print JSON: one field per key
  6. One trace id per request

    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.

    traceparent: version-traceid-parentid-flags
    00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01
    
    trace id   16 bytes, 32 hex digits, same everywhere
    parent id  8 bytes, the caller's span
    flags      01 = sampled (recorded)
  7. Spans build the waterfall

    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.

    web      GET /checkout start    0 ms  1250 ms
    orders   POST /orders  start   20 ms  1200 ms
    payments charge        start   60 ms  1120 ms
    db       SELECT        start  140 ms   950 ms
    
    parent → child: web → orders → payments → db
  8. Find the slow span

    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.

    self time = duration − children
      web       1250 − 1200 =  50 ms
      orders    1200 − 1120 =  80 ms
      payments  1120 −  950 = 170 ms
      db         950 −    0 = 950 ms
    
    db: 950 of 1250 ms = 76 %
  9. Join logs and traces

    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.

    query: trace_id = 4bf92f3577b34da6a3ce929d0e0e4736
      7 lines, from all four services
      payments  WARNING  db slow
      db        LOG      duration: 950 ms
    
    trace → where;  log → what
  10. Keep the cost down

    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.

    level:      INFO in production, not DEBUG
    retention:  delete after N days (30 here)
    sampling:   keep 1 trace in 10 (head sampling)
                01 = kept,  00 = not recorded

اختبار يتاح بعد الخطوة الأخيرة.