Skip to content

[Bug] Fix per-model metrics cross-talk on multi-model pods - #2331

Merged
varungup90 merged 5 commits into
vllm-project:mainfrom
V-3604:varshith/bug_fix_engine_fetcher_multimodel
Jun 16, 2026
Merged

varungup90 merged 5 commits into
vllm-project:mainfrom
V-3604:varshith/bug_fix_engine_fetcher_multimodel

Conversation

@V-3604

@V-3604 V-3604 commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

Pull Request Description

FetchAllTypedMetrics parses a model-scoped metric family once through parseMetricFromFamily, which reads only metricFamily.Metric[0], then assigns that single MetricValue to every model_name found in the family. On a pod that exposes the same metric under more than one model_name, every model ends up pointing at the first instance's value. The pointer is shared, so sanitizeMetricValueLabels rewrites one labels map on behalf of all of them, which makes the model_name label wrong too.

This change parses model-scoped metrics per model_name. parseModelMetricsFromFamily iterates every instance in the family, matches model_name, and parses each one through parseMetricInstance, the per-instance type switch extracted from parseMetricFromFamily. Each model therefore gets its own value and its own labels map. parseMetricFromFamily keeps the Metric[0] path for pod-scoped metrics, which are not differentiated by model_name. extractModelNamesFromMetrics is removed because it only returned names and the new name-to-value helper supersedes it.

TestEngineMetricsFetcher_FetchAllTypedMetrics/FetchMultiModelMetrics scrapes vllm_num_requests_running and vllm_num_requests_waiting under two model_names and asserts each model/metric key holds its own value and model_name label. It fails on the current code, where model-b reads model-a's value 3 and label model-a, and passes with this change. The existing single-model and pod-scoped tests are unchanged.

go test ./pkg/metrics/ -run 'TestEngineMetricsFetcher_FetchAllTypedMetrics' -v

=== RUN TestEngineMetricsFetcher_FetchAllTypedMetrics
=== RUN TestEngineMetricsFetcher_FetchAllTypedMetrics/FetchAllVllmMetrics
=== RUN TestEngineMetricsFetcher_FetchAllTypedMetrics/FetchSpecificMetrics
=== RUN TestEngineMetricsFetcher_FetchAllTypedMetrics/FetchMultiModelMetrics
--- PASS: TestEngineMetricsFetcher_FetchAllTypedMetrics (0.00s)
--- PASS: TestEngineMetricsFetcher_FetchAllTypedMetrics/FetchAllVllmMetrics (0.00s)
--- PASS: TestEngineMetricsFetcher_FetchAllTypedMetrics/FetchSpecificMetrics (0.00s)
--- PASS: TestEngineMetricsFetcher_FetchAllTypedMetrics/FetchMultiModelMetrics (0.00s)
PASS
ok github.com/vllm-project/aibrix/pkg/metrics 0.193s

Related Issues

Resolves: #2229

Important: Before submitting, please complete the description above and review the checklist below.


Contribution Guidelines (Expand for Details)

We appreciate your contribution to aibrix! To ensure a smooth review process and maintain high code quality, please adhere to the following guidelines:

Pull Request Title Format

Your PR title should start with one of these prefixes to indicate the nature of the change:

  • [Bug]: Corrections to existing functionality
  • [CI]: Changes to build process or CI pipeline
  • [Docs]: Updates or additions to documentation
  • [API]: Modifications to aibrix's API or interface
  • [CLI]: Changes or additions to the Command Line Interface
  • [Misc]: For changes not covered above (use sparingly)

Note: For changes spanning multiple categories, use multiple prefixes in order of importance.

Submission Checklist

  • PR title includes appropriate prefix(es)
  • Changes are clearly explained in the PR description
  • New and existing tests pass successfully
  • Code adheres to project style and best practices
  • Documentation updated to reflect changes (if applicable)
  • Thorough testing completed, no regressions introduced

By submitting this PR, you confirm that you've read these guidelines and your changes align with the project's contribution standards.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the metrics fetcher to properly parse model-scoped metrics on multi-model pods by introducing parseModelMetricsFromFamily to parse and map distinct metric values per model_name. A unit test was also added to verify this behavior. Feedback highlights a critical issue where multiple metric instances sharing the same model_name but differing in other labels will overwrite each other in the map, leading to data loss. It is recommended to aggregate these values for counters and histograms to ensure accurate metric reporting.

Comment thread pkg/metrics/engine_fetcher.go Resolved
@varungup90

Copy link
Copy Markdown
Collaborator

Thanks for putting this together! This is a really solid fix for the metrics cross-talk issue on multi-model pods, and splitting out parseMetricInstance makes the pod-scoped path much cleaner.

I noticed a few edge cases and potential robustness improvements while reviewing the implementation:

🛑 Potential Issues / Architectural Concerns

  1. Lingering SGLang TODO in production path
   // TODO: confirm whether vLLM/SGLang uses the same label_key.
   modelName, err := GetLabelValueForKey(familyMetric, "model_name")

Carried over from extractModelNamesFromMetrics, this means the fix is silently a no-op for SGLang if it uses a different label key. Since this bug also affects SGLang multi-model pods, we should either verify this or track it in a separate GitHub issue before merging.

  1. **In-place mutation in aggregateModelMetric**
   existingVal.Value += incomingVal.Value  // modifies the pointer stored in modelMetrics
   existingVal.Labels = map[string]string{"model_name": modelName}
   return existingVal

This works correctly because existingVal is the pointer in the map, but it's a bit fragile. If this helper is ever reused elsewhere, it could lead to silent data corruption. Consider adding a quick docstring documenting this mutation contract, or refactoring it to return a new copy.

  1. Fail-fast error drops all models on single-instance failure
   value, err := ef.parseMetricInstance(...)
   if err != nil {
       return nil, err  // entire metric family is dropped
   }

If a single malformed instance causes a parse error, the entire metric family is dropped for all models. While fine for pod-scoped metrics, for model-scoped metrics it might be more robust to log the error/continue rather than completely aborting.

  1. Silent default fallback for QueryLabel-typed metrics
    aggregateModelMetric switches on rawType, which is zero-valued when using MetricType.Query == QueryLabel. The default branch returns incoming, meaning the last-parsed instance wins. While likely fine for identical label values, it's undocumented and could silently drop data if a future metric introduces meaningful per-instance labels.
  2. Empty modelMetrics map results in a silent drop
    If a metric is declared PodModelMetricScope but the Prometheus family contains no instances with a model_name label, parseModelMetricsFromFamily returns an empty map and the outer loop silently skips it. This preserves old behavior, but might be worth adding a trace log for easier debugging.

🎨 Code Quality & Refactoring

  • Comment Length: The multi-sentence docstrings on parseModelMetricsFromFamily and aggregateModelMetric explain the design well, but Go conventions lean toward shorter, punchier docstrings. We might want to move some of the deep implementation details into the PR description or commit message instead.
  • Great Refactoring: Removing extractModelNamesFromMetrics was absolutely the right call—eliminating that extra pass makes the logic much more efficient!

@V-3604

V-3604 commented Jun 15, 2026

Copy link
Copy Markdown
Contributor Author

Hi, I pushed af9bcac addressing all of these.

  1. Verified and removed the TODO. SGLang labels per-model metrics with model_name, the same as vLLM. Its production_metrics.md shows sglang:...{model_name="..."}, and aibrix's SGLang PromQL templates and test fixtures already rely on that key, so this path is not a no-op for SGLang.
  2. Documented the in-place mutation in the docstring. The single caller reassigns the map entry, so the fold into existing is intentional.
  3. Now logs at V(4) and continues instead of dropping the whole family on one malformed instance.
  4. The default is only ever the gauge case: the three QueryLabel metrics are all PodMetricScope, so aggregateModelMetric only sees raw types. Added an explicit case Gauge so it is stated rather than implied.
  5. Added a V(4) trace when a family carries no model_name-labeled instances.

Also trimmed both docstrings. go vet and the TestEngineMetricsFetcher suite pass, including the multi-model and multi-series subtests.

go test ./pkg/metrics/ -run TestEngineMetricsFetcher_FetchAllTypedMetrics -v

=== RUN TestEngineMetricsFetcher_FetchAllTypedMetrics
=== RUN TestEngineMetricsFetcher_FetchAllTypedMetrics/FetchAllVllmMetrics
=== RUN TestEngineMetricsFetcher_FetchAllTypedMetrics/FetchSpecificMetrics
=== RUN TestEngineMetricsFetcher_FetchAllTypedMetrics/FetchMultiModelMetrics
=== RUN TestEngineMetricsFetcher_FetchAllTypedMetrics/FetchMultiSeriesPerModelMetrics
--- PASS: TestEngineMetricsFetcher_FetchAllTypedMetrics (0.00s)
--- PASS: TestEngineMetricsFetcher_FetchAllTypedMetrics/FetchAllVllmMetrics (0.00s)
--- PASS: TestEngineMetricsFetcher_FetchAllTypedMetrics/FetchSpecificMetrics (0.00s)
--- PASS: TestEngineMetricsFetcher_FetchAllTypedMetrics/FetchMultiModelMetrics (0.00s)
--- PASS: TestEngineMetricsFetcher_FetchAllTypedMetrics/FetchMultiSeriesPerModelMetrics (0.00s)
PASS
ok github.com/vllm-project/aibrix/pkg/metrics 0.166s

@varungup90
varungup90 merged commit 34e2f43 into vllm-project:main Jun 16, 2026
13 checks passed
@V-3604
V-3604 deleted the varshith/bug_fix_engine_fetcher_multimodel branch June 23, 2026 20:13
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.

[Bug] Per-model metrics cross-talk on multi-model pods (engine_fetcher only parses Metric[0])

2 participants