Repository navigation
feat(mcp): unsampled per-tool Prometheus metrics for hosted tools/call - #25514
Conversation
PostHog's MCP Tool Call event is sampled per user (25%) and carries no
response size, so there was no complete answer to which tools fail, how slow
they are, how big their answers are, or how often reads are cut short.
- omi_mcp_tool_calls_total{tool,client,outcome,error_category}
- omi_mcp_tool_duration_seconds{tool}
- omi_mcp_tool_response_chars{tool} (compact JSON text already serialized)
- omi_mcp_tool_truncated_results_total{tool} (results with truncated=true)
Labels are bounded: tool is allowlisted to the registry, client is the
analytics client family. Recording fails open and never changes a result.
backend/docs/mcp-analytics.md lists the series and starter PromQL.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 516f21db34
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
…y, guard metric shapes Review follow-ups on the per-tool metrics: - the daily-cap / usage-budget early return now increments omi_mcp_tool_calls_total with error_category=capped (metrics only); - calls rejected before dispatch add no duration sample, and duration buckets extend to 120s; - reusing a registered collector checks its type and labels. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 1fb057ce70
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Why
To make the hosted MCP server work well for ChatGPT, Claude and other agents, we need to see how it behaves on every call. Today the only per-tool signal is PostHog's
MCP Tool Call, which is sampled per user (25% by default) and has no response size. It cannot say which tools fail, how slow they are, how big their answers are, or how often reads are cut short and need another page. The only MCP Prometheus metric is the OAuth token counter.What changes
Every hosted
tools/callthat reaches dispatch now updates four unsampled series (backend/utils/mcp_server/tool_metrics.py):omi_mcp_tool_calls_totaltool,client,outcome,error_categoryomi_mcp_tool_duration_secondstoolomi_mcp_tool_response_charstoolomi_mcp_tool_truncated_results_totaltooltruncated=trueLabels are bounded:
toolis allowlisted to the registry throughmcp_tool_enum, and anything else isunknown;clientis the existing analytics client family (chatgpt,claude,other_registered,api_key,unknown), exposed asmcp_client_family;outcomeanderror_categoryare closed sets.No uid, query, id or payload is a label. Worst case is about 30 tools × 5 clients × 6 outcome/category pairs (success, and five error categories: authorization_denied, validation, unknown_tool, internal, and the metrics-only
cappedfor the daily cap or usage budget), and real traffic uses far fewer.Recording happens in
transport._tool_call_analytics, which every dispatch path already calls (success, model-visible error, unknown tool, scope denial, internal error), and in the daily-cap early return and the quota-store outage 503 (counted asinternal), which the sampled PostHog event has never covered. Result size and truncation are recorded in the success path after the payload is built. Calls rejected before dispatch (malformed arguments) are counted but add no latency sample. Duration buckets reach 120 seconds. Reloading a collector with a different shape raises an error. It fails open: a broken metric never changes a tool result. REST twins are not counted.backend/docs/mcp-analytics.mdlists the series with starter PromQL (error rate by tool, p95 latency, truncated-read share).Tests
New
backend/tests/unit/test_mcp_tool_metrics.py, 10 tests throughtransport.handle_mcp_message:An
omi-panelreview (luna, deepseek, glm-flash) found 7 issues; 6 were fixed here. The seventh, that the histogram test sums cumulative buckets, was rejected: prometheus_client stores per-label buckets non-cumulatively.All 50
backend/tests/unit/test_mcp_*.pyfiles pass throughtest.sh, each in its own process.Release
Default-on. These are additive metrics, with no change to any response. Rollback is reverting this PR.
Not verified: series in the production Prometheus scrape, which needs a deploy.
Failure-Class: none
🤖 Generated with Claude Code