Skip to content

[ISSUE] Fix: Wildcard Stats Key Behavior Changes#1017

Merged
collin-lee merged 7 commits intoenvoyproxy:mainfrom
xuannam230201:fix/metrics-for-wild-card
Dec 5, 2025
Merged

[ISSUE] Fix: Wildcard Stats Key Behavior Changes#1017
collin-lee merged 7 commits intoenvoyproxy:mainfrom
xuannam230201:fix/metrics-for-wild-card

Conversation

@xuannam230201
Copy link
Copy Markdown
Contributor

@xuannam230201 xuannam230201 commented Dec 5, 2025

[ISSUE]: Wildcard Stats Key Behavior Changes

Problem

Previously, when value_to_metric logic was added (in my another PR), the wildcard stats key behavior was accidentally changed. Wildcard patterns without value_to_metric or share_threshold were generating stats keys without the wildcard value pattern (e.g., test-domain.wild instead of test-domain.wild_foo*). This broke backward compatibility for wildcard configurations.

Additionally, when share_threshold was implemented, stats keys for wildcard patterns with share_threshold were also not including the wildcard pattern with * (e.g., test-domain.files instead of test-domain.files_files/*), which was inconsistent with the expected behavior for wildcard stats.

Note: The lack of unit tests for wildcard stats keys made it difficult to detect this behavioral change. Adding comprehensive unit tests can help avoid such issues in the future.

Solution

The code now preserves the original wildcard stats behavior while properly handling share_threshold and value_to_metric flags according to the following rules:

  1. No value_to_metric OR detailed_metric: Stats key includes wildcard pattern with * (e.g., test-domain.wild_foo*)
  2. value_to_metric OR detailed_metric: Stats key includes actual runtime value (e.g., test-domain.wild_foo1)
  3. share_threshold: true: Stats key always includes wildcard pattern with *, regardless of other flags (e.g., test-domain.wild_foo*)

This change ensures that:

  1. When a wildcard is matched and neither value_to_metric nor share_threshold is enabled, the stats key includes the wildcard pattern (e.g., foo*) instead of just the key name (original behavior).
  2. When share_threshold is enabled, the stats key consistently includes the wildcard pattern with * (e.g., test-domain.files_files/*), maintaining consistency with wildcard stats behavior.

Test Coverage

Added & Updated comprehensive unit tests to prevent future regressions.

@xuannam230201 xuannam230201 force-pushed the fix/metrics-for-wild-card branch from 711a97d to 40f3048 Compare December 5, 2025 12:55
@xuannam230201
Copy link
Copy Markdown
Contributor Author

xuannam230201 commented Dec 5, 2025

Hi @collin-lee , this issue was introduced in my another PR (this line and this line). As a result, I create this PR to address this issue (to maintain backward compatibility for metrics of wild card). Could you please help to take a look at this?

Thanks in advance!

@xuannam230201 xuannam230201 changed the title [ISSUE]: Wildcard Stats Key Behavior Changes [ISSUE] Fix: Wildcard Stats Key Behavior Changes Dec 5, 2025
@xuannam230201 xuannam230201 force-pushed the fix/metrics-for-wild-card branch from 40f3048 to 430727e Compare December 5, 2025 14:30
Signed-off-by: Nam Dang <xuannam230201@gmail.com>
@xuannam230201 xuannam230201 force-pushed the fix/metrics-for-wild-card branch from 430727e to 5a4a039 Compare December 5, 2025 14:41
Signed-off-by: Nam Dang <xuannam230201@gmail.com>
Signed-off-by: Nam Dang <xuannam230201@gmail.com>
…uest

Signed-off-by: Nam Dang <xuannam230201@gmail.com>
Signed-off-by: Nam Dang <xuannam230201@gmail.com>
Comment thread src/config/config_impl.go
rateLimit = NewRateLimit(rateLimit.Limit.RequestsPerUnit, rateLimit.Limit.Unit, this.statsManager.NewStats(rateLimit.FullKey), rateLimit.Unlimited, rateLimit.ShadowMode, rateLimit.Name, rateLimit.Replaces, rateLimit.DetailedMetric)
rateLimit = NewRateLimit(rateLimit.Limit.RequestsPerUnit, rateLimit.Limit.Unit, this.statsManager.NewStats(enhancedKey), rateLimit.Unlimited, rateLimit.ShadowMode, rateLimit.Name, rateLimit.Replaces, rateLimit.DetailedMetric)
rateLimit.ShareThresholdKeyPattern = originalShareThresholdKeyPattern
rateLimit.Stats = this.statsManager.NewStats(enhancedKey)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Remove these codes because each time this.statsManager.NewStats is called, logger prints debug log "Creating stats..." once. As a result, with current code, each request makes logger log that twice.

FullKey is automatically assigned to stat.string when rateLimit is initialized; as a result, remove the assignment (redundant).

Signed-off-by: Nam Dang <xuannam230201@gmail.com>
Comment thread src/config/config_impl.go
// Build value_to_metric metrics path for this level
valueToMetricFullKey.WriteString(".")
if nextDescriptor != nil {
// Check if share_threshold is enabled for this entry
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Refactor the logic to include value in stats. It's not necessary to repeat:

valueToMetricFullKey.WriteString(entry.Key)
valueToMetricFullKey.WriteString("_")
valueToMetricFullKey.WriteString(...)

several times in this logic

Signed-off-by: Nam Dang <xuannam230201@gmail.com>
@xuannam230201 xuannam230201 force-pushed the fix/metrics-for-wild-card branch from a4a9d77 to e06479e Compare December 5, 2025 18:19
@collin-lee collin-lee merged commit e4ac90e into envoyproxy:main Dec 5, 2025
6 checks passed
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.

2 participants