From 9f01ea12b95521a7ec35b2c930a04a71183d560b Mon Sep 17 00:00:00 2001 From: Arthur Silva Sens Date: Fri, 10 Jan 2025 15:47:08 -0300 Subject: [PATCH] Add default Fallback Scrape Config Signed-off-by: Arthur Silva Sens --- receiver/prometheusreceiver/factory.go | 9 +++++++++ receiver/prometheusreceiver/factory_test.go | 18 ++++++++++++++++++ .../config_fallback_scrape_protocol.yaml | 8 ++++++++ 3 files changed, 35 insertions(+) create mode 100644 receiver/prometheusreceiver/testdata/config_fallback_scrape_protocol.yaml diff --git a/receiver/prometheusreceiver/factory.go b/receiver/prometheusreceiver/factory.go index 247ffb428cdd..bbb82a9442e9 100644 --- a/receiver/prometheusreceiver/factory.go +++ b/receiver/prometheusreceiver/factory.go @@ -63,6 +63,7 @@ func createMetricsReceiver( nextConsumer consumer.Metrics, ) (receiver.Metrics, error) { configWarnings(set.Logger, cfg.(*Config)) + addDefaultFallbackScrapeProtocol(cfg.(*Config)) return newPrometheusReceiver(set, cfg.(*Config), nextConsumer), nil } @@ -75,3 +76,11 @@ func configWarnings(logger *zap.Logger, cfg *Config) { } } } + +func addDefaultFallbackScrapeProtocol(cfg *Config) { + for _, sc := range cfg.PrometheusConfig.ScrapeConfigs { + if sc.ScrapeFallbackProtocol == "" { + sc.ScrapeFallbackProtocol = promconfig.PrometheusText1_0_0 + } + } +} diff --git a/receiver/prometheusreceiver/factory_test.go b/receiver/prometheusreceiver/factory_test.go index 2fe0592f7d2d..9f52e9310d20 100644 --- a/receiver/prometheusreceiver/factory_test.go +++ b/receiver/prometheusreceiver/factory_test.go @@ -8,6 +8,7 @@ import ( "path/filepath" "testing" + promconfig "github.com/prometheus/prometheus/config" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.opentelemetry.io/collector/component" @@ -62,3 +63,20 @@ func TestMultipleCreate(t *testing.T) { require.NoError(t, secondRcvr.Start(context.Background(), host)) require.NoError(t, secondRcvr.Shutdown(context.Background())) } + +func TestDefaultFallbackScrapeProtocol(t *testing.T) { + cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config_fallback_scrape_protocol.yaml")) + require.NoError(t, err) + factory := NewFactory() + cfg := factory.CreateDefaultConfig() + + sub, err := cm.Sub(component.NewIDWithName(metadata.Type, "").String()) + require.NoError(t, err) + assert.NoError(t, sub.Unmarshal(cfg)) + + factory.CreateMetrics(context.Background(), receivertest.NewNopSettings(), cfg, consumertest.NewNop()) + + // During receiver creation, scrapeconfig without fallback scrape protocol set, should be set to 'PrometheusText1.0.0'. + assert.Equal(t, promconfig.PrometheusText1_0_0, cfg.(*Config).PrometheusConfig.ScrapeConfigs[0].ScrapeFallbackProtocol) + assert.Equal(t, promconfig.OpenMetricsText1_0_0, cfg.(*Config).PrometheusConfig.ScrapeConfigs[1].ScrapeFallbackProtocol) +} diff --git a/receiver/prometheusreceiver/testdata/config_fallback_scrape_protocol.yaml b/receiver/prometheusreceiver/testdata/config_fallback_scrape_protocol.yaml new file mode 100644 index 000000000000..1fd77527bc05 --- /dev/null +++ b/receiver/prometheusreceiver/testdata/config_fallback_scrape_protocol.yaml @@ -0,0 +1,8 @@ +prometheus: + config: + scrape_configs: + - job_name: test1 + scrape_interval: 5s + - job_name: test2 + scrape_interval: 5s + fallback_scrape_protocol: OpenMetricsText1.0.0