Bug fix: Return 429 response when Native Histogram samples are discarded by Native Histogram Ingestion Rate Limiter by PaurushGarg · Pull Request #6994 · cortexproject/cortex · GitHub
Skip to content
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
30 changes: 20 additions & 10 deletions pkg/distributor/distributor.go
Original file line number Diff line number Diff line change
Expand Up @@ -778,16 +778,7 @@ func (d *Distributor) Push(ctx context.Context, req *cortexpb.WriteRequest) (*co
d.receivedExemplars.WithLabelValues(userID).Add(float64(validatedExemplars))
d.receivedMetadata.WithLabelValues(userID).Add(float64(len(validatedMetadata)))

if !d.nativeHistogramIngestionRateLimiter.AllowN(now, userID, validatedHistogramSamples) {
level.Warn(d.log).Log("msg", "native histogram ingestion rate limit (%v) exceeded while adding %d native histogram samples", d.nativeHistogramIngestionRateLimiter.Limit(now, userID), validatedHistogramSamples)
d.validateMetrics.DiscardedSamples.WithLabelValues(validation.NativeHistogramRateLimited, userID).Add(float64(validatedHistogramSamples))
validatedHistogramSamples = 0
} else {
seriesKeys = append(seriesKeys, nhSeriesKeys...)
validatedTimeseries = append(validatedTimeseries, nhValidatedTimeseries...)
}

if len(seriesKeys) == 0 && len(metadataKeys) == 0 {
if len(seriesKeys) == 0 && len(nhSeriesKeys) == 0 && len(metadataKeys) == 0 {
// Ensure the request slice is reused if there's no series or metadata passing the validation.
cortexpb.ReuseSlice(req.Timeseries)

Expand All @@ -812,6 +803,17 @@ func (d *Distributor) Push(ctx context.Context, req *cortexpb.WriteRequest) (*co
// totalN included samples and metadata. Ingester follows this pattern when computing its ingestion rate.
d.ingestionRate.Add(int64(totalN))

var nativeHistogramErr error

if !d.nativeHistogramIngestionRateLimiter.AllowN(now, userID, validatedHistogramSamples) {
d.validateMetrics.DiscardedSamples.WithLabelValues(validation.NativeHistogramRateLimited, userID).Add(float64(validatedHistogramSamples))
nativeHistogramErr = httpgrpc.Errorf(http.StatusTooManyRequests, "native histogram ingestion rate limit (%v) exceeded while adding %d native histogram samples", d.nativeHistogramIngestionRateLimiter.Limit(now, userID), validatedHistogramSamples)
validatedHistogramSamples = 0
} else {
seriesKeys = append(seriesKeys, nhSeriesKeys...)
validatedTimeseries = append(validatedTimeseries, nhValidatedTimeseries...)
}

subRing := d.ingestersRing

// Obtain a subring if required.
Expand All @@ -822,6 +824,10 @@ func (d *Distributor) Push(ctx context.Context, req *cortexpb.WriteRequest) (*co
keys := append(seriesKeys, metadataKeys...)
initialMetadataIndex := len(seriesKeys)

if len(keys) == 0 && nativeHistogramErr != nil {
return nil, nativeHistogramErr
Comment thread
yeya24 marked this conversation as resolved.
}

err = d.doBatch(ctx, req, subRing, keys, initialMetadataIndex, validatedMetadata, validatedTimeseries, userID)
if err != nil {
return nil, err
Expand All @@ -837,6 +843,10 @@ func (d *Distributor) Push(ctx context.Context, req *cortexpb.WriteRequest) (*co
resp.Exemplars = int64(validatedExemplars)
}

if nativeHistogramErr != nil {
return resp, nativeHistogramErr
Comment thread
yeya24 marked this conversation as resolved.
}

return resp, firstPartialErr
}

Expand Down
91 changes: 60 additions & 31 deletions pkg/distributor/distributor_test.go
Loading