|
| 1 | +# Quickstart |
| 2 | + |
| 3 | +This walkthrough takes you from an empty project to a **FastAPI service with |
| 4 | +production instrumentation** — metrics, health checks, CORS, Swagger, and |
| 5 | +structured logging — wired up in a few lines. The same pattern applies to |
| 6 | +[Litestar](../integrations/litestar.md), [FastStream](../integrations/faststream.md), |
| 7 | +[FastMCP](../integrations/fastmcp.md), and |
| 8 | +[framework-free services](../integrations/free.md). |
| 9 | + |
| 10 | +## 1. Install |
| 11 | + |
| 12 | +Install `lite-bootstrap` with the FastAPI extras. `fastapi-all` pulls in every |
| 13 | +supported instrument: |
| 14 | + |
| 15 | +=== "uv" |
| 16 | + |
| 17 | + ```bash |
| 18 | + uv add lite-bootstrap[fastapi-all] |
| 19 | + ``` |
| 20 | + |
| 21 | +=== "pip" |
| 22 | + |
| 23 | + ```bash |
| 24 | + pip install lite-bootstrap[fastapi-all] |
| 25 | + ``` |
| 26 | + |
| 27 | +=== "poetry" |
| 28 | + |
| 29 | + ```bash |
| 30 | + poetry add lite-bootstrap[fastapi-all] |
| 31 | + ``` |
| 32 | + |
| 33 | +Want only some instruments? See the extras matrix in |
| 34 | +[Installation](installation.md). |
| 35 | + |
| 36 | +## 2. Bootstrap your application |
| 37 | + |
| 38 | +Create `main.py`: |
| 39 | + |
| 40 | +```python |
| 41 | +from fastapi import FastAPI |
| 42 | +from lite_bootstrap import FastAPIConfig, FastAPIBootstrapper |
| 43 | + |
| 44 | +config = FastAPIConfig( |
| 45 | + service_name="my-service", |
| 46 | + service_version="1.0.0", |
| 47 | + service_environment="local", |
| 48 | + prometheus_metrics_path="/metrics", |
| 49 | + health_checks_enabled=True, |
| 50 | + health_checks_path="/health", |
| 51 | + cors_allowed_origins=["*"], |
| 52 | + swagger_offline_docs=True, |
| 53 | +) |
| 54 | + |
| 55 | +app: FastAPI = FastAPIBootstrapper(config).bootstrap() |
| 56 | + |
| 57 | + |
| 58 | +@app.get("/") |
| 59 | +async def root() -> dict[str, str]: |
| 60 | + return {"hello": "world"} |
| 61 | +``` |
| 62 | + |
| 63 | +`bootstrap()` returns a ready `FastAPI` app with every configured instrument |
| 64 | +attached. |
| 65 | + |
| 66 | +## 3. Run it |
| 67 | + |
| 68 | +```bash |
| 69 | +uvicorn main:app --reload |
| 70 | +``` |
| 71 | + |
| 72 | +## 4. What you get |
| 73 | + |
| 74 | +With the config above, your service now exposes: |
| 75 | + |
| 76 | +- **Metrics** at `/metrics` in Prometheus format — point your scraper at it. |
| 77 | +- **Health check** at `/health` — wire it to your orchestrator's readiness probe. |
| 78 | +- **Swagger UI** at `/docs` — with offline assets, so it renders without a CDN. |
| 79 | +- **CORS** headers for the configured origins. |
| 80 | +- **Structured logging** via `structlog` — enabled by default (opt out with |
| 81 | + `logging_enabled=False`). |
| 82 | + |
| 83 | +## 5. Enable more |
| 84 | + |
| 85 | +Each instrument is opt-in: add its config field to turn it on. Unset (or empty) |
| 86 | +fields are skipped automatically, so you only pay for what you enable. |
| 87 | + |
| 88 | +```python |
| 89 | +config = FastAPIConfig( |
| 90 | + service_name="my-service", |
| 91 | + service_version="1.0.0", |
| 92 | + service_environment="local", |
| 93 | + # tracing — spans exported to your OTLP endpoint |
| 94 | + opentelemetry_endpoint="http://localhost:4317", |
| 95 | + # error reporting |
| 96 | + sentry_dsn="https://examplePublicKey@o0.ingest.sentry.io/0", |
| 97 | + # continuous profiling (needs the `pyroscope` extra) |
| 98 | + pyroscope_endpoint="http://localhost:4040", |
| 99 | +) |
| 100 | +``` |
| 101 | + |
| 102 | +To see exactly which instruments were enabled or skipped at startup, inspect the |
| 103 | +bootstrapper: |
| 104 | + |
| 105 | +```python |
| 106 | +bootstrapper = FastAPIBootstrapper(config) |
| 107 | +app = bootstrapper.bootstrap() |
| 108 | + |
| 109 | +print(bootstrapper.build_summary()) # human-readable summary |
| 110 | +for cls, reason in bootstrapper.skipped_instruments: |
| 111 | + print(f"skipped {cls.__name__}: {reason}") |
| 112 | +``` |
| 113 | + |
| 114 | +## Next steps |
| 115 | + |
| 116 | +- [Configuration](configuration.md) — every config field and per-instrument option. |
| 117 | +- [Integrations](../integrations/litestar.md) — Litestar, FastStream, FastMCP, |
| 118 | + and framework-free services. |
0 commit comments