Skip to content
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
* [BUGFIX] Ring: Fix `DoBatch` counting a key more than once when more replicas than the quorum succeed, which in rare cases could hide a write error from a key that did not reach quorum. A distributor push now always waits until every series reaches quorum, so it returns the error of a series that misses quorum, and takes as long as its slowest series to reach quorum. #7975
* [BUGFIX] Compactor: Stop downloading the parquet files and the `parquet-converter-mark.json` and `parquet-no-convert-mark.json` markers written by the parquet converter under a block's prefix. Compaction never reads them, so they only used local disk and bandwidth until the compaction job ended. #7972
* [BUGFIX] Ring: Fix shuffle-sharding subrings treating instances with a stale heartbeat as healthy when using the DynamoDB KV store. #7976
* [BUGFIX] Distributor, Ingester: Stop the goroutines updating the `cortex_discarded_series` and `cortex_discarded_series_per_labelset` metrics when the distributor or ingester stops. They were never stopped. #7978

## 1.22.0 2026-11-19

Expand Down
1 change: 1 addition & 0 deletions pkg/distributor/distributor.go
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,7 @@ func (d *Distributor) cleanupInactiveUser(userID string) {
func (d *Distributor) stopping(_ error) error {
d.asyncExecutor.Stop()
d.queryWorkers.Stop()
d.validateMetrics.Stop()
return services.StopManagerAndAwaitStopped(context.Background(), d.subservices)
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/ingester/ingester.go
Original file line number Diff line number Diff line change
Expand Up @@ -1105,6 +1105,8 @@ func (i *Ingester) stopping(_ error) error {
if !i.cfg.BlocksStorageConfig.TSDB.KeepUserTSDBOpenOnShutdown {
i.closeAllTSDB()
}

i.validateMetrics.Stop()
return nil
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/ingester/ingester_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4984,6 +4984,8 @@ func prepareIngesterWithBlocksStorageAndLimits(t testing.TB, ingesterCfg Config,
if err != nil {
return nil, err
}
// Many tests never start the ingester, so its stopping function never runs.
t.Cleanup(ingester.validateMetrics.Stop)

return ingester, nil
}
Expand Down Expand Up @@ -5157,6 +5159,8 @@ func TestIngester_OpenExistingTSDBOnStartup(t *testing.T) {

ingester, err := New(ingesterCfg, overrides, prometheus.NewRegistry(), log.NewNopLogger(), nil)
require.NoError(t, err)
// The stopping function doesn't run when the ingester fails to start.
t.Cleanup(ingester.validateMetrics.Stop)

startErr := services.StartAndAwaitRunning(context.Background(), ingester)
if testData.expectedErr == "" {
Expand Down
4 changes: 3 additions & 1 deletion pkg/ingester/user_metrics_metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ func Test_UserMetricsMetadata(t *testing.T) {

for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
userMetricsMetadata := newMetadataMap(limiter, m, validation.NewValidateMetrics(reg), userId, test.skipMetadataLimits)
validateMetrics := validation.NewValidateMetrics(reg)
defer validateMetrics.Stop()
userMetricsMetadata := newMetadataMap(limiter, m, validateMetrics, userId, test.skipMetadataLimits)

addMetricMetadata := func(name string, i int) {
metadata := &cortexpb.MetricMetadata{
Expand Down
13 changes: 6 additions & 7 deletions pkg/util/discardedseries/perlabelset_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package discardedseries

import (
"sync"
"time"

"github.com/prometheus/client_golang/prometheus"
)
Expand All @@ -21,13 +20,16 @@ type DiscardedSeriesPerLabelsetTracker struct {
*sync.RWMutex
userLabelsetMap map[string]*labelsetCounterStruct
discardedSeriesPerLabelsetGauge *prometheus.GaugeVec

metricsVendor
}

func NewDiscardedSeriesPerLabelsetTracker(discardedSeriesPerLabelsetGauge *prometheus.GaugeVec) *DiscardedSeriesPerLabelsetTracker {
tracker := &DiscardedSeriesPerLabelsetTracker{
RWMutex: &sync.RWMutex{},
userLabelsetMap: make(map[string]*labelsetCounterStruct),
discardedSeriesPerLabelsetGauge: discardedSeriesPerLabelsetGauge,
metricsVendor: newMetricsVendor(),
}
return tracker
}
Expand Down Expand Up @@ -121,13 +123,10 @@ func (t *DiscardedSeriesPerLabelsetTracker) UpdateMetrics() {
}
}

// StartVendDiscardedSeriesMetricGoroutine starts a goroutine which periodically
// updates the metrics. It is stopped by Stop.
func (t *DiscardedSeriesPerLabelsetTracker) StartVendDiscardedSeriesMetricGoroutine() {
go func() {
ticker := time.NewTicker(vendMetricsInterval)
for range ticker.C {
t.UpdateMetrics()
}
}()
t.start(t.UpdateMetrics)
}

// only used in testing
Expand Down
12 changes: 6 additions & 6 deletions pkg/util/discardedseries/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@ type DiscardedSeriesTracker struct {
*sync.RWMutex
reasonUserMap map[string]*userCounterStruct
discardedSeriesGauge *prometheus.GaugeVec

metricsVendor
}

func NewDiscardedSeriesTracker(discardedSeriesGauge *prometheus.GaugeVec) *DiscardedSeriesTracker {
tracker := &DiscardedSeriesTracker{
RWMutex: &sync.RWMutex{},
reasonUserMap: make(map[string]*userCounterStruct),
discardedSeriesGauge: discardedSeriesGauge,
metricsVendor: newMetricsVendor(),
}
return tracker
}
Expand Down Expand Up @@ -113,13 +116,10 @@ func (t *DiscardedSeriesTracker) UpdateMetrics() {
t.RUnlock()
}

// StartVendDiscardedSeriesMetricGoroutine starts a goroutine which periodically
// updates the metrics. It is stopped by Stop.
func (t *DiscardedSeriesTracker) StartVendDiscardedSeriesMetricGoroutine() {
go func() {
ticker := time.NewTicker(vendMetricsInterval)
for range ticker.C {
t.UpdateMetrics()
}
}()
t.start(t.UpdateMetrics)
}

// only used in testing
Expand Down
43 changes: 43 additions & 0 deletions pkg/util/discardedseries/vendor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package discardedseries

import (
"sync"
"time"
)

// metricsVendor periodically updates the metrics of a tracker in a goroutine
// which can be stopped.
type metricsVendor struct {
stop chan struct{}
stopOnce sync.Once
wg sync.WaitGroup
}

func newMetricsVendor() metricsVendor {
return metricsVendor{stop: make(chan struct{})}
}

func (v *metricsVendor) start(updateMetrics func()) {
v.wg.Go(func() {
ticker := time.NewTicker(vendMetricsInterval)
defer ticker.Stop()

for {
select {
case <-ticker.C:
updateMetrics()
case <-v.stop:
return
}
}
})
}

// Stop stops the goroutine updating the metrics and waits until it exits.
// It is safe to call Stop more than once, or when the goroutine was never started.
func (v *metricsVendor) Stop() {
v.stopOnce.Do(func() {
close(v.stop)
})
v.wg.Wait()
}
36 changes: 36 additions & 0 deletions pkg/util/discardedseries/vendor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package discardedseries

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestMetricsVendor_Stop(t *testing.T) {
t.Run("stops the started goroutine", func(t *testing.T) {
v := newMetricsVendor()
v.start(func() {})

// Stop returns only once the goroutine has exited.
v.Stop()
require.True(t, isClosed(v.stop))

// Stopping again is a no-op.
v.Stop()
})

t.Run("can be stopped when never started", func(t *testing.T) {
v := newMetricsVendor()
v.Stop()
require.True(t, isClosed(v.stop))
})
}

func isClosed(ch chan struct{}) bool {
select {
case <-ch:
return true
default:
return false
}
}
6 changes: 6 additions & 0 deletions pkg/util/validation/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,12 @@ func NewValidateMetrics(r prometheus.Registerer) *ValidateMetrics {
return m
}

// Stop stops the goroutines updating the discarded series metrics.
func (m *ValidateMetrics) Stop() {
m.DiscardedSeriesTracker.Stop()
m.DiscardedSeriesPerLabelsetTracker.Stop()
}

// UpdateSamplesDiscardedForSeries updates discarded samples and discarded samples per labelset for the provided reason and series.
// Used in test only for now.
func (m *ValidateMetrics) updateSamplesDiscardedForSeries(userID, reason string, labelSetLimits []LimitsPerLabelSet, lbls labels.Labels, count int) {
Expand Down
9 changes: 9 additions & 0 deletions pkg/util/validation/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func TestValidateLabels_UTF8(t *testing.T) {

reg := prometheus.NewRegistry()
validateMetrics := NewValidateMetrics(reg)
defer validateMetrics.Stop()

cfg.MaxLabelValueLength = 25
cfg.MaxLabelNameLength = 25
Expand Down Expand Up @@ -108,6 +109,7 @@ func TestValidateLabels(t *testing.T) {

reg := prometheus.NewRegistry()
validateMetrics := NewValidateMetrics(reg)
defer validateMetrics.Stop()

cfg.MaxLabelValueLength = 25
cfg.MaxLabelNameLength = 25
Expand Down Expand Up @@ -226,6 +228,7 @@ func TestValidateExemplars(t *testing.T) {
userID := "testUser"
reg := prometheus.NewRegistry()
validateMetrics := NewValidateMetrics(reg)
defer validateMetrics.Stop()
invalidExemplars := []cortexpb.Exemplar{
{
// Missing labels
Expand Down Expand Up @@ -274,6 +277,7 @@ func TestValidateMetadata(t *testing.T) {
cfg.MaxMetadataLength = 22
reg := prometheus.NewRegistry()
validateMetrics := NewValidateMetrics(reg)
defer validateMetrics.Stop()
userID := "testUser"

for _, c := range []struct {
Expand Down Expand Up @@ -342,6 +346,7 @@ func TestValidateLabelOrder(t *testing.T) {
cfg.MaxLabelValueLength = 10
reg := prometheus.NewRegistry()
validateMetrics := NewValidateMetrics(reg)
defer validateMetrics.Stop()
userID := "testUser"

actual := ValidateLabels(validateMetrics, cfg, userID, []cortexpb.LabelAdapter{
Expand Down Expand Up @@ -370,6 +375,7 @@ func TestValidateLabelDuplication(t *testing.T) {
cfg.MaxLabelValueLength = 10
reg := prometheus.NewRegistry()
validateMetrics := NewValidateMetrics(reg)
defer validateMetrics.Stop()
userID := "testUser"

actual := ValidateLabels(validateMetrics, cfg, userID, []cortexpb.LabelAdapter{
Expand Down Expand Up @@ -715,6 +721,7 @@ func TestValidateNativeHistogram(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
reg := prometheus.NewRegistry()
validateMetrics := NewValidateMetrics(reg)
defer validateMetrics.Stop()
limits := new(Limits)
limits.MaxNativeHistogramBuckets = tc.bucketLimit
limits.MaxNativeHistogramSampleSizeBytes = tc.maxNativeHistogramSampleSizeBytesLimit
Expand All @@ -739,6 +746,7 @@ func TestValidateNativeHistogram(t *testing.T) {
func TestValidateMetrics_UpdateSamplesDiscardedForSeries(t *testing.T) {
reg := prometheus.NewRegistry()
v := NewValidateMetrics(reg)
defer v.Stop()
userID := "user"
limits := []LimitsPerLabelSet{
{
Expand Down Expand Up @@ -795,6 +803,7 @@ func TestValidateMetrics_UpdateSamplesDiscardedForSeries(t *testing.T) {
func TestValidateMetrics_UpdateLabelSet(t *testing.T) {
reg := prometheus.NewRegistry()
v := NewValidateMetrics(reg)
defer v.Stop()
userID := "user"
logger := log.NewNopLogger()
limits := []LimitsPerLabelSet{
Expand Down