simplify metrics configuration logic

This commit is contained in:
Chris Lu 2020-09-16 01:39:30 -07:00
parent be54eeb364
commit e861a6a3ab
3 changed files with 7 additions and 22 deletions

View file

@ -157,10 +157,7 @@ func maybeStartMetrics(fs *FilerServer, option *FilerOption) {
if metricsAddress == "" && metricsIntervalSec <= 0 { if metricsAddress == "" && metricsIntervalSec <= 0 {
return return
} }
go stats.LoopPushingMetric("filer", stats.SourceName(option.Port), stats.FilerGather, go stats.LoopPushingMetric("filer", stats.SourceName(option.Port), stats.FilerGather, metricsAddress, metricsIntervalSec)
func() (addr string, intervalSeconds int) {
return metricsAddress, metricsIntervalSec
})
} }
func readFilerConfiguration(grpcDialOption grpc.DialOption, masterAddress string) (metricsAddress string, metricsIntervalSec int, err error) { func readFilerConfiguration(grpcDialOption grpc.DialOption, masterAddress string) (metricsAddress string, metricsIntervalSec int, err error) {

View file

@ -98,10 +98,7 @@ func NewVolumeServer(adminMux, publicMux *http.ServeMux, ip string,
go vs.heartbeat() go vs.heartbeat()
hostAddress := fmt.Sprintf("%s:%d", ip, port) hostAddress := fmt.Sprintf("%s:%d", ip, port)
go stats.LoopPushingMetric("volumeServer", hostAddress, stats.VolumeServerGather, go stats.LoopPushingMetric("volumeServer", hostAddress, stats.VolumeServerGather, vs.MetricsAddress, vs.MetricsIntervalSec)
func() (addr string, intervalSeconds int) {
return vs.MetricsAddress, vs.MetricsIntervalSec
})
return vs return vs
} }

View file

@ -108,32 +108,23 @@ func init() {
} }
func LoopPushingMetric(name, instance string, gatherer *prometheus.Registry, fnGetMetricsDest func() (addr string, intervalSeconds int)) { func LoopPushingMetric(name, instance string, gatherer *prometheus.Registry, addr string, intervalSeconds int) {
if fnGetMetricsDest == nil { if addr == "" || intervalSeconds == 0 {
return return
} }
addr, intervalSeconds := fnGetMetricsDest()
pusher := push.New(addr, name).Gatherer(gatherer).Grouping("instance", instance) pusher := push.New(addr, name).Gatherer(gatherer).Grouping("instance", instance)
currentAddr := addr
for { for {
if currentAddr != "" {
err := pusher.Push() err := pusher.Push()
if err != nil && !strings.HasPrefix(err.Error(), "unexpected status code 200") { if err != nil && !strings.HasPrefix(err.Error(), "unexpected status code 200") {
glog.V(0).Infof("could not push metrics to prometheus push gateway %s: %v", addr, err) glog.V(0).Infof("could not push metrics to prometheus push gateway %s: %v", addr, err)
} }
}
if intervalSeconds <= 0 { if intervalSeconds <= 0 {
intervalSeconds = 15 intervalSeconds = 15
} }
time.Sleep(time.Duration(intervalSeconds) * time.Second) time.Sleep(time.Duration(intervalSeconds) * time.Second)
addr, intervalSeconds = fnGetMetricsDest()
if currentAddr != addr {
pusher = push.New(addr, name).Gatherer(gatherer).Grouping("instance", instance)
currentAddr = addr
}
} }
} }