Skip to content

fix(metrics): add path label to HTTP response metrics - #1328

Merged
slin1237 merged 2 commits into
mainfrom
fix/http-response-path-metrics
Apr 22, 2026
Merged

slin1237 merged 2 commits into
mainfrom
fix/http-response-path-metrics

Conversation

@zhaowenzi

@zhaowenzi zhaowenzi commented Apr 22, 2026 •

Copy link
Copy Markdown
Contributor

Description

Problem

/v1/responses success rate could not be computed from the existing HTTP metrics.

The main issue was that smg_http_requests_total already exposed a normalized path label, but smg_http_responses_total only exposed status_code and error_code. That meant we could count requests for /v1/responses, and we could count responses globally, but we could not slice response outcomes on the same path dimension to build a correct numerator and denominator for a /v1/responses success-rate query.

Existing error-count metrics were not a reliable substitute:

  • smg_http_responses_total{status_code=~"5.."} only gave a global HTTP error count, not a per-path error count, so it could not isolate /v1/responses failures.
  • smg_router_request_errors_total{endpoint="responses"} was also not a reliable source of truth for this use case. Responses API traffic is not uniformly represented by router-level error metrics across implementations, so pairing router-level error counts with edge HTTP request counts would produce an incomplete and potentially misleading success-rate calculation.

Because of that, the dedicated error-count metrics could show that failures existed, but they still could not answer the actual question: what is the HTTP success rate for /v1/responses?

Solution

Add the normalized HTTP path label to smg_http_responses_total and emit the response metric from HttpMetricsLayer, where both the normalized request path and the final HTTP response are available together.

This keeps request, response, and latency metrics aligned on the same path dimension and makes /v1/responses success-rate queries possible directly from Layer 1 HTTP metrics.

Changes

  • add path to smg_http_responses_total
  • change Metrics::record_http_response(...) to record path, status_code, and error_code
  • move HTTP response metric emission out of the logging layer and into HttpMetricsLayer
  • keep response-path normalization aligned with the existing request metric path normalization
  • update metrics and monitoring docs with a /v1/responses success-rate query example
  • document why the existing error-count metrics were insufficient for calculating /v1/responses success rate

Test Plan

Before this change, the following query was not possible because smg_http_responses_total did not have a path label:

sum(rate(smg_http_responses_total{path="/v1/responses",status_code=~"2.."}[5m]))
/
sum(rate(smg_http_responses_total{path="/v1/responses"}[5m]))

After this change:

  1. start SMG with Prometheus metrics enabled
  2. send traffic to POST /v1/responses
  3. verify that smg_http_responses_total now includes path="/v1/responses"
  4. run the query above and confirm it returns a per-path success rate

Validation run for this PR:

  • cargo test -p smg test_normalize_path_with_prefixed_id --lib
  • pre-commit run --all-files (with only the branch-protection hook skipped on main)
Checklist
  • cargo +nightly fmt passes
  • cargo clippy --all-targets --all-features -- -D warnings passes
  • (Optional) Documentation updated
  • (Optional) Please join us on Slack #sig-smg to discuss, review, and merge PRs

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@coderabbitai

coderabbitai Bot commented Apr 22, 2026 •

Copy link
Copy Markdown

Warning

Rate limit exceeded

@zhaowenzi has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 44 minutes and 55 seconds before requesting another review.

Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 44 minutes and 55 seconds.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: e6807745-ee04-4f3d-b897-5fcb3357e24d

📥 Commits

Reviewing files that changed from the base of the PR and between 60e3feb and 991c7c5.

📒 Files selected for processing (5)
  • docs/getting-started/monitoring.md
  • docs/reference/metrics.md
  • model_gateway/src/middleware/logging.rs
  • model_gateway/src/middleware/metrics.rs
  • model_gateway/src/observability/metrics.rs
📝 Walkthrough

Walkthrough

HTTP response metrics are enhanced to track request paths as a new label dimension. The metric recording API signature is updated to accept path information, and middleware responsibilities are refactored to move error code extraction from the logging middleware to the metrics middleware.

Changes

Cohort / File(s) Summary
Documentation Updates
docs/getting-started/monitoring.md, docs/reference/metrics.md
Updated smg_http_responses_total metric documentation to reflect new path label dimension (alongside existing status_code and error_code). Added example Grafana query computing success rate filtered by specific path.
Middleware Refactoring
model_gateway/src/middleware/logging.rs, model_gateway/src/middleware/metrics.rs
Relocated error code extraction logic from logging middleware to metrics middleware. Logging middleware now focuses on status code and latency tracking, while metrics middleware handles the complete metrics emission including path, status, and error code.
Metrics API Update
model_gateway/src/observability/metrics.rs
Updated record_http_response() public method signature to accept path: &str parameter as the first argument. Implementation now interns and includes path as a metric label.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested labels

model-gateway, documentation

Suggested reviewers

  • CatherineSue
  • slin1237

Poem

🐰 A path now tracked in metrics divine,
Where responses dance on labels fine,
From logging burrows to metrics it goes,
HTTP journeys that everyone knows! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 40.00% 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
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately and concisely summarizes the main change: adding a path label to HTTP response metrics across documentation and implementation files.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/http-response-path-metrics

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.

@github-actions github-actions Bot added documentation Improvements or additions to documentation model-gateway Model gateway crate changes labels Apr 22, 2026

@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 `@docs/getting-started/monitoring.md`:
- Around line 186-192: The markdown lint rule MD031 flags the fenced code block
after the heading "**`/v1/responses` Success Rate**" as not surrounded by blank
lines; fix by inserting a single blank line between that bold heading and the
opening triple-backtick fence so the PromQL block (the code starting with
sum(rate(smg_http_responses_total...))) is separated by an empty line from the
heading.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: da0f3d09-c5df-48b3-b93f-c6af50146000

📥 Commits

Reviewing files that changed from the base of the PR and between a92cfb3 and 60e3feb.

📒 Files selected for processing (5)
  • docs/getting-started/monitoring.md
  • docs/reference/metrics.md
  • model_gateway/src/middleware/logging.rs
  • model_gateway/src/middleware/metrics.rs
  • model_gateway/src/observability/metrics.rs

Comment thread docs/getting-started/monitoring.md
Signed-off-by: Ziwen Zhao <zzw.mose@gmail.com>
Signed-off-by: Ziwen Zhao <zzw.mose@gmail.com>
@zhaowenzi
zhaowenzi force-pushed the fix/http-response-path-metrics branch from ac69da0 to 991c7c5 Compare April 22, 2026 18:35
@slin1237
slin1237 merged commit 06eaccc into main Apr 22, 2026
33 of 34 checks passed
@slin1237
slin1237 deleted the fix/http-response-path-metrics branch April 22, 2026 19:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation model-gateway Model gateway crate changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants