From b0c181dc0db9c6ba041224d43cc90e7c71bd1927 Mon Sep 17 00:00:00 2001 From: Fabian Reinartz Date: Thu, 14 May 2015 13:32:11 +0200 Subject: [PATCH] Add Consul SD configuration. --- config/config.go | 42 ++++++++++++++++++++++++++++++++++++++ retrieval/targetmanager.go | 3 +++ 2 files changed, 45 insertions(+) diff --git a/config/config.go b/config/config.go index 5da03e2de4..bb8e752217 100644 --- a/config/config.go +++ b/config/config.go @@ -78,6 +78,12 @@ var ( DefaultFileSDConfig = DefaultedFileSDConfig{ RefreshInterval: Duration(30 * time.Second), } + + // The default Consul SD configuration. + DefaultConsulSDConfig = DefaultedConsulSDConfig{ + TagSeparator: ",", + Scheme: "http", + } ) // Config is the top-level configuration for Prometheus's config files. @@ -200,6 +206,8 @@ type DefaultedScrapeConfig struct { DNSSDConfigs []*DNSSDConfig `yaml:"dns_sd_configs,omitempty"` // List of file service discovery configurations. FileSDConfigs []*FileSDConfig `yaml:"file_sd_configs,omitempty"` + // List of Consul service discovery configurations. + ConsulSDConfigs []*ConsulSDConfig `yaml:"consul_sd_configs,omitempty"` // List of relabel configurations. RelabelConfigs []*RelabelConfig `yaml:"relabel_configs,omitempty"` } @@ -340,6 +348,40 @@ type DefaultedFileSDConfig struct { RefreshInterval Duration `yaml:"refresh_interval,omitempty"` } +// ConsulSDConfig is the configuration for Consul service discovery. +type ConsulSDConfig struct { + // DefaultedConsulSDConfig contains the actual fields for ConsulSDConfig. + DefaultedConsulSDConfig `yaml:",inline"` +} + +// UnmarshalYAML implements the yaml.Unmarshaller interface. +func (c *ConsulSDConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { + c.DefaultedConsulSDConfig = DefaultConsulSDConfig + err := unmarshal(&c.DefaultedConsulSDConfig) + if err != nil { + return err + } + if strings.TrimSpace(c.Server) == "" { + return fmt.Errorf("Consul SD configuration requires a server address") + } + if len(c.Services) == 0 { + return fmt.Errorf("Consul SD configuration requires at least one service name") + } + return nil +} + +// DefaultedConsulSDConfig is a proxy type for ConsulSDConfig. +type DefaultedConsulSDConfig struct { + Server string `yaml:"server"` + Token string `yaml:"token"` + Datacenter string `yaml:"datacenter"` + TagSeparator string `yaml:"tag_separator"` + Scheme string `yaml:"scheme"` + Username string `yaml:"username"` + Password string `yaml:"password"` + Services []string `yaml:"services"` +} + // RelabelAction is the action to be performed on relabeling. type RelabelAction string diff --git a/retrieval/targetmanager.go b/retrieval/targetmanager.go index 7763a5604d..5ddc2892f5 100644 --- a/retrieval/targetmanager.go +++ b/retrieval/targetmanager.go @@ -361,6 +361,9 @@ func ProvidersFromConfig(cfg *config.ScrapeConfig) []TargetProvider { for _, c := range cfg.FileSDConfigs { providers = append(providers, discovery.NewFileDiscovery(c)) } + for _, c := range cfg.ConsulSDConfigs { + providers = append(providers, discovery.NewConsulDiscovery(c)) + } if len(cfg.TargetGroups) > 0 { providers = append(providers, NewStaticProvider(cfg.TargetGroups)) }