Do not panic in Consul SD creation

This commit is contained in:
Fabian Reinartz 2015-11-30 18:41:48 +01:00
parent 953b3bf849
commit 29a69eecb8
3 changed files with 45 additions and 41 deletions

View file

@ -74,7 +74,7 @@ type consulService struct {
}
// NewConsulDiscovery returns a new ConsulDiscovery for the given config.
func NewConsulDiscovery(conf *config.ConsulSDConfig) *ConsulDiscovery {
func NewConsulDiscovery(conf *config.ConsulSDConfig) (*ConsulDiscovery, error) {
clientConf := &consul.Config{
Address: conf.Server,
Scheme: conf.Scheme,
@ -87,8 +87,7 @@ func NewConsulDiscovery(conf *config.ConsulSDConfig) *ConsulDiscovery {
}
client, err := consul.NewClient(clientConf)
if err != nil {
// NewClient always returns a nil error.
panic(fmt.Errorf("discovery.NewConsulDiscovery: %s", err))
return nil, err
}
cd := &ConsulDiscovery{
client: client,
@ -102,7 +101,7 @@ func NewConsulDiscovery(conf *config.ConsulSDConfig) *ConsulDiscovery {
if clientConf.Datacenter == "" {
info, err := client.Agent().Self()
if err != nil {
panic(fmt.Errorf("discovery.NewConsulDiscovery: %s", err))
return nil, err
}
cd.clientDatacenter = info["Config"]["Datacenter"].(string)
} else {
@ -111,7 +110,7 @@ func NewConsulDiscovery(conf *config.ConsulSDConfig) *ConsulDiscovery {
for _, name := range conf.Services {
cd.scrapedServices[name] = struct{}{}
}
return cd
return cd, nil
}
// Sources implements the TargetProvider interface.

View file

@ -413,7 +413,12 @@ func providersFromConfig(cfg *config.ScrapeConfig) []TargetProvider {
app("file", i, discovery.NewFileDiscovery(c))
}
for i, c := range cfg.ConsulSDConfigs {
app("consul", i, discovery.NewConsulDiscovery(c))
k, err := discovery.NewConsulDiscovery(c)
if err != nil {
log.Errorf("Cannot create Consul discovery: %s", err)
continue
}
app("consul", i, k)
}
for i, c := range cfg.MarathonSDConfigs {
app("marathon", i, discovery.NewMarathonDiscovery(c))

File diff suppressed because one or more lines are too long