Skip to content

Commit 4641b6e

Browse files
committed
disallow renaming metrics using the prometheus receiver
1 parent 3ac0f32 commit 4641b6e

File tree

5 files changed

+48
-3
lines changed

5 files changed

+48
-3
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
- Updated configgrpc `ToDialOptions` and confighttp `ToClient` apis to take extensions configuration map (#3340)
3030
- Remove `GenerateSequentialTraceID` and `GenerateSequentialSpanIDin` functions in testbed (#3390)
3131
- Change "grpc" to "GRPC" in configauth function/type names (#3285)
32+
- Disallow renaming metrics using metric relabel configs (#3410)
3233

3334
## 💡 Enhancements 💡
3435

receiver/prometheusreceiver/config.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,17 @@ var _ config.CustomUnmarshable = (*Config)(nil)
5151

5252
// Validate checks the receiver configuration is valid
5353
func (cfg *Config) Validate() error {
54-
if cfg.PrometheusConfig != nil && len(cfg.PrometheusConfig.ScrapeConfigs) == 0 {
55-
return errNilScrapeConfig
54+
if cfg.PrometheusConfig != nil {
55+
if len(cfg.PrometheusConfig.ScrapeConfigs) == 0 {
56+
return errNilScrapeConfig
57+
}
58+
for _, sc := range cfg.PrometheusConfig.ScrapeConfigs {
59+
for _, rc := range sc.MetricRelabelConfigs {
60+
if rc.TargetLabel == "__name__" {
61+
return fmt.Errorf("error validating scrapeconfig for job %v: %w", sc.JobName, errRenamingDisallowed)
62+
}
63+
}
64+
}
5665
}
5766
return nil
5867
}

receiver/prometheusreceiver/config_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,15 @@ func TestLoadConfigFailsOnUnknownPrometheusSection(t *testing.T) {
125125
assert.Error(t, err)
126126
assert.Nil(t, cfg)
127127
}
128+
129+
// Renaming is not allowed
130+
func TestLoadConfigFailsOnRenameDisallowed(t *testing.T) {
131+
factories, err := componenttest.NopFactories()
132+
assert.NoError(t, err)
133+
134+
factory := NewFactory()
135+
factories.Receivers[typeStr] = factory
136+
cfg, err := configtest.LoadConfigAndValidate(path.Join(".", "testdata", "invalid-config-prometheus-relabel.yaml"), factories)
137+
assert.Error(t, err)
138+
assert.NotNil(t, cfg)
139+
}

receiver/prometheusreceiver/factory.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ const (
3333
)
3434

3535
var (
36-
errNilScrapeConfig = errors.New("expecting a non-nil ScrapeConfig")
36+
errNilScrapeConfig = errors.New("expecting a non-nil ScrapeConfig")
37+
errRenamingDisallowed = errors.New("metric renaming using metric_relabel_configs is disallowed")
3738
)
3839

3940
// NewFactory creates a new Prometheus receiver factory.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
receivers:
2+
prometheus:
3+
config:
4+
scrape_configs:
5+
- job_name: rename
6+
metric_relabel_configs:
7+
- source_labels: [__name__]
8+
regex: "foo_(.*)"
9+
target_label: __name__
10+
11+
processors:
12+
nop:
13+
14+
exporters:
15+
nop:
16+
17+
service:
18+
pipelines:
19+
traces:
20+
receivers: [prometheus]
21+
processors: [nop]
22+
exporters: [nop]

0 commit comments

Comments
 (0)