Skip to content

feat(mcp): record metrics for backend notification streams - #2775

Open
sangkyoonnam wants to merge 1 commit into
theagentrouter:mainfrom
sangkyoonnam:feat/2725-mcp-notification-stream-metrics
Open

sangkyoonnam wants to merge 1 commit into
theagentrouter:mainfrom
sangkyoonnam:feat/2725-mcp-notification-stream-metrics

Conversation

@sangkyoonnam

Copy link
Copy Markdown

Description

Legacy backend notification GETs had no lifecycle metrics. Client heartbeats keep going after backend streams end, while failures are either silent (202/204/405, unexpected EOF) or only logged with the backend name.

This adds four backend-scoped instruments, all with mcp.backend, recorded in sendRequestPerBackend for GET only:

  • mcp.notification_stream.open.attempts: counted before client.Do.
  • mcp.notification_stream.open.outcomes with mcp.notification_stream.outcome: opened, unsupported (202/204/405, or a single JSON-RPC message in reply to the GET), http_4xx, http_5xx, http_other, transport_error, cancelled (before the stream opened). One outcome per completed opening attempt; a non-200 status keeps its class even when decoding the body fails.
  • mcp.notification_stream.active: up/down counter, +1 on open and -1 on end.
  • mcp.notification_stream.duration (seconds) with mcp.notification_stream.end_reason: eof, cancelled, error. A malformed final event, which the parser returns joined with io.EOF, counts as error.

The end reason is a dimension on the duration histogram rather than a separate counter; its count gives closes per reason. http_other keeps 201 and 3xx out of the 4xx/5xx buckets. Modern subscriptions/listen isn't proxied yet, so it is out of scope. If the existing eventChan send blocks after the client leaves, active stays above zero for that stream. POST requests, existing metrics, logs and return values are unchanged.

Tests use a real ManualReader: one healthy and one 405 backend in the same session, all backend streams ending while client heartbeats continue (active back to 0), status classes, single JSON-RPC responses, body-read and gzip failures, transport errors, unexpected and malformed final events, cancellation before and after open, and no notification-stream metrics for POST. make precommit and make test pass.

Per the generative AI policy: this change was developed with AI assistance (Claude Code). I have reviewed it, understand it, and take full ownership of the code, tests, and this description.

Related Issues/PRs (if applicable)

Fixes #2725
Related PR: #2733 (backend initialization metrics; no shared changed files)

Special notes for reviewers (if applicable)

The metric and attribute names, the http_other value and the histogram bounds (0.1s to 2h) are open to change if you'd rather match another convention.

Signed-off-by: Sangkyoon Nam <sangkyoon.nam@namsang.co>
@sangkyoonnam
sangkyoonnam requested a review from a team as a code owner October 2, 2026 08:07
@netlify

netlify Bot commented Oct 2, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for theagentrouter canceled.

Name Link
🔨 Latest commit f72bb8a
🔍 Latest deploy log https://app.netlify.com/projects/theagentrouter/deploys/6abf663c5eeaaf00087c141b

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.

MCP: expose backend notification-stream lifecycle and failure metrics

1 participant