Skip to content

Commit ae42627

Browse files
authored
Validate the Prometheus configuration (#3589)
* Validate the Prometheus config Validate against nil configuration and configuration with no scrape_configs. Fixes #2705. * Allow initialization of recievers without any configuration
1 parent d4f068d commit ae42627

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

receiver/prometheusreceiver/config.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package prometheusreceiver
1616

1717
import (
18+
"errors"
1819
"fmt"
1920
"time"
2021

@@ -49,18 +50,20 @@ type Config struct {
4950
var _ config.Receiver = (*Config)(nil)
5051
var _ config.CustomUnmarshable = (*Config)(nil)
5152

52-
// Validate checks the receiver configuration is valid
53+
// Validate checks the receiver configuration is valid.
5354
func (cfg *Config) Validate() error {
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-
// TODO(#2297): Remove validation after renaming is fixed
62-
return fmt.Errorf("error validating scrapeconfig for job %v: %w", sc.JobName, errRenamingDisallowed)
63-
}
55+
if cfg.PrometheusConfig == nil {
56+
return nil // noop receiver
57+
}
58+
if len(cfg.PrometheusConfig.ScrapeConfigs) == 0 {
59+
return errors.New("no Prometheus scrape_configs")
60+
}
61+
62+
for _, sc := range cfg.PrometheusConfig.ScrapeConfigs {
63+
for _, rc := range sc.MetricRelabelConfigs {
64+
if rc.TargetLabel == "__name__" {
65+
// TODO(#2297): Remove validation after renaming is fixed
66+
return fmt.Errorf("error validating scrapeconfig for job %v: %w", sc.JobName, errRenamingDisallowed)
6467
}
6568
}
6669
}
@@ -94,5 +97,6 @@ func (cfg *Config) Unmarshal(componentParser *configparser.Parser) error {
9497
if err != nil {
9598
return fmt.Errorf("prometheus receiver failed to unmarshal yaml to prometheus config: %s", err)
9699
}
100+
97101
return nil
98102
}

receiver/prometheusreceiver/factory.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,7 @@ const (
3232
typeStr = "prometheus"
3333
)
3434

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

4037
// NewFactory creates a new Prometheus receiver factory.
4138
func NewFactory() component.ReceiverFactory {

0 commit comments

Comments
 (0)