diff --git a/docs/sources/shared/reference/components/prom-operator-scrape.md b/docs/sources/shared/reference/components/prom-operator-scrape.md index 345fa3304b7..caf853d5e16 100644 --- a/docs/sources/shared/reference/components/prom-operator-scrape.md +++ b/docs/sources/shared/reference/components/prom-operator-scrape.md @@ -6,6 +6,7 @@ headless: true | Name | Type | Description | Default | Required | |----------------------------|------------|------------------------------------------------------------------------------------------------------------------------------|---------|----------| +| `default_sample_limit` | `int` | The default maximum samples per scrape. Used as the default if the target resource doesn't provide a sample limit. | | no | | `default_scrape_interval` | `duration` | The default interval between scraping targets. Used as the default if the target resource doesn't provide a scrape interval. | `1m` | no | | `default_scrape_timeout` | `duration` | The default timeout for scrape requests. Used as the default if the target resource doesn't provide a scrape timeout. | `10s` | no | | `scrape_native_histograms` | `bool` | Whether to scrape native histograms from targets. | `false` | no | diff --git a/internal/component/prometheus/operator/configgen/config_gen.go b/internal/component/prometheus/operator/configgen/config_gen.go index df93414be6c..ddcc594bd13 100644 --- a/internal/component/prometheus/operator/configgen/config_gen.go +++ b/internal/component/prometheus/operator/configgen/config_gen.go @@ -190,6 +190,10 @@ func (cg *ConfigGenerator) generateDefaultScrapeConfig() *config.ScrapeConfig { c.ScrapeTimeout = model.Duration(opt.DefaultScrapeTimeout) } + if opt.DefaultSampleLimit != 0 { + c.SampleLimit = opt.DefaultSampleLimit + } + if opt.ScrapeNativeHistograms { c.ScrapeProtocols = config.DefaultProtoFirstScrapeProtocols } diff --git a/internal/component/prometheus/operator/configgen/config_gen_podmonitor_test.go b/internal/component/prometheus/operator/configgen/config_gen_podmonitor_test.go index 496eeda10dc..adfb31f2e19 100644 --- a/internal/component/prometheus/operator/configgen/config_gen_podmonitor_test.go +++ b/internal/component/prometheus/operator/configgen/config_gen_podmonitor_test.go @@ -92,6 +92,7 @@ func TestGeneratePodMonitorConfig(t *testing.T) { }, }, ScrapeNativeHistograms: falsePtr, + SampleLimit: 18, AlwaysScrapeClassicHistograms: falsePtr, ConvertClassicHistogramsToNHCB: falsePtr, MetricNameValidationScheme: model.LegacyValidation, @@ -155,6 +156,7 @@ func TestGeneratePodMonitorConfig(t *testing.T) { }, }, }, + SampleLimit: 18, ScrapeNativeHistograms: falsePtr, AlwaysScrapeClassicHistograms: falsePtr, ConvertClassicHistogramsToNHCB: falsePtr, @@ -219,6 +221,7 @@ func TestGeneratePodMonitorConfig(t *testing.T) { }, }, }, + SampleLimit: 18, ScrapeNativeHistograms: falsePtr, AlwaysScrapeClassicHistograms: falsePtr, ConvertClassicHistogramsToNHCB: falsePtr, @@ -281,6 +284,7 @@ func TestGeneratePodMonitorConfig(t *testing.T) { }, }, }, + SampleLimit: 18, ScrapeNativeHistograms: falsePtr, AlwaysScrapeClassicHistograms: falsePtr, ConvertClassicHistogramsToNHCB: falsePtr, @@ -345,6 +349,7 @@ func TestGeneratePodMonitorConfig(t *testing.T) { }, }, }, + SampleLimit: 18, ScrapeNativeHistograms: falsePtr, AlwaysScrapeClassicHistograms: falsePtr, ConvertClassicHistogramsToNHCB: falsePtr, @@ -538,6 +543,7 @@ func TestGeneratePodMonitorConfig(t *testing.T) { DefaultScrapeInterval: time.Hour, DefaultScrapeTimeout: 42 * time.Second, ScrapeNativeHistograms: false, + DefaultSampleLimit: 18, }, } cfg, err := cg.GeneratePodMonitorConfig(tc.m, tc.ep, 1) diff --git a/internal/component/prometheus/operator/configgen/config_gen_test.go b/internal/component/prometheus/operator/configgen/config_gen_test.go index db7bf3da92b..97c21552ede 100644 --- a/internal/component/prometheus/operator/configgen/config_gen_test.go +++ b/internal/component/prometheus/operator/configgen/config_gen_test.go @@ -450,6 +450,7 @@ func TestGenerateDefaultScrapeConfig(t *testing.T) { scrapeOptions: operator.ScrapeOptions{ DefaultScrapeInterval: 30 * time.Second, DefaultScrapeTimeout: 5 * time.Second, + DefaultSampleLimit: 100, }, expectedInterval: 30 * time.Second, expectedTimeout: 5 * time.Second, @@ -466,6 +467,7 @@ func TestGenerateDefaultScrapeConfig(t *testing.T) { assert.Equal(t, model.Duration(tt.expectedInterval), got.ScrapeInterval) assert.Equal(t, model.Duration(tt.expectedTimeout), got.ScrapeTimeout) assert.Equal(t, tt.expectedFallbackProtocol, got.ScrapeFallbackProtocol) + assert.Equal(t, tt.scrapeOptions.DefaultSampleLimit, got.SampleLimit) }) } } diff --git a/internal/component/prometheus/operator/types.go b/internal/component/prometheus/operator/types.go index c4b8d5baaf0..b78adee704c 100644 --- a/internal/component/prometheus/operator/types.go +++ b/internal/component/prometheus/operator/types.go @@ -50,6 +50,9 @@ type ScrapeOptions struct { // DefaultScrapeTimeout is the default timeout to scrape targets. DefaultScrapeTimeout time.Duration `alloy:"default_scrape_timeout,attr,optional"` + // DefaultSampleLimit is the default sample limit per scrape. + DefaultSampleLimit uint `alloy:"default_sample_limit,attr,optional"` + // ScrapeNativeHistograms enables scraping of Prometheus native histograms. ScrapeNativeHistograms bool `alloy:"scrape_native_histograms,attr,optional"` } @@ -58,6 +61,7 @@ func (s *ScrapeOptions) GlobalConfig() promconfig.GlobalConfig { cfg := promconfig.DefaultGlobalConfig cfg.ScrapeInterval = model.Duration(s.DefaultScrapeInterval) cfg.ScrapeTimeout = model.Duration(s.DefaultScrapeTimeout) + cfg.SampleLimit = s.DefaultSampleLimit // TODO: add support for choosing validation scheme: https://github.com/grafana/alloy/issues/4122 cfg.MetricNameValidationScheme = model.LegacyValidation cfg.MetricNameEscapingScheme = model.EscapeUnderscores