From 5336008dcd297715b986c48c7fa22ea99b8c83e0 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Fri, 14 Jun 2019 13:06:01 -0700 Subject: [PATCH] refactoring --- weed/server/filer_server.go | 24 ------------ weed/server/metrics.go | 43 +++++++++++++++++++-- weed/server/volume_server_handlers_read.go | 2 +- weed/server/volume_server_handlers_write.go | 4 +- 4 files changed, 43 insertions(+), 30 deletions(-) diff --git a/weed/server/filer_server.go b/weed/server/filer_server.go index 9cd425981..1670d70b3 100644 --- a/weed/server/filer_server.go +++ b/weed/server/filer_server.go @@ -3,11 +3,8 @@ package weed_server import ( "net/http" "os" - "time" "github.com/chrislusf/seaweedfs/weed/util" - "github.com/prometheus/client_golang/prometheus" - "github.com/prometheus/client_golang/prometheus/push" "google.golang.org/grpc" "github.com/chrislusf/seaweedfs/weed/filer2" @@ -93,24 +90,3 @@ func NewFilerServer(defaultMux, readonlyMux *http.ServeMux, option *FilerOption) return fs, nil } -func startPushingMetric(name string, gatherer *prometheus.Registry, addr string, intervalSeconds int) { - if intervalSeconds == 0 || addr == "" { - glog.V(0).Info("disable metrics reporting") - return - } - glog.V(0).Infof("push metrics to %s every %d seconds", addr, intervalSeconds) - go loopPushMetrics(name, gatherer, addr, intervalSeconds) -} - -func loopPushMetrics(name string, gatherer *prometheus.Registry, addr string, intervalSeconds int) { - - pusher := push.New(addr, name).Gatherer(gatherer) - - for { - err := pusher.Push() - if err != nil { - glog.V(0).Infof("could not push metrics to prometheus push gateway %s: %v", addr, err) - } - time.Sleep(time.Duration(intervalSeconds) * time.Second) - } -} diff --git a/weed/server/metrics.go b/weed/server/metrics.go index 0bee27d22..3f4be1a7d 100644 --- a/weed/server/metrics.go +++ b/weed/server/metrics.go @@ -1,6 +1,12 @@ package weed_server -import "github.com/prometheus/client_golang/prometheus" +import ( + "time" + + "github.com/chrislusf/seaweedfs/weed/glog" + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/push" +) var ( filerGather = prometheus.NewRegistry() @@ -31,7 +37,7 @@ var ( Help: "Counter of filer requests.", }, []string{"type"}) - volumeServerHistogram = prometheus.NewHistogramVec( + volumeServerRequestHistogram = prometheus.NewHistogramVec( prometheus.HistogramOpts{ Namespace: "SeaweedFS", Subsystem: "volumeServer", @@ -39,6 +45,15 @@ var ( Help: "Bucketed histogram of filer request processing time.", Buckets: prometheus.ExponentialBuckets(0.0001, 2, 24), }, []string{"type"}) + + volumeServerVolumeCounter = prometheus.NewGauge( + prometheus.GaugeOpts{ + Namespace: "SeaweedFS", + Subsystem: "volumeServer", + Name: "volumes", + Help: "Number of volumes.", + }) + ) func init() { @@ -47,6 +62,28 @@ func init() { filerGather.MustRegister(filerRequestHistogram) volumeServerGather.MustRegister(volumeServerRequestCounter) - volumeServerGather.MustRegister(volumeServerHistogram) + volumeServerGather.MustRegister(volumeServerRequestHistogram) } + +func startPushingMetric(name string, gatherer *prometheus.Registry, addr string, intervalSeconds int) { + if intervalSeconds == 0 || addr == "" { + glog.V(0).Info("disable metrics reporting") + return + } + glog.V(0).Infof("push metrics to %s every %d seconds", addr, intervalSeconds) + go loopPushMetrics(name, gatherer, addr, intervalSeconds) +} + +func loopPushMetrics(name string, gatherer *prometheus.Registry, addr string, intervalSeconds int) { + + pusher := push.New(addr, name).Gatherer(gatherer) + + for { + err := pusher.Push() + if err != nil { + glog.V(0).Infof("could not push metrics to prometheus push gateway %s: %v", addr, err) + } + time.Sleep(time.Duration(intervalSeconds) * time.Second) + } +} diff --git a/weed/server/volume_server_handlers_read.go b/weed/server/volume_server_handlers_read.go index 5e8bc7469..18c315257 100644 --- a/weed/server/volume_server_handlers_read.go +++ b/weed/server/volume_server_handlers_read.go @@ -29,7 +29,7 @@ func (vs *VolumeServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request) volumeServerRequestCounter.WithLabelValues("get").Inc() start := time.Now() - defer func() { volumeServerHistogram.WithLabelValues("get").Observe(time.Since(start).Seconds()) }() + defer func() { volumeServerRequestHistogram.WithLabelValues("get").Observe(time.Since(start).Seconds()) }() n := new(needle.Needle) vid, fid, filename, ext, _ := parseURLPath(r.URL.Path) diff --git a/weed/server/volume_server_handlers_write.go b/weed/server/volume_server_handlers_write.go index b53defd26..eb76e4414 100644 --- a/weed/server/volume_server_handlers_write.go +++ b/weed/server/volume_server_handlers_write.go @@ -18,7 +18,7 @@ func (vs *VolumeServer) PostHandler(w http.ResponseWriter, r *http.Request) { volumeServerRequestCounter.WithLabelValues("post").Inc() start := time.Now() - defer func() { volumeServerHistogram.WithLabelValues("post").Observe(time.Since(start).Seconds()) }() + defer func() { volumeServerRequestHistogram.WithLabelValues("post").Observe(time.Since(start).Seconds()) }() if e := r.ParseForm(); e != nil { glog.V(0).Infoln("form parse error:", e) @@ -68,7 +68,7 @@ func (vs *VolumeServer) DeleteHandler(w http.ResponseWriter, r *http.Request) { volumeServerRequestCounter.WithLabelValues("delete").Inc() start := time.Now() - defer func() { volumeServerHistogram.WithLabelValues("delete").Observe(time.Since(start).Seconds()) }() + defer func() { volumeServerRequestHistogram.WithLabelValues("delete").Observe(time.Since(start).Seconds()) }() n := new(needle.Needle) vid, fid, _, _, _ := parseURLPath(r.URL.Path)