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

Validate the Prometheus configuration #3589

Merged
merged 2 commits into from
Jul 13, 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
26 changes: 15 additions & 11 deletions receiver/prometheusreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package prometheusreceiver

import (
"errors"
"fmt"
"time"

Expand Down Expand Up @@ -49,18 +50,20 @@ type Config struct {
var _ config.Receiver = (*Config)(nil)
var _ config.CustomUnmarshable = (*Config)(nil)

// Validate checks the receiver configuration is valid
// Validate checks the receiver configuration is valid.
func (cfg *Config) Validate() error {
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__" {
// TODO(#2297): Remove validation after renaming is fixed
return fmt.Errorf("error validating scrapeconfig for job %v: %w", sc.JobName, errRenamingDisallowed)
}
if cfg.PrometheusConfig == nil {
return nil // noop receiver
}
if len(cfg.PrometheusConfig.ScrapeConfigs) == 0 {
return errors.New("no Prometheus scrape_configs")
}

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)
}
}
}
Expand Down Expand Up @@ -94,5 +97,6 @@ func (cfg *Config) Unmarshal(componentParser *configparser.Parser) error {
if err != nil {
return fmt.Errorf("prometheus receiver failed to unmarshal yaml to prometheus config: %s", err)
}

return nil
}
5 changes: 1 addition & 4 deletions receiver/prometheusreceiver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ const (
typeStr = "prometheus"
)

var (
errNilScrapeConfig = errors.New("expecting a non-nil ScrapeConfig")
errRenamingDisallowed = errors.New("metric renaming using metric_relabel_configs is disallowed")
)
var errRenamingDisallowed = errors.New("metric renaming using metric_relabel_configs is disallowed")

// NewFactory creates a new Prometheus receiver factory.
func NewFactory() component.ReceiverFactory {
Expand Down