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

rpc server: add prometheus label is_rate_limited #3504

Merged
merged 8 commits into from
Mar 5, 2024

Conversation

niklasad1
Copy link
Member

@niklasad1 niklasad1 commented Feb 28, 2024

After some discussion with @kogeler after the we added the rate-limit middleware it may slow down
the rpc call timings metrics significantly because it works as follows:

  1. The rate limit guard is checked when the call comes and if a slot is available -> process the call
  2. If no free spot is available then the call will be sleeping jitter_delay + min_time_rate_guard then woken up and checked at most ten times
  3. If no spot is available after 10 iterations -> the call is rejected (this may take tens of seconds)

Thus, this PR adds a label "is_rate_limited" to filter those out on the metrics "substrate_rpc_calls_time" and "substrate_rpc_calls_finished".

I had to merge two middleware layers Metrics and RateLimit to avoid shared state in a hacky way.

@niklasad1 niklasad1 changed the title rpc: add prometheus label is_rate_limited rpc server: add prometheus label is_rate_limited Feb 28, 2024
@niklasad1 niklasad1 added the T0-node This PR/Issue is related to the topic “node”. label Feb 28, 2024
Comment on lines +176 to +188
let middleware_layer = match (metrics, rate_limit) {
(None, None) => None,
(Some(metrics), None) => Some(
MiddlewareLayer::new().with_metrics(Metrics::new(metrics, transport_label)),
),
(None, Some(rate_limit)) =>
Some(MiddlewareLayer::new().with_rate_limit_per_minute(rate_limit)),
(Some(metrics), Some(rate_limit)) => Some(
MiddlewareLayer::new()
.with_metrics(Metrics::new(metrics, transport_label))
.with_rate_limit_per_minute(rate_limit),
),
};
Copy link
Contributor

Choose a reason for hiding this comment

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

would it be possible to do something like this instead to simplify a bit?:

let mut middleware_layer = MiddlewareLayer::new();

if let Some(metrics) = metrics {
    middleware_layer = middleware_layer.with_metrics(Metrics::new(metrics, transport_label));
}
if let Some(rate_limit) = rate_limit {
    middleware_layer = middleware_layer.with_rate_limit_per_minute(rate_limit);
}

Copy link
Member Author

@niklasad1 niklasad1 Feb 29, 2024

Choose a reason for hiding this comment

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

It's possible but it doesn't make sense to enable middleware if it's empty that's why I did it like that i.e, to box the future when it's a no-op middleware.

Copy link
Contributor

@jsdw jsdw left a comment

Choose a reason for hiding this comment

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

Looks good to me!

@niklasad1 niklasad1 enabled auto-merge March 5, 2024 08:22
@niklasad1 niklasad1 added this pull request to the merge queue Mar 5, 2024
Merged via the queue into master with commit efcea0e Mar 5, 2024
129 of 130 checks passed
@niklasad1 niklasad1 deleted the na-is-rate-limited-label branch March 5, 2024 10:28
bgallois pushed a commit to duniter/duniter-polkadot-sdk that referenced this pull request Mar 25, 2024
After some discussion with @kogeler after the we added the rate-limit
middleware it may slow down
the rpc call timings metrics significantly because it works as follows:

1. The rate limit guard is checked when the call comes and if a slot is
available -> process the call
2. If no free spot is available then the call will be sleeping
`jitter_delay + min_time_rate_guard` then woken up and checked at most
ten times
3. If no spot is available after 10 iterations -> the call is rejected
(this may take tens of seconds)

Thus, this PR adds a label "is_rate_limited" to filter those out on the
metrics "substrate_rpc_calls_time" and "substrate_rpc_calls_finished".

I had to merge two middleware layers Metrics and RateLimit to avoid
shared state in a hacky way.

---------

Co-authored-by: James Wilson <james@jsdw.me>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T0-node This PR/Issue is related to the topic “node”.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants