4.5.22 · HinglishSoftware Engineering

Logging and monitoring — structured logging, metrics, alerting

2,291 words10 min readRead in English

4.5.22 · Coding › Software Engineering


YEH topic exist kyun karta hai?

Yeh teeno milke aksar observability kehlate hain — woh property ki tum external outputs se internal state infer kar sako.


Pillar 1 — Structured Logging

FARQ kya hai?

Unstructured:  "User 42 failed login from 10.0.0.3 after 3 tries"
Structured:    {"event":"login_failed","user_id":42,"ip":"10.0.0.3","attempts":3,"ts":"2024-...","level":"warn"}

Structured ek sentence se kyun behtar hai:

  • Tum ise query kar sakte ho: event=login_failed AND attempts>2. Tum ek sentence ko reliably grep nahi kar sakte.
  • Yeh stable hai: ek message ki wording badalne se tumhare dashboards nahi tootenge.
  • Yeh context fields (request_id, user_id, trace_id) carry karta hai taaki tum ek request ki journey kai services mein stitch kar sako.

Pillar 2 — Metrics

Teen classic metric types:

Type Kya karta hai Example
Counter sirf upar jaata hai (ya reset hota hai) total requests served
Gauge upar aur neeche jaata hai current memory in use
Histogram values ko bucket karta hai distribution estimate karne ke liye request latency

Counter se rate derive karna

The Four Golden Signals (monitoring ka 80/20)

Percentiles kyun, averages kyun nahi

Figure — Logging and monitoring — structured logging, metrics, alerting

Pillar 3 — Alerting

Ek achha alert kaise banta hai:

  1. Ek metric chunno jo user pain se judi ho (high error rate, high p99).
  2. Ek threshold set karo.
  3. Ek duration / "for" clause add karo taaki 1-second blip kisi ko page na kare.

Error Budgets (woh math jo SLOs ko kaam karaata hai)


Recall Feynman: 12-saal ke bachche ko explain karo

Socho tumhare video game ke andar ek chhota robot hai. Robot ek diary rakhta hai: "11:03 — player jumped, 11:04 — coin grabbed" (yeh hai logging). Woh ek scoreboard bhi rakhta hai numbers ka: coins per minute, game kitna slow feel hota hai (yeh hai metrics). Aur tum robot ko kehte ho: "Agar game poore 5 minutes ke liye super slow ho jaaye, toh ghanti bajao aur mujhe jagao" (yeh hai alerting). Diary batati hai kya hua, scoreboard batata hai kaisa chal raha hai, aur ghanti ensure karti hai ki tumhe sirf tab jagaya jaaye jab kuch actually matter kare — har baar jab koi patta hiley nahi.


Flashcards

Structured logging kya hai?
Har log event ko machine-parseable key–value records (jaise JSON) ke roop mein emit karna na ki free-form text, taaki unhe query, filter, aur aggregate kiya ja sake.
Logs se correlation/trace ID kyun attach karna chahiye?
Taaki ek request se judi saari log lines multiple services mein ek single story mein stitched ki ja sakein.
Counter vs Gauge vs Histogram?
Counter sirf badhta hai (jaise total requests); Gauge upar neeche jaata hai (jaise current memory); Histogram values ko bucket karta hai distribution estimate karne ke liye (jaise latency).
Counter ki rate measure kyun karein, uski absolute value kyun nahi?
Tumhe events-per-second ki parwah hai, jo machines ke beech comparable hai aur restarts survive karta hai; rate = ΔC / Δt.
Four Golden Signals kya hain?
Latency, Traffic, Errors, Saturation.
Average ki jagah p99 latency kyun use karein?
Average tail chhupa deta hai; p99 us slow 1% requests ko reveal karta hai jo real users actually suffer karte hain.
n sorted samples ka p-th percentile kaise compute karte hain?
Rank k = ceil(p/100 · n), phir k-th smallest value lo.
Error budget kya hai?
Allowed failure ki maatra, jo (1 − SLO) × total requests (ya window time) ke barabar hai.
99.9% SLO monthly kitna downtime allow karta hai?
(1 − 0.999) × 43200 min ≈ 43.2 minutes per month.
Alert mein "for X minutes" duration kyun add karte hain?
Noisy one-off spikes suppress karne ke liye; condition sustained rehni chahiye, thodi detection delay ke badle bahut kam false pages.
Alert fatigue kya hai aur ise kaise avoid karein?
Insaan alerts ko ignore karte hain kyunki bahut saare fire hote hain; fix: sirf actionable, symptom/budget-burn conditions par alert karo.
Symptom-based vs cause-based alerting — kaun sa preferred hai aur kyun?
Symptom-based (jo users feel karte hain, jaise error rate), kyunki causes infinite hain lekin user-facing symptoms kam hain.
Ek cheez jo tumhe logs mein KABHI nahi daalni chahiye?
Secrets / PII jaise passwords ya full card numbers.

Connections

  • Observability — woh umbrella property jo yeh teen pillars provide karte hain
  • Distributed Tracing — correlation IDs ko full request spans mein extend karta hai
  • SLI SLO SLA — service levels jinpar error budgets based hain
  • Percentiles and Distributions — p95/p99 ke peeche statistics
  • Time Series Databases — jahan metrics store hoti hain (Prometheus, etc.)
  • Incident Response — alert fire hone ke baad kya hota hai
  • Rate Limiting — same counter/rate machinery use karta hai
  • PII and Data Privacy — isliye logs scrub karte hain

Concept Map

needs visibility

pillar 1

pillar 2

pillar 3

emitted as

enables

stitches request across services

thresholds trigger

decides

controlled by

over-logging causes

Running service is a black box

Observability

Structured logging

Metrics

Alerting

JSON key-value records

Correlation ID / trace ID

Log levels DEBUG/INFO/WARN/ERROR

PII leakage risk

Wake a human