Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor metrics / log improvement for global ratelimiter #6259

Merged
merged 1 commit into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions common/metrics/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,12 @@ func AsyncWFRequestTypeTag(value string) Tag {
return metricWithUnknown(asyncWFRequestType, value)
}

// GlobalRatelimiterKeyTag reports the full (global) ratelimit key being used, e.g. "user:domain-x",
// though the value will be sanitized and may appear as "user_domain_x" or similar.
// GlobalRatelimiterKeyTag reports the local ratelimit key being used, e.g. "domain-x".
// This will likely be ambiguous if it is not combined with the collection name,
// but keeping this untouched helps keep the values template-friendly and correlate-able
// in metrics dashboards and queries.
func GlobalRatelimiterKeyTag(value string) Tag {
return simpleMetric{key: globalRatelimitKey, value: sanitizer.Value(value)}
Copy link
Contributor

Choose a reason for hiding this comment

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

not sure I understand the problem with the tag, sorry? is it just that this makes joining with other metrics using domain_name hard?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

to pull from (and paraphrase) chat:

I'm trying to build this data into our client dashboard, where we have a $domain variable with domain-name in it, which is used for all of our other domain-related queries.
Right now this is emitting collection_name_domain_name, which needs pretty complex rewrites / set intersections / etc to correlate with the domain-name value. Possible, but a bit absurd.

return simpleMetric{key: globalRatelimitKey, value: value}
}

// GlobalRatelimiterTypeTag reports the "limit usage type" being reported, e.g. global vs local
Expand Down
14 changes: 7 additions & 7 deletions common/quotas/global/collection/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,14 +351,14 @@ func (c *Collection) backgroundUpdateLoop() {
if counts.Idle > gcAfterIdle || c.shouldDeleteKey(mode, true) {
c.logger.Debug(
"deleting local ratelimiter",
tag.GlobalRatelimiterKey(string(gkey)),
tag.GlobalRatelimiterKey(string(k)),
tag.GlobalRatelimiterIdleCount(counts.Idle),
)
c.local.Delete(k)
return true // continue iterating, possibly delete others too
}

c.sendMetrics(gkey, k, true, mode, counts)
c.sendMetrics(k, true, mode, counts)
return true
})
}()
Expand All @@ -374,7 +374,7 @@ func (c *Collection) backgroundUpdateLoop() {
if counts.Idle > gcAfterIdle || c.shouldDeleteKey(mode, false) {
c.logger.Debug(
"deleting global ratelimiter",
tag.GlobalRatelimiterKey(string(gkey)),
tag.GlobalRatelimiterKey(string(k)),
tag.GlobalRatelimiterIdleCount(counts.Idle),
)
c.global.Delete(k)
Expand All @@ -394,7 +394,7 @@ func (c *Collection) backgroundUpdateLoop() {
Allowed: counts.Allowed,
Rejected: counts.Rejected,
}
c.sendMetrics(gkey, k, false, mode, counts)
c.sendMetrics(k, false, mode, counts)

return true
})
Expand All @@ -419,13 +419,13 @@ func (c *Collection) backgroundUpdateLoop() {
}
}

func (c *Collection) sendMetrics(gkey shared.GlobalKey, lkey shared.LocalKey, isLocalLimiter bool, mode keyMode, usage internal.UsageMetrics) {
func (c *Collection) sendMetrics(lkey shared.LocalKey, isLocalLimiter bool, mode keyMode, usage internal.UsageMetrics) {
// emit quota information to make monitoring easier.
// regrettably this will only be emitted when the key is (recently) in use, but
// for active users this is probably sufficient. other cases will probably need
// a continual "emit all quotas" loop somewhere.
c.scope.
Tagged(metrics.GlobalRatelimiterKeyTag(string(gkey))).
Tagged(metrics.GlobalRatelimiterKeyTag(string(lkey))).
UpdateGauge(metrics.GlobalRatelimiterQuota, float64(c.targetRPS(lkey)))

limitType := "global"
Expand All @@ -435,7 +435,7 @@ func (c *Collection) sendMetrics(gkey shared.GlobalKey, lkey shared.LocalKey, is
limitTypeIsPrimary := isLocalLimiter && mode.isLocalPrimary() || !isLocalLimiter && mode.isGlobalPrimary()

scope := c.scope.Tagged(
metrics.GlobalRatelimiterKeyTag(string(gkey)),
metrics.GlobalRatelimiterKeyTag(string(lkey)),
metrics.GlobalRatelimiterTypeTag(limitType),
// useful for being able to tell when a key is "in use" or not, e.g. for monitoring purposes
metrics.GlobalRatelimiterIsPrimary(limitTypeIsPrimary),
Expand Down
Loading