Skip to content

Commit

Permalink
Disallow renaming metrics using the prometheus receiver (#3410)
Browse files Browse the repository at this point in the history
* disallow renaming metrics using the prometheus receiver

* add TODO for removing the validation
  • Loading branch information
dashpole authored Jun 15, 2021
1 parent 0d94201 commit 05f16c5
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 💡

Expand Down
14 changes: 12 additions & 2 deletions receiver/prometheusreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
12 changes: 12 additions & 0 deletions receiver/prometheusreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
3 changes: 2 additions & 1 deletion receiver/prometheusreceiver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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]

0 comments on commit 05f16c5

Please sign in to comment.