Skip to content

Commit caf716e

Browse files
authored
Make prometheus reciever config loading strict (#697)
Fixes #583 Using strict umarshalling ensures eliminates common mistakes such as adding a key intended for the prometheus reciever itself into the prometheus subconfiguration.
1 parent c09da73 commit caf716e

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

receiver/prometheusreceiver/config_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,20 @@ func TestLoadConfigFailsOnUnknownSection(t *testing.T) {
9393
require.Error(t, err)
9494
require.Nil(t, cfg)
9595
}
96+
97+
// As one of the config parameters is consuming prometheus
98+
// configuration as a subkey, ensure that invalid configuration
99+
// within the subkey will also raise an error.
100+
func TestLoadConfigFailsOnUnknownPrometheusSection(t *testing.T) {
101+
factories, err := config.ExampleComponents()
102+
assert.Nil(t, err)
103+
104+
factory := &Factory{}
105+
factories.Receivers[typeStr] = factory
106+
cfg, err := config.LoadConfigFile(
107+
t,
108+
path.Join(".", "testdata", "invalid-config-prometheus-section.yaml"), factories)
109+
110+
require.Error(t, err)
111+
require.Nil(t, cfg)
112+
}

receiver/prometheusreceiver/factory.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func CustomUnmarshalerFunc(v *viper.Viper, viperKey string, sourceViperSection *
8989
out = []byte(os.ExpandEnv(string(out)))
9090
config := intoCfg.(*Config)
9191

92-
err = yaml.Unmarshal(out, &config.PrometheusConfig)
92+
err = yaml.UnmarshalStrict(out, &config.PrometheusConfig)
9393
if err != nil {
9494
return fmt.Errorf("prometheus receiver failed to unmarshal yaml to prometheus config: %s", err)
9595
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
receivers:
2+
prometheus:
3+
config:
4+
use_start_time_metric: true
5+
scrape_configs:
6+
- job_name: 'demo'
7+
scrape_interval: 5s
8+
9+
processors:
10+
exampleprocessor:
11+
12+
exporters:
13+
exampleexporter:
14+
15+
service:
16+
pipelines:
17+
traces:
18+
receivers: [prometheus]
19+
processors: [exampleprocessor]
20+
exporters: [exampleexporter]

0 commit comments

Comments
 (0)