Skip to content

fix: remove raw request path label from shenyu_request_type_total (#6671) - #7056

Open
wy471x wants to merge 7 commits into
apache:masterfrom
wy471x:fix_Prometheus-counter-labeled-by-raw-request-path
Open

wy471x wants to merge 7 commits into
apache:masterfrom
wy471x:fix_Prometheus-counter-labeled-by-raw-request-path

Conversation

@wy471x

@wy471x wy471x commented Sep 12, 2026

Copy link
Copy Markdown
Contributor

Make sure that:

  • You have read the contribution guidelines.
  • You submit test cases (unit or integration tests) that back your changes.
  • Your local test passed ./mvnw clean install -Dmaven.javadoc.skip=true.

Summary

Fixes #6671. shenyu_request_type_total was incremented with the raw request path as a
label value, so every distinct path (/api/user/123, /api/order/456, ...) created a new
Counter.Child inside the Prometheus client's internal children map, which is never
evicted. The outer counter/gauge/histogram maps are bounded by metric name, but that
per-series map is not: heap usage grows with the number of distinct request paths, which
is unbounded on a gateway serving parameterised routes, and it also inflates the
/metrics payload. The path label is now dropped, keeping only the bounded rpcType
dimension.

Changes

  1. MetricsPlugin#execute (shenyu-plugin/shenyu-plugin-metrics/src/main/java/org/apache/shenyu/plugin/metrics/MetricsPlugin.java:49) — counterIncrement(LabelNames.REQUEST_TYPE_TOTAL, ...) now passes new String[]{shenyuContext.getRpcType()} instead of new String[]{exchange.getRequest().getURI().getRawPath(), shenyuContext.getRpcType()}.

  2. MetricsReporter#register (shenyu-plugin/shenyu-plugin-metrics/src/main/java/org/apache/shenyu/plugin/metrics/reporter/MetricsReporter.java:43) — the counter is registered with label names {"type"} instead of {"path", "type"}, so the registration matches the values the plugin emits.

Note for reviewers: this changes the exposed label set of shenyu_request_type_total
from {path,type} to {type}. It is an intentional breaking change to the metric shape,
since per-path granularity cannot be kept without the unbounded cardinality. Dashboards
that break this metric down by path need to be adjusted.

Test Cases

  1. MetricsPluginTest.testRequestTypeTotalIsNotLabelledByRawPath (shenyu-plugin/shenyu-plugin-metrics/src/test/java/org/apache/shenyu/plugin/metrics/MetricsPluginTest.java:93) — executes the plugin for /api/user/123 and /api/order/456 and verifies both increments carry the same label vector {rpcType}, i.e. the raw path is no longer forwarded as a label value.

Verification

  • ./mvnw -pl shenyu-plugin/shenyu-plugin-metrics clean install -Dmaven.javadoc.skip=true — BUILD SUCCESS; Tests run: 15, Failures: 0, Errors: 0, Skipped: 0.
  • ./mvnw clean install -Dmaven.javadoc.skip=true — all modules build and install. The only failure is a pre-existing, order-dependent one in shenyu-kubernetes-controller: DubboReconcilerTest#testReconcile fails when that module's test suite runs as a whole and passes when the class is run in isolation; it is untouched by this PR. Excluding only that class, the remaining reactor finishes green.
  • Checkstyle: 0 violations.
  • RAT: Unapproved 0, unknown 0, generated 0, approved 17 licenses.

close #6671

…ache#6671)

shenyu_request_type_total was labelled with the raw request path, so every
distinct path (for example /api/user/123 and /api/order/456) created a new
Counter.Child inside the Prometheus client's internal children map, which is
never evicted. On a gateway serving parameterised routes that map grows with
the number of distinct paths and eventually exhausts the heap.

Drop the path label and keep only rpcType, which is a bounded dimension, and
add a regression test asserting that the raw path is no longer forwarded as a
label value.

Copilot AI 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.

🟡 Changes recommended

One or more issues must be addressed before approval.

Get a fresh assessment by requesting another Copilot review.

Pull request overview

Removes the raw request path from shenyu_request_type_total to prevent unbounded Prometheus label cardinality.

Changes:

  • Uses only rpcType as the request-type metric label.
  • Updates Prometheus registration to match the new label vector.
  • Adds a regression test for path-independent increments.
File summaries
File Description
shenyu-plugin/shenyu-plugin-metrics/src/test/java/org/apache/shenyu/plugin/metrics/MetricsPluginTest.java Updated as part of this pull request.
shenyu-plugin/shenyu-plugin-metrics/src/main/java/org/apache/shenyu/plugin/metrics/reporter/MetricsReporter.java Updated as part of this pull request.
shenyu-plugin/shenyu-plugin-metrics/src/main/java/org/apache/shenyu/plugin/metrics/MetricsPlugin.java Updated as part of this pull request.
Review details

Suppressed comments (1)

shenyu-plugin/shenyu-plugin-metrics/src/main/java/org/apache/shenyu/plugin/metrics/reporter/MetricsReporter.java:43

  • The regression test only verifies that the plugin passes a one-element value vector to a mock; it never asserts the counter is registered with the matching one-label schema. If this registration accidentally remains {path, type}, the test would still pass even though the Prometheus client rejects the one-value labels(...) call at runtime. Please add a registration assertion (or exercise an actual Prometheus register) that shenyu_request_type_total has exactly the type label.
        MetricsReporter.registerCounter(LabelNames.REQUEST_TYPE_TOTAL, new String[]{"type"}, "shenyu http request type total count");
  • Files reviewed: 3/3 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@Test
public void testRequestTypeTotalIsNotLabelledByRawPath() {
MetricsRegister metricsRegister = Mockito.mock(MetricsRegister.class);
MetricsReporter.register(metricsRegister);

@Aias00 Aias00 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.

Approved as PMC (Aias00). Small, well-scoped fix with regression tests; green CI, mergeable. Reviewed the diff.

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] — Prometheus counter labeled by raw request path → high-cardinality children map grows unbounded

3 participants