Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disallow renaming metrics using the prometheus receiver #3410

Merged
merged 2 commits into from
Jun 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 {
dashpole marked this conversation as resolved.
Show resolved Hide resolved
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]