python student-tests/incident_db.py
A. slow HTTP handler, healthy DB (fault api_slow_handler=1.5 s in every api replica)
   request 5251b25d1bb54d0b92d0ddf565ecf373 -> 201; log event=http.request severity=INFO; trace b3002e4e6c367387a94d2cfe9c1c19f4
     messenger-api    SERVER   POST /api/v1/chats/{chat_id}/messages              1509.8 ms  route=/api/v1/chats/{chat_id}/messages status_code=201
       messenger-api    CLIENT   SQL messenger                                         0.6 ms
       messenger-api    CLIENT   SQL INSERT                                            1.3 ms
   server span 1510 ms, of it in SQL 1.9 ms (slowest SQL INSERT 1.3 ms): the time is spent in the handler, not in the DB
   metrics: p95 POST messages = 0.073 s, app 5xx/s = 0, pool waiting = 0
B. slow DB: a session holds LOCK TABLE messages; POST message waits until statement_timeout (5 s) -> 503
   t=   20s edge_5xx_ratio=     0 app_5xx/s=     0 app_rps= 4.375 p95_messages= 0.042s pool_used=  0 pool_waiting=  0 ready_replicas= 2 HighErrorRate=inactive http={'201': 264}
   t=20s lock taken
   request 2bf9d5bf4786487ab0099233a896a582 -> 503; log event=http.request severity=WARN; trace 0c50ab02599e0932544e54b368230bd6
     messenger-api    SERVER   POST /api/v1/chats/{chat_id}/messages              5012.5 ms ERROR route=/api/v1/chats/{chat_id}/messages status_code=503
       messenger-api    CLIENT   SQL messenger                                         0.5 ms
       messenger-api    CLIENT   SQL SELECT                                            1.6 ms
       messenger-api    CLIENT   SQL messenger                                         0.5 ms
       messenger-api    CLIENT   SQL INSERT                                         5001.6 ms ERROR
   server span 5012 ms, of it in SQL 5004 ms; slowest SQL INSERT 5002 ms status ERROR: the DB is slow
   t=   40s edge_5xx_ratio=     0 app_5xx/s= 0.095 app_rps= 5.429 p95_messages= 0.033s pool_used=  2 pool_waiting=  0 ready_replicas= 2 HighErrorRate=inactive http={'201': 267, '503': 9}
   t=   55s edge_5xx_ratio= 0.009 app_5xx/s= 0.264 app_rps=   5.6 p95_messages= 0.048s pool_used=  3 pool_waiting=  0 ready_replicas= 2 HighErrorRate=inactive http={'201': 267, '503': 18}
   t=   70s edge_5xx_ratio= 0.027 app_5xx/s= 0.422 app_rps= 1.661 p95_messages= 7.008s pool_used=  3 pool_waiting=  0 ready_replicas= 2 HighErrorRate=inactive http={'201': 267, '503': 27}
   t=   85s edge_5xx_ratio= 0.055 app_5xx/s= 0.578 app_rps= 0.578 p95_messages= 7.375s pool_used=  3 pool_waiting=  0 ready_replicas= 2 HighErrorRate=inactive http={'201': 267, '503': 36}
   t=  100s edge_5xx_ratio=   0.1 app_5xx/s= 0.599 app_rps= 0.599 p95_messages= 7.375s pool_used=  3 pool_waiting=  0 ready_replicas= 2 HighErrorRate=pending  http={'201': 267, '503': 45}
   t=  115s edge_5xx_ratio= 0.117 app_5xx/s= 0.599 app_rps= 0.599 p95_messages= 7.375s pool_used=  3 pool_waiting=  0 ready_replicas= 2 HighErrorRate=pending  http={'201': 267, '503': 54}
   t=  130s edge_5xx_ratio= 0.163 app_5xx/s=   0.6 app_rps=   0.6 p95_messages= 7.375s pool_used=  3 pool_waiting=  0 ready_replicas= 2 HighErrorRate=pending  http={'201': 267, '503': 63}
   t=  145s edge_5xx_ratio= 0.655 app_5xx/s=   0.6 app_rps=   0.6 p95_messages= 7.375s pool_used=  2 pool_waiting=  0 ready_replicas= 2 HighErrorRate=pending  http={'201': 267, '503': 69}
   t=  160s edge_5xx_ratio=     1 app_5xx/s=  0.54 app_rps=  0.54 p95_messages= 7.375s pool_used=  3 pool_waiting=  0 ready_replicas= 2 HighErrorRate=firing   http={'201': 267, '503': 78}
   HighErrorRate FIRING at t=160s (140 s after the lock)
   t=161s lock released (pg_cancel_backend)
   t=  161s edge_5xx_ratio=     1 app_5xx/s=  0.54 app_rps=  0.54 p95_messages= 7.375s pool_used=  3 pool_waiting=  0 ready_replicas= 2 HighErrorRate=firing   http={'201': 270, '503': 78}
   t=  176s edge_5xx_ratio=     1 app_5xx/s= 0.465 app_rps= 1.894 p95_messages= 6.991s pool_used=  0 pool_waiting=  0 ready_replicas= 2 HighErrorRate=firing   http={'201': 466, '503': 78}
   t=  191s edge_5xx_ratio=  0.36 app_5xx/s= 0.235 app_rps= 6.123 p95_messages= 2.043s pool_used=  4 pool_waiting=  2 ready_replicas= 2 HighErrorRate=firing   http={'201': 616, '503': 78}
   t=  206s edge_5xx_ratio= 0.143 app_5xx/s= 0.074 app_rps=10.397 p95_messages=  0.04s pool_used=  0 pool_waiting=  0 ready_replicas= 2 HighErrorRate=firing   http={'201': 815, '503': 78}
   t=  221s edge_5xx_ratio= 0.081 app_5xx/s=     0 app_rps=12.217 p95_messages= 0.025s pool_used=  0 pool_waiting=  0 ready_replicas= 2 HighErrorRate=firing   http={'201': 1013, '503': 78}
   t=  236s edge_5xx_ratio= 0.045 app_5xx/s=     0 app_rps=12.284 p95_messages= 0.025s pool_used=  0 pool_waiting=  0 ready_replicas= 2 HighErrorRate=firing   http={'201': 1212, '503': 78}
   t=  251s edge_5xx_ratio= 0.024 app_5xx/s=     0 app_rps=13.186 p95_messages= 0.024s pool_used=  1 pool_waiting=  0 ready_replicas= 2 HighErrorRate=inactive http={'201': 1410, '503': 78}
   HighErrorRate RESOLVED at t=251s (90 s after the release); http codes: {'201': 1410, '503': 78}
Summary: how to tell them apart
   slow handler: server span 1510 ms, SQL 2 ms, no 5xx, pool free
   slow DB:      server span 5012 ms, SQL 5004 ms (ERROR, statement timeout), 5xx, pool connections busy; HighErrorRate firing in 140 s, resolved 90 s after the fix
PASS: slow DB vs slow handler distinguished by trace (time in SQL spans) and metrics (5xx, pool); HighErrorRate firing -> resolved
