Skip to content

Commit 6b6663b

Browse files
authored
discarded samples per labelset metrics for throttle by labelset (#6492)
* discarded samples per labelset metrics for throttle by labelset Signed-off-by: Ben Ye <[email protected]> * address comment Signed-off-by: Ben Ye <[email protected]> --------- Signed-off-by: Ben Ye <[email protected]>
1 parent 9571b8a commit 6b6663b

File tree

9 files changed

+747
-266
lines changed

9 files changed

+747
-266
lines changed

pkg/distributor/distributor.go

+28-4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
"github.com/cortexproject/cortex/pkg/tenant"
3737
"github.com/cortexproject/cortex/pkg/util"
3838
"github.com/cortexproject/cortex/pkg/util/extract"
39+
"github.com/cortexproject/cortex/pkg/util/labelset"
3940
"github.com/cortexproject/cortex/pkg/util/limiter"
4041
util_log "github.com/cortexproject/cortex/pkg/util/log"
4142
util_math "github.com/cortexproject/cortex/pkg/util/math"
@@ -130,7 +131,7 @@ type Distributor struct {
130131
asyncExecutor util.AsyncExecutor
131132

132133
// Map to track label sets from user.
133-
labelSetTracker *labelSetTracker
134+
labelSetTracker *labelset.LabelSetTracker
134135
}
135136

136137
// Config contains the configuration required to
@@ -388,7 +389,7 @@ func New(cfg Config, clientConfig ingester_client.Config, limits *validation.Ove
388389
asyncExecutor: util.NewNoOpExecutor(),
389390
}
390391

391-
d.labelSetTracker = newLabelSetTracker(d.receivedSamplesPerLabelSet)
392+
d.labelSetTracker = labelset.NewLabelSetTracker()
392393

393394
if cfg.NumPushWorkers > 0 {
394395
util_log.WarnExperimentalUse("Distributor: using goroutine worker pool")
@@ -810,7 +811,16 @@ func (d *Distributor) updateLabelSetMetrics() {
810811
}
811812
}
812813

813-
d.labelSetTracker.updateMetrics(activeUserSet)
814+
d.labelSetTracker.UpdateMetrics(activeUserSet, func(user, labelSetStr string, removeUser bool) {
815+
if removeUser {
816+
if err := util.DeleteMatchingLabels(d.receivedSamplesPerLabelSet, map[string]string{"user": user}); err != nil {
817+
level.Warn(d.log).Log("msg", "failed to remove cortex_distributor_received_samples_per_labelset_total metric for user", "user", user, "err", err)
818+
}
819+
return
820+
}
821+
d.receivedSamplesPerLabelSet.DeleteLabelValues(user, sampleMetricTypeFloat, labelSetStr)
822+
d.receivedSamplesPerLabelSet.DeleteLabelValues(user, sampleMetricTypeHistogram, labelSetStr)
823+
})
814824
}
815825

816826
func (d *Distributor) cleanStaleIngesterMetrics() {
@@ -913,6 +923,12 @@ func (d *Distributor) prepareMetadataKeys(req *cortexpb.WriteRequest, limits *va
913923
return metadataKeys, validatedMetadata, firstPartialErr
914924
}
915925

926+
type samplesLabelSetEntry struct {
927+
floatSamples int64
928+
histogramSamples int64
929+
labels labels.Labels
930+
}
931+
916932
func (d *Distributor) prepareSeriesKeys(ctx context.Context, req *cortexpb.WriteRequest, userID string, limits *validation.Limits, removeReplica bool) ([]uint32, []cortexpb.PreallocTimeseries, int, int, int, error, error) {
917933
pSpan, _ := opentracing.StartSpanFromContext(ctx, "prepareSeriesKeys")
918934
defer pSpan.Finish()
@@ -1070,8 +1086,16 @@ func (d *Distributor) prepareSeriesKeys(ctx context.Context, req *cortexpb.Write
10701086
validatedExemplars += len(ts.Exemplars)
10711087
}
10721088
for h, counter := range labelSetCounters {
1073-
d.labelSetTracker.increaseSamplesLabelSet(userID, h, counter.labels, counter.floatSamples, counter.histogramSamples)
1089+
d.labelSetTracker.Track(userID, h, counter.labels)
1090+
labelSetStr := counter.labels.String()
1091+
if counter.floatSamples > 0 {
1092+
d.receivedSamplesPerLabelSet.WithLabelValues(userID, sampleMetricTypeFloat, labelSetStr).Add(float64(counter.floatSamples))
1093+
}
1094+
if counter.histogramSamples > 0 {
1095+
d.receivedSamplesPerLabelSet.WithLabelValues(userID, sampleMetricTypeHistogram, labelSetStr).Add(float64(counter.histogramSamples))
1096+
}
10741097
}
1098+
10751099
return seriesKeys, validatedTimeseries, validatedFloatSamples, validatedHistogramSamples, validatedExemplars, firstPartialErr, nil
10761100
}
10771101

pkg/distributor/metrics.go

-95
This file was deleted.

pkg/distributor/metrics_test.go

-103
This file was deleted.

0 commit comments

Comments
 (0)