Skip to content

Commit

Permalink
disallow renaming metrics using the prometheus receiver
Browse files Browse the repository at this point in the history
  • Loading branch information
dashpole committed Jun 9, 2021
1 parent 3ac0f32 commit 4641b6e
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,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
13 changes: 11 additions & 2 deletions receiver/prometheusreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,17 @@ 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__" {
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 4641b6e

Please sign in to comment.