feat: expose configured rate limit as a gauge metric#1138
Open
gd03champ wants to merge 1 commit into
Open
Conversation
Add a `rate_limit` gauge to `RateLimitStats` that emits the configured
`requests_per_unit` value for each descriptor. The gauge is set once at
config load time via `NewRateLimit`, making it stable between reloads.
This allows operators to compute utilization ratios in Prometheus and
alert at arbitrary thresholds without relying on the global
`NEAR_LIMIT_RATIO` setting:
ratelimit_service_rate_limit_total_hits
/ ratelimit_service_rate_limit_limit > 0.6
Unlimited descriptors emit 0, which produces NaN in ratio queries and
does not trigger threshold alerts.
Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Signed-off-by: GD <gsnish25255@gmail.com>
159967a to
11311e1
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The existing
near_limitcounter fires at a fixed percentage controlled by the globalNEAR_LIMIT_RATIOenv var (default 80%). There is no way to:requests_per_unitvalue is never exported as a metric — the denominator simply doesn't exist.Solution
Add a
rate_limitgauge toRateLimitStatsthat holds the configuredrequests_per_unitfor each descriptor. The gauge is populated once at config load time insideNewRateLimit(), so it reflects the current config and is updated on hot-reload.With this gauge, operators can construct arbitrary utilization alerts in Prometheus without touching
NEAR_LIMIT_RATIO:Unlimited descriptors (
requests_per_unit: 0) emit0, which produces+Inf/NaNin ratio queries and does not trigger threshold alerts.Changes
src/stats/manager.goRateLimit gostats.Gaugefield toRateLimitStatssrc/stats/manager_impl.goNewStats()test/mocks/stats/manager.gosrc/config/config_impl.goNewRateLimit()at config load timesrc/stats/prom/default_mapper.yamlratelimit_service_rate_limit_limittest/stats/manager_impl_test.gorequests_per_unitvalueTest plan
go test ./test/stats/...— newTestNewStatsCreatesRateLimitGaugepassesgo test ./test/config/... ./test/limiter/... ./test/redis/... ./test/memcached/... ./test/service/...— all existing tests passgo build ./...— compiles cleanly