metrics_address should be optional parameter (#2087)

This commit is contained in:
wasaga 2021-04-13 15:56:35 -04:00 committed by GitHub
parent 1dcccf2b56
commit c12c0aab49
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 16 deletions

View file

@ -1,7 +1,6 @@
package registry package registry
import ( import (
"errors"
"time" "time"
) )
@ -15,9 +14,3 @@ const (
// path metrics are available at // path metrics are available at
defaultMetricsPath = "/metrics" defaultMetricsPath = "/metrics"
) )
var (
errNoMetricsAddr = errors.New("no metrics address provided")
errNoMetricsPort = errors.New("no metrics port provided")
errNoMetricsHost = errors.New("no metrics host provided")
)

View file

@ -30,8 +30,7 @@ func (r *Reporter) OnConfigChange(cfg *config.Config) {
services, err := getReportedServices(cfg) services, err := getReportedServices(cfg)
if err != nil { if err != nil {
log.Error().Err(err).Msg("service registry reporter") log.Warn().Err(err).Msg("metrics announce to service registry is disabled")
return
} }
sharedKey, err := base64.StdEncoding.DecodeString(cfg.Options.SharedKey) sharedKey, err := base64.StdEncoding.DecodeString(cfg.Options.SharedKey)
@ -63,9 +62,11 @@ func (r *Reporter) OnConfigChange(cfg *config.Config) {
return return
} }
ctx, cancel := context.WithCancel(context.TODO()) if len(services) > 0 {
go runReporter(ctx, pb.NewRegistryClient(registryConn), services) ctx, cancel := context.WithCancel(context.TODO())
r.cancel = cancel go runReporter(ctx, pb.NewRegistryClient(registryConn), services)
r.cancel = cancel
}
} }
func getReportedServices(cfg *config.Config) ([]*pb.Service, error) { func getReportedServices(cfg *config.Config) ([]*pb.Service, error) {
@ -103,20 +104,20 @@ func metricsURL(o config.Options) (*url.URL, error) {
} }
if o.MetricsAddr == "" { if o.MetricsAddr == "" {
return nil, errNoMetricsAddr return nil, fmt.Errorf("no metrics address provided")
} }
host, port, err := net.SplitHostPort(o.MetricsAddr) host, port, err := net.SplitHostPort(o.MetricsAddr)
if err != nil { if err != nil {
return nil, fmt.Errorf("invalid metrics address: %w", err) return nil, fmt.Errorf("invalid metrics address %q: %w", o.MetricsAddr, err)
} }
if port == "" { if port == "" {
return nil, errNoMetricsPort return nil, fmt.Errorf("invalid metrics value %q: port is required", o.MetricsAddr)
} }
if host == "" { if host == "" {
return nil, errNoMetricsHost return nil, fmt.Errorf("invalid metrics value %q: either host or IP address is required", o.MetricsAddr)
} }
return &u, nil return &u, nil