|
1 | | -# Minimal FastAPI service for the demo overlay. All telemetry (traces, |
2 | | -# metrics, logs with trace context) comes from OTel auto-instrumentation — |
3 | | -# see compose.demo.yml. No OTel code needed here. |
| 1 | +"""Minimal FastAPI service for the demo overlay. |
| 2 | +
|
| 3 | +All telemetry (traces, metrics, logs with trace context) comes from OTel |
| 4 | +auto-instrumentation — see compose.demo.yml. No OTel code needed here. |
| 5 | +""" |
| 6 | + |
4 | 7 | import logging |
5 | 8 | import random |
6 | 9 | import time |
7 | 10 |
|
8 | 11 | from fastapi import FastAPI, HTTPException |
9 | 12 |
|
| 13 | +ERROR_RATE = 0.1 # fixed error rate, enough to light up RED panels |
| 14 | + |
10 | 15 | logging.basicConfig(level=logging.INFO) # root logger defaults to WARNING; we want the INFO lines too |
11 | 16 | log = logging.getLogger("demo-api") |
12 | 17 | app = FastAPI() |
13 | 18 |
|
14 | 19 |
|
15 | 20 | @app.get("/") |
16 | | -def root(): |
| 21 | +def root() -> dict[str, bool]: |
| 22 | + """Fast, always-successful endpoint.""" |
17 | 23 | return {"ok": True} |
18 | 24 |
|
19 | 25 |
|
20 | 26 | @app.get("/work") |
21 | | -def work(): |
22 | | - time.sleep(random.uniform(0.02, 0.3)) |
23 | | - if random.random() < 0.1: # NOTE: fixed 10% error rate, enough to light up RED panels |
| 27 | +def work() -> dict[str, bool]: |
| 28 | + """Simulate variable-latency work that sometimes fails.""" |
| 29 | + time.sleep(random.uniform(0.02, 0.3)) # noqa: S311 — not crypto, just jitter |
| 30 | + if random.random() < ERROR_RATE: # noqa: S311 |
24 | 31 | log.error("work failed: upstream flaked") |
25 | 32 | raise HTTPException(status_code=500, detail="upstream flaked") |
26 | 33 | log.info("work done") |
|
0 commit comments