feat(mcp): record metrics for backend notification streams - #2775
Open
sangkyoonnam wants to merge 1 commit into
Open
sangkyoonnam wants to merge 1 commit into
sangkyoonnam wants to merge 1 commit into
Conversation
Signed-off-by: Sangkyoon Nam <sangkyoon.nam@namsang.co>
✅ Deploy Preview for theagentrouter canceled.
|
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.
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 insendRequestPerBackendfor GET only:mcp.notification_stream.open.attempts: counted beforeclient.Do.mcp.notification_stream.open.outcomeswithmcp.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) withmcp.notification_stream.end_reason:eof,cancelled,error. A malformed final event, which the parser returns joined withio.EOF, counts aserror.The end reason is a dimension on the duration histogram rather than a separate counter; its count gives closes per reason.
http_otherkeeps 201 and 3xx out of the 4xx/5xx buckets. Modernsubscriptions/listenisn't proxied yet, so it is out of scope. If the existingeventChansend blocks after the client leaves,activestays 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 precommitandmake testpass.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_othervalue and the histogram bounds (0.1s to 2h) are open to change if you'd rather match another convention.