Skip to content

docs: Use lifespan instead of deprecated on_event in README - #403

Open
fjvbn2003 wants to merge 1 commit into
trallnag:masterfrom
fjvbn2003:docs-lifespan-readme
Open

fjvbn2003 wants to merge 1 commit into
trallnag:masterfrom
fjvbn2003:docs-lifespan-readme

Conversation

@fjvbn2003

@fjvbn2003 fjvbn2003 commented Oct 16, 2026 •

Copy link
Copy Markdown

The fast-track section suggests exposing the metrics endpoint from @app.on_event("startup"). FastAPI marks on_event as deprecated, and every use emits a DeprecationWarning ("on_event is deprecated, use lifespan event handlers instead"). Issue #292 discussed the lifespan-based replacement, but the README was not updated.

This replaces the snippet with a lifespan handler and adds one sentence explaining why instrument() must stay outside of the handler. Calling it inside the lifespan fails with RuntimeError: Cannot add middleware after an application has started. It also fixes a small typo ("will to nothing" → "will do nothing").

Verified with fastapi 0.141.1, starlette 1.7.0, prometheus-fastapi-instrumentator 8.1.0, and prometheus-client 0.26.0 (TestClient):

  • old snippet: works, but emits DeprecationWarning from on_event
  • new snippet: with DeprecationWarning turned into an error, the app starts, and /metrics returns 200 with http_requests_total{handler="/",method="GET",status="2xx"} 1.0
  • Instrumentator().instrument(app) inside lifespan: RuntimeError: Cannot add middleware after an application has started

The broken CONTRIBUTING link is left alone because #325 already fixes it.

I used an AI assistant (Claude) to help find and draft this change. The checks above were also run with Claude's help.

🤖 Generated with Claude Code

🤖 Generated with Claude Code

FastAPI deprecates on_event and warns on every use. Show the
lifespan-based setup instead, and note that instrument() has to run
before the app starts (calling it inside lifespan raises RuntimeError).

Signed-off-by: YoungjuKim <fjvbn2003@gmail.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant