feat(prometheus): expose MCP tool metadata in Prometheus metrics - #31899
Conversation
|
@greptileai review |
|
|
Greptile SummaryExposes MCP tool call metadata in two new Prometheus counters —
Confidence Score: 5/5This PR is safe to merge — the new MCP metric path is purely additive, guarded by two isinstance checks, and isolated to a dedicated label-value object that does not touch any existing counter's label set. The change is narrowly scoped: two new counters wired only through _increment_mcp_tool_call_metrics, which bails out early on any unexpected payload shape. Previously flagged issues (mutable shared list, missing dict guards) have all been corrected in this version. No regressions are introduced to existing counters or the success-logging critical path. No files require special attention.
|
| Filename | Overview |
|---|---|
| litellm/integrations/prometheus.py | Adds two new MCP Counter metrics with proper isinstance dict guards and a dedicated UserAPIKeyLabelValues/PrometheusLabelFactoryContext so MCP labels stay isolated from other counters. No issues found. |
| litellm/types/integrations/prometheus.py | Adds MCP_TOOL_NAME/MCP_SERVER_NAME to UserAPIKeyLabelNames, registers both metric names in DEFINED_PROMETHEUS_METRICS, defines label lists as independent objects (spend list uses list(...) copy), and adds optional fields to UserAPIKeyLabelValues. |
| tests/test_litellm/integrations/test_prometheus_mcp_tool_metrics.py | New mock-only test file covering registration, label propagation, spend counter gating, and graceful no-ops for missing/non-dict metadata. All tests use mocks — no real network calls. No issues found. |
Reviews (5): Last reviewed commit: "feat(prometheus): expose MCP tool metada..." | Re-trigger Greptile
Greptile SummaryThis PR adds two Prometheus counters —
Confidence Score: 4/5Safe to merge; the new code path is additive and only activates when MCP metadata is present in a log payload. The implementation is well-structured and tested. The only concerns are defensive: The
|
| Filename | Overview |
|---|---|
| litellm/integrations/prometheus.py | Adds two new Prometheus counters and _increment_mcp_tool_call_metrics() that fires from async_log_success_event; logic is sound but mcp_meta is not guarded as dict before .get() is called. |
| litellm/types/integrations/prometheus.py | Registers MCP metric names, adds label sets to PrometheusMetricLabels, and extends UserAPIKeyLabelValues; new class attributes lack List[str] type annotations unlike the rest of the class. |
| tests/test_litellm/integrations/test_prometheus_mcp_tool_metrics.py | New test file with 14 unit tests covering metric registration, counter increment, no-op behaviour, and label propagation; tests are mock-only and comply with the repository's testing rules. |
Reviews (2): Last reviewed commit: "feat(prometheus): expose MCP tool metada..." | Re-trigger Greptile
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
|
@greptileai review |
72bf712 to
f641746
Compare
|
@greptileai review |
f641746 to
9949ba0
Compare
|
@greptileai review |
…riAI#31899) Co-authored-by: Yassin Kortam <yassin@berri.ai>
Relevant issues
Resolves LIT-3765
Linear ticket
Resolves LIT-3765
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
@greptileaiand received a Confidence Score of at least 4/5 before requesting a maintainer reviewScreenshots / Proof of Fix
Ran a live proxy with the prometheus callback and a real MCP server (deepwiki), config:
Made real MCP tool calls through the proxy's
/mcpendpoint:Output, both counters increment per tool with
mcp_server_nameandmcp_tool_namelabels:Devin e2e run:
Type
🆕 New Feature
Changes
Two new Prometheus counters that fire when
mcp_tool_call_metadatais present in the standard logging payload:litellm_mcp_tool_calls_totalcounts MCP tool callslitellm_mcp_tool_call_spend_metrictracks spend on MCP tool callsBoth carry
mcp_tool_nameandmcp_server_namelabels alongside the standard key/team/user labels, enabling Grafana dashboards to break down MCP usage by server and toollitellm/types/integrations/prometheus.pyaddsMCP_TOOL_NAMEandMCP_SERVER_NAMEtoUserAPIKeyLabelNames, registers both metric names inDEFINED_PROMETHEUS_METRICS, defines their label sets inPrometheusMetricLabels(each metric gets its own annotated list so the two class attributes do not share one mutable object), and adds the two optional fields toUserAPIKeyLabelValueslitellm/integrations/prometheus.pycreates the two Counter metrics in__init__, adds_increment_mcp_tool_call_metrics()which extractsmcp_tool_call_metadatafrom the payload metadata (with dict guards on both the metadata and the MCP entry) and increments the counters with a dedicatedUserAPIKeyLabelValuescarrying the MCP-specific fields, and calls it fromasync_log_success_eventThe method builds a separate
UserAPIKeyLabelValuesfor MCP metrics (withmcp_tool_name/mcp_server_namepopulated) rather than adding those fields to the mainenum_values, since MCP labels only apply to these two metrics and shouldn't pollute label sets for the 30+ other countersLink to Devin session: https://app.devin.ai/sessions/8ad6e7828a5149d0af0e4d148d7d8bcc