Skip to content

proxy: fix metrics capture for v1/responses - #586

Merged
mostlygeek merged 1 commit into
mainfrom
mg/responses-streaming-metrics
Mar 13, 2026
Merged

proxy: fix metrics capture for v1/responses#586
mostlygeek merged 1 commit into
mainfrom
mg/responses-streaming-metrics

Conversation

@mostlygeek

Copy link
Copy Markdown
Owner

properly parse anthropic compatible usage data from streaming responses.

closes: #577

properly parse anthropic compatible usage data from streaming
responses.

closes: #577
@coderabbitai

coderabbitai Bot commented Mar 13, 2026

Copy link
Copy Markdown

Walkthrough

The PR modifies metrics monitoring to handle nested usage structures in SSE streaming responses. When parsing streaming data, if usage is not found at the top level, the code now checks for usage under response.usage. Additionally, header parsing in filterAcceptEncoding is improved using safer string operations (strings.SplitSeq and strings.Cut). A test case validates the nested usage parsing behavior.

Changes

Cohort / File(s) Summary
Streaming Response Metrics Parsing
proxy/metrics_monitor.go
Enhanced processStreamingResponse to check for usage under response.usage when not found at top level. Improved filterAcceptEncoding to use strings.SplitSeq and strings.Cut for safer header encoding parsing.
Metrics Test Coverage
proxy/metrics_monitor_test.go
Added test case to TestMetricsMonitor_StreamingResponse validating parsing of v1 SSE responses with usage tokens nested under response.usage.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main change: fixing metrics capture for the v1/responses endpoint by properly parsing usage data.
Description check ✅ Passed The description is related to the changeset, explaining that it properly parses Anthropic-compatible usage data from streaming responses and references the relevant issue #577.
Linked Issues check ✅ Passed The PR addresses the primary coding requirement from issue #577: parsing nested Responses usage payloads in streaming metrics by checking response.usage when top-level usage is absent.
Out of Scope Changes check ✅ Passed The changes are limited to metrics parsing for streaming responses and directly address the issue scope; no unrelated modifications are present in the changeset.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch mg/responses-streaming-metrics
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Tip

CodeRabbit can enforce grammar and style rules using `languagetool`.

Configure the reviews.tools.languagetool setting to enable/disable rules and categories. Refer to the LanguageTool Community to learn more.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@proxy/metrics_monitor.go`:
- Around line 513-515: The code extracts the encoding token with encoding, _, _
:= strings.Cut(strings.TrimSpace(part), ";") but doesn't trim encoding
afterwards, so values like "gzip " fail the supported[strings.ToLower(encoding)]
check; fix by trimming encoding (e.g., encoding = strings.TrimSpace(encoding))
before lowercasing and checking the supported map in the same block where part,
encoding and filtered are handled (referencing the variables encoding, part,
supported and the strings.Cut call).

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 20900357-3e65-4ecf-9afb-eb30a080abe5

📥 Commits

Reviewing files that changed from the base of the PR and between 29a38fd and 3f7be88.

📒 Files selected for processing (2)
  • proxy/metrics_monitor.go
  • proxy/metrics_monitor_test.go

Comment thread proxy/metrics_monitor.go
Comment on lines +513 to 515
encoding, _, _ := strings.Cut(strings.TrimSpace(part), ";")
if supported[strings.ToLower(encoding)] {
filtered = append(filtered, strings.TrimSpace(part))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Trim the encoding token after strings.Cut to handle valid OWS before ;q=.

At Line 513, headers like gzip ;q=1.0 leave encoding as "gzip " and fail the supported check.

💡 Proposed fix
-		encoding, _, _ := strings.Cut(strings.TrimSpace(part), ";")
+		encoding, _, _ := strings.Cut(strings.TrimSpace(part), ";")
+		encoding = strings.TrimSpace(encoding)
 		if supported[strings.ToLower(encoding)] {
 			filtered = append(filtered, strings.TrimSpace(part))
 		}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
encoding, _, _ := strings.Cut(strings.TrimSpace(part), ";")
if supported[strings.ToLower(encoding)] {
filtered = append(filtered, strings.TrimSpace(part))
encoding, _, _ := strings.Cut(strings.TrimSpace(part), ";")
encoding = strings.TrimSpace(encoding)
if supported[strings.ToLower(encoding)] {
filtered = append(filtered, strings.TrimSpace(part))
}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@proxy/metrics_monitor.go` around lines 513 - 515, The code extracts the
encoding token with encoding, _, _ := strings.Cut(strings.TrimSpace(part), ";")
but doesn't trim encoding afterwards, so values like "gzip " fail the
supported[strings.ToLower(encoding)] check; fix by trimming encoding (e.g.,
encoding = strings.TrimSpace(encoding)) before lowercasing and checking the
supported map in the same block where part, encoding and filtered are handled
(referencing the variables encoding, part, supported and the strings.Cut call).

@mostlygeek
mostlygeek merged commit c3c258a into main Mar 13, 2026
3 checks passed
@mostlygeek
mostlygeek deleted the mg/responses-streaming-metrics branch March 13, 2026 23:50
mostlygeek added a commit that referenced this pull request May 26, 2026
properly parse anthropic compatible usage data from streaming responses.

closes: #577
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.

1 participant