Repository navigation
feat: record FastMCP HTTP request and tool call metrics - #281
Merged
Merged
Conversation
lesnik512
force-pushed
the
feat/fastmcp-prometheus-metrics
branch
from
October 9, 2026 19:18
9a557a3 to
2fe7c2b
Compare
FastMcpPrometheusInstrument runs prometheus-fastapi-instrumentator on every ASGI application http_app() builds, labelled by route and excluding the metrics route, and a FastMCP middleware counts and times tool calls in fastmcp_tool_calls_total and fastmcp_tool_call_duration_seconds. The fastmcp-metrics extra now requires prometheus-fastapi-instrumentator.
lesnik512
force-pushed
the
feat/fastmcp-prometheus-metrics
branch
from
October 9, 2026 19:21
2fe7c2b to
1eb1163
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Builds on #280's
_postprocess_http_appshook (merged).Why
FastMcpPrometheusInstrumentonly serves the registry, so a FastMCP service's/metricshas nothing butprocess_*andpython_*. FastAPI, Litestar and FastStream all record request metrics. Ported from community-of-python/microbootstrap#155.Changes
http_app()builds is instrumented withprometheus-fastapi-instrumentator, the library the FastAPI bootstrapper already uses (it needs only Starlette). Requests are labelled by route template (handler="/mcp",handler="/health/"), unknown paths are grouped intohandler="none", and the metrics route is excluded by default.prometheus_instrumentator_paramsandprometheus_instrument_paramsare passed through under the same names as inFastAPIConfig.FastMcpPrometheusMiddlewarerecordsfastmcp_tool_calls_total{tool, status}(success/error) andfastmcp_tool_call_duration_seconds{tool}. It is on by default, andfastmcp_prometheus_tool_metrics_enabled=Falseturns it off.fastmcp-metricsextra addsprometheus-fastapi-instrumentator>=8.0.1, the same floor asfastapi-metrics. The instrument now needs both packages; with either missing it is skipped with the usual dependency warning.Registry notes
http_requests_totaland its siblings are already registered,metrics.default()does not reuse them. It catches the duplicate and returnsNone, so a second instrumented application records nothing. The instrument therefore builds the default metrics once, on the firsthttp_app(), and.add()s that closure to every later application. Its parameters are the subset of instrumentator and instrument params thatmetrics.defaultaccepts.DuplicateTimeseries.Tests
noneis counted for unmatched paths, and the metrics route is not counted.http_app()calls are both counted. This test fails without the shared default metrics.prometheus_instrumentator_paramsreach the instrumentator.http_app. The instrument is skipped without the instrumentator.just lint,ty0.0.85 andjust test-cipass on top of feat!: trace Litestar through its own OpenTelemetryPlugin #279 and feat: trace FastMCP HTTP requests with OpenTelemetry #280 (358 passed, 100% coverage).fastmcp==3.0.0,prometheus-client==0.20.0,prometheus-fastapi-instrumentator==8.0.1, Python 3.11). At those floors, twohttp_app()applications each serving the health route and a 404 count 2 and 2, and a tool call counts 1.