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

View file

@ -413,7 +413,12 @@ func providersFromConfig(cfg *config.ScrapeConfig) []TargetProvider {
app("file", i, discovery.NewFileDiscovery(c)) app("file", i, discovery.NewFileDiscovery(c))
} }
for i, c := range cfg.ConsulSDConfigs { 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 { for i, c := range cfg.MarathonSDConfigs {
app("marathon", i, discovery.NewMarathonDiscovery(c)) app("marathon", i, discovery.NewMarathonDiscovery(c))

File diff suppressed because one or more lines are too long