Skip to content

Commit

Permalink
metrics-generator: add override to disable metrics collection (#1375)
Browse files Browse the repository at this point in the history
  • Loading branch information
kvrhdn authored Apr 15, 2022
1 parent 288c32c commit 083f714
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 1 deletion.
4 changes: 4 additions & 0 deletions modules/generator/instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ func (m *mockOverrides) MetricsGeneratorProcessors(userID string) map[string]str
return m.processors
}

func (m *mockOverrides) MetricsGeneratorDisableCollection(userID string) bool {
return false
}

type noopStorage struct{}

var _ storage.Storage = (*noopStorage)(nil)
Expand Down
1 change: 1 addition & 0 deletions modules/generator/registry/overrides.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
type Overrides interface {
MetricsGeneratorMaxActiveSeries(userID string) uint32
MetricsGeneratorCollectionInterval(userID string) time.Duration
MetricsGeneratorDisableCollection(userID string) bool
}

var _ Overrides = (*overrides.Overrides)(nil)
4 changes: 4 additions & 0 deletions modules/generator/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ func (r *ManagedRegistry) onRemoveMetricSeries(count uint32) {
}

func (r *ManagedRegistry) collectMetrics(ctx context.Context) {
if r.overrides.MetricsGeneratorDisableCollection(r.tenant) {
return
}

r.metricsMtx.RLock()
defer r.metricsMtx.RUnlock()

Expand Down
27 changes: 26 additions & 1 deletion modules/generator/registry/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,26 @@ func TestManagedRegistry_maxSeries(t *testing.T) {
collectRegistryMetricsAndAssert(t, registry, appender, expectedSamples)
}

func TestManagedRegistry_disableCollection(t *testing.T) {
appender := &capturingAppender{}

overrides := &mockOverrides{
disableCollection: true,
}
registry := New(&Config{}, overrides, "test", appender, log.NewNopLogger())
defer registry.Close()

counter := registry.NewCounter("metric_1", nil)
counter.Inc(nil, 1.0)

// active series are still tracked
assert.Equal(t, uint32(1), registry.activeSeries.Load())
// but no samples are collected and sent out
registry.collectMetrics(context.Background())
assert.Empty(t, appender.samples)
assert.Empty(t, appender.exemplars)
}

func collectRegistryMetricsAndAssert(t *testing.T, r *ManagedRegistry, appender *capturingAppender, expectedSamples []sample) {
assert.Equal(t, uint32(len(expectedSamples)), r.activeSeries.Load())

Expand All @@ -186,7 +206,8 @@ func collectRegistryMetricsAndAssert(t *testing.T, r *ManagedRegistry, appender
}

type mockOverrides struct {
maxActiveSeries uint32
maxActiveSeries uint32
disableCollection bool
}

var _ Overrides = (*mockOverrides)(nil)
Expand All @@ -199,6 +220,10 @@ func (m *mockOverrides) MetricsGeneratorCollectionInterval(userID string) time.D
return 15 * time.Second
}

func (m *mockOverrides) MetricsGeneratorDisableCollection(userID string) bool {
return m.disableCollection
}

func mustGetHostname() string {
hostname, _ := os.Hostname()
return hostname
Expand Down
1 change: 1 addition & 0 deletions modules/overrides/limits.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type Limits struct {
MetricsGeneratorProcessors ListToMap `yaml:"metrics_generator_processors" json:"metrics_generator_processors"`
MetricsGeneratorMaxActiveSeries uint32 `yaml:"metrics_generator_max_active_series" json:"metrics_generator_max_active_series"`
MetricsGeneratorCollectionInterval time.Duration `yaml:"metrics_generator_collection_interval" json:"metrics_generator_collection_interval"`
MetricsGeneratorDisableCollection bool `yaml:"metrics_generator_disable_collection" json:"metrics_generator_disable_collection"`

// Compactor enforced limits.
BlockRetention model.Duration `yaml:"block_retention" json:"block_retention"`
Expand Down
5 changes: 5 additions & 0 deletions modules/overrides/overrides.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,11 @@ func (o *Overrides) MetricsGeneratorCollectionInterval(userID string) time.Durat
return o.getOverridesForUser(userID).MetricsGeneratorCollectionInterval
}

// MetricsGeneratorDisableRemoteWrite controls whether metrics are remote written for this tenant.
func (o *Overrides) MetricsGeneratorDisableCollection(userID string) bool {
return o.getOverridesForUser(userID).MetricsGeneratorDisableCollection
}

// BlockRetention is the duration of the block retention for this tenant.
func (o *Overrides) BlockRetention(userID string) time.Duration {
return time.Duration(o.getOverridesForUser(userID).BlockRetention)
Expand Down

0 comments on commit 083f714

Please sign in to comment.