From 05f16c5b13710bfa43c23cad0ad192127f9e5e1f Mon Sep 17 00:00:00 2001 From: David Ashpole Date: Tue, 15 Jun 2021 11:44:58 -0400 Subject: [PATCH] Disallow renaming metrics using the prometheus receiver (#3410) * disallow renaming metrics using the prometheus receiver * add TODO for removing the validation --- CHANGELOG.md | 1 + receiver/prometheusreceiver/config.go | 14 ++++++++++-- receiver/prometheusreceiver/config_test.go | 12 ++++++++++ receiver/prometheusreceiver/factory.go | 3 ++- .../invalid-config-prometheus-relabel.yaml | 22 +++++++++++++++++++ 5 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 receiver/prometheusreceiver/testdata/invalid-config-prometheus-relabel.yaml diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b57cebe524..11f85fae1b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,7 @@ - Updated configgrpc `ToDialOptions` and confighttp `ToClient` apis to take extensions configuration map (#3340) - Remove `GenerateSequentialTraceID` and `GenerateSequentialSpanIDin` functions in testbed (#3390) - Change "grpc" to "GRPC" in configauth function/type names (#3285) +- Disallow renaming metrics using metric relabel configs (#3410) ## 💡 Enhancements 💡 diff --git a/receiver/prometheusreceiver/config.go b/receiver/prometheusreceiver/config.go index d7080de38f4..f5e7d445f1a 100644 --- a/receiver/prometheusreceiver/config.go +++ b/receiver/prometheusreceiver/config.go @@ -51,8 +51,18 @@ var _ config.CustomUnmarshable = (*Config)(nil) // Validate checks the receiver configuration is valid func (cfg *Config) Validate() error { - if cfg.PrometheusConfig != nil && len(cfg.PrometheusConfig.ScrapeConfigs) == 0 { - return errNilScrapeConfig + if cfg.PrometheusConfig != nil { + if len(cfg.PrometheusConfig.ScrapeConfigs) == 0 { + return errNilScrapeConfig + } + for _, sc := range cfg.PrometheusConfig.ScrapeConfigs { + for _, rc := range sc.MetricRelabelConfigs { + if rc.TargetLabel == "__name__" { + // TODO(#2297): Remove validation after renaming is fixed + return fmt.Errorf("error validating scrapeconfig for job %v: %w", sc.JobName, errRenamingDisallowed) + } + } + } } return nil } diff --git a/receiver/prometheusreceiver/config_test.go b/receiver/prometheusreceiver/config_test.go index 0e46de7ea5d..d4f170fd203 100644 --- a/receiver/prometheusreceiver/config_test.go +++ b/receiver/prometheusreceiver/config_test.go @@ -125,3 +125,15 @@ func TestLoadConfigFailsOnUnknownPrometheusSection(t *testing.T) { assert.Error(t, err) assert.Nil(t, cfg) } + +// Renaming is not allowed +func TestLoadConfigFailsOnRenameDisallowed(t *testing.T) { + factories, err := componenttest.NopFactories() + assert.NoError(t, err) + + factory := NewFactory() + factories.Receivers[typeStr] = factory + cfg, err := configtest.LoadConfigAndValidate(path.Join(".", "testdata", "invalid-config-prometheus-relabel.yaml"), factories) + assert.Error(t, err) + assert.NotNil(t, cfg) +} diff --git a/receiver/prometheusreceiver/factory.go b/receiver/prometheusreceiver/factory.go index b2d0f40e893..98f5e9b68aa 100644 --- a/receiver/prometheusreceiver/factory.go +++ b/receiver/prometheusreceiver/factory.go @@ -33,7 +33,8 @@ const ( ) var ( - errNilScrapeConfig = errors.New("expecting a non-nil ScrapeConfig") + errNilScrapeConfig = errors.New("expecting a non-nil ScrapeConfig") + errRenamingDisallowed = errors.New("metric renaming using metric_relabel_configs is disallowed") ) // NewFactory creates a new Prometheus receiver factory. diff --git a/receiver/prometheusreceiver/testdata/invalid-config-prometheus-relabel.yaml b/receiver/prometheusreceiver/testdata/invalid-config-prometheus-relabel.yaml new file mode 100644 index 00000000000..90f40fa88e8 --- /dev/null +++ b/receiver/prometheusreceiver/testdata/invalid-config-prometheus-relabel.yaml @@ -0,0 +1,22 @@ +receivers: + prometheus: + config: + scrape_configs: + - job_name: rename + metric_relabel_configs: + - source_labels: [__name__] + regex: "foo_(.*)" + target_label: __name__ + +processors: + nop: + +exporters: + nop: + +service: + pipelines: + traces: + receivers: [prometheus] + processors: [nop] + exporters: [nop]