Skip to content

feat(distributor): Add simulated latency #16733

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

Merged
merged 5 commits into from
Mar 14, 2025

Conversation

cyriltovena
Copy link
Contributor

This change adds the ability to configure simulated latency on a per-tenant basis in the distributor. This is useful for testing and simulating real-world conditions.

The implementation:

  • Adds a new simulated_latency field to the Limits struct using model.Duration
  • Adds a SimulatedLatency method to the Overrides struct
  • Adds the SimulatedLatency method to the Limits interface
  • Implements a waitSimulatedLatency method in the Distributor
  • Adds a defer call in the Push method to apply the latency at the end of processing

The latency is applied at the end of request processing to ensure that all requests take at least the configured latency time, but without adding additional latency to requests that are already slow.

When the configured latency is 0s, the feature is disabled.

This change adds the ability to configure simulated latency on a per-tenant basis
in the distributor. This is useful for testing and simulating real-world conditions.

The implementation:
- Adds a new `simulated_latency` field to the Limits struct using model.Duration
- Adds a SimulatedLatency method to the Overrides struct
- Adds the SimulatedLatency method to the Limits interface
- Implements a waitSimulatedLatency method in the Distributor
- Adds a defer call in the Push method to apply the latency at the end of processing

The latency is applied at the end of request processing to ensure that all requests
take at least the configured latency time, but without adding additional latency to
requests that are already slow.

When the configured latency is 0s, the feature is disabled.
@cyriltovena cyriltovena requested a review from a team as a code owner March 13, 2025 10:28
@github-actions github-actions bot added the type/docs Issues related to technical documentation; the Docs Squad uses this label across many repositories label Mar 13, 2025
@cyriltovena cyriltovena changed the title Feat simulated latency Mar 13, 2025
@@ -485,11 +485,30 @@ func (p *pushTracker) doneWithResult(err error) {
}
}

func (d *Distributor) waitSimulatedLatency(ctx context.Context, tenantID string, start time.Time) {
latency := d.validator.Limits.SimulatedPushLatency(tenantID)
if latency > 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe this line can be removed as the condition is tested again when we check if wait > 0. For example, when latency is 0, then wait is negative.

However, I suppose someone could set latency to a negative number though? Is that a concern?

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh I see, you are using 0 as a special value to disable the feature.

func (d *Distributor) Push(ctx context.Context, req *logproto.PushRequest) (*logproto.PushResponse, error) {
tenantID, err := tenant.TenantID(ctx)
if err != nil {
return nil, err
}
start := time.Now()
defer d.waitSimulatedLatency(ctx, tenantID, start)
Copy link
Contributor

Choose a reason for hiding this comment

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

Btw it's OK to use time.Now() as the argument to a deferred call. Go evaluates arguments for deferred functions when the defer is pushed onto the call stack, not when it is executed.

Suggested change
defer d.waitSimulatedLatency(ctx, tenantID, start)
defer d.waitSimulatedLatency(ctx, tenantID, time.Now())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size/M type/docs Issues related to technical documentation; the Docs Squad uses this label across many repositories
2 participants