Skip to content

Commit

Permalink
Do not use two variables for sample counts inside function
Browse files Browse the repository at this point in the history
Signed-off-by: György Krajcsovits <[email protected]>
  • Loading branch information
krajorama committed Feb 7, 2023
1 parent a302848 commit 04be313
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions pkg/distributor/distributor.go
Original file line number Diff line number Diff line change
Expand Up @@ -767,26 +767,24 @@ func (d *Distributor) prePushHaDedupeMiddleware(next push.Func) push.Func {
}

numSamples := 0
numHistograms := 0
group := d.activeGroups.UpdateActiveGroupTimestamp(userID, validation.GroupLabel(d.limits, userID, req.Timeseries), time.Now())
for _, ts := range req.Timeseries {
numSamples += len(ts.Samples)
numHistograms += len(ts.Histograms)
numSamples += len(ts.Samples) + len(ts.Histograms)
}

removeReplica, err := d.checkSample(ctx, userID, cluster, replica)
if err != nil {
if errors.Is(err, replicasNotMatchError{}) {
// These samples and histograms have been deduped.
if numSamples+numHistograms > 0 {
d.dedupedSamples.WithLabelValues(userID, cluster).Add(float64(numSamples + numHistograms))
if numSamples > 0 {
d.dedupedSamples.WithLabelValues(userID, cluster).Add(float64(numSamples))
}
return nil, httpgrpc.Errorf(http.StatusAccepted, err.Error())
}

if errors.Is(err, tooManyClustersError{}) {
if numSamples+numHistograms > 0 {
d.discardedSamplesTooManyHaClusters.WithLabelValues(userID, group).Add(float64(numSamples + numHistograms))
if numSamples > 0 {
d.discardedSamplesTooManyHaClusters.WithLabelValues(userID, group).Add(float64(numSamples))
}

return nil, httpgrpc.Errorf(http.StatusBadRequest, err.Error())
Expand All @@ -804,8 +802,8 @@ func (d *Distributor) prePushHaDedupeMiddleware(next push.Func) push.Func {
}
} else {
// If there wasn't an error but removeReplica is false that means we didn't find both HA labels.
if numSamples+numHistograms > 0 {
d.nonHASamples.WithLabelValues(userID).Add(float64(numSamples + numHistograms))
if numSamples > 0 {
d.nonHASamples.WithLabelValues(userID).Add(float64(numSamples))
}
}

Expand Down

0 comments on commit 04be313

Please sign in to comment.