Skip to content

Commit

Permalink
Add default Fallback Scrape Config
Browse files Browse the repository at this point in the history
Signed-off-by: Arthur Silva Sens <[email protected]>
  • Loading branch information
ArthurSens committed Jan 10, 2025
1 parent 6d3856c commit 9f01ea1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
9 changes: 9 additions & 0 deletions receiver/prometheusreceiver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}
}
}
18 changes: 18 additions & 0 deletions receiver/prometheusreceiver/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 9f01ea1

Please sign in to comment.