From 8b43679ae38c11b60678d6c93965227f84919b0b Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Sat, 15 Jun 2019 12:21:44 -0700 Subject: [PATCH] refactoring --- weed/command/filer.go | 1 + weed/server/filer_server.go | 12 +++++++- weed/server/filer_server_handlers_read.go | 9 +++--- weed/server/filer_server_handlers_write.go | 9 +++--- weed/server/volume_server.go | 8 +++-- weed/server/volume_server_handlers_read.go | 5 +-- weed/server/volume_server_handlers_write.go | 9 +++--- weed/{server => stats}/metrics.go | 34 ++++++++++----------- 8 files changed, 53 insertions(+), 34 deletions(-) rename weed/{server => stats}/metrics.go (61%) diff --git a/weed/command/filer.go b/weed/command/filer.go index 83e9df20f..f20ac9714 100644 --- a/weed/command/filer.go +++ b/weed/command/filer.go @@ -116,6 +116,7 @@ func (fo *FilerOptions) startFiler() { DisableHttp: *fo.disableHttp, MetricsAddress: *fo.metricsAddress, MetricsIntervalSec: *fo.metricsIntervalSec, + Port: *fo.port, }) if nfs_err != nil { glog.Fatalf("Filer startup error: %v", nfs_err) diff --git a/weed/server/filer_server.go b/weed/server/filer_server.go index 1670d70b3..fa14159dc 100644 --- a/weed/server/filer_server.go +++ b/weed/server/filer_server.go @@ -1,9 +1,11 @@ package weed_server import ( + "fmt" "net/http" "os" + "github.com/chrislusf/seaweedfs/weed/stats" "github.com/chrislusf/seaweedfs/weed/util" "google.golang.org/grpc" @@ -38,6 +40,7 @@ type FilerOption struct { DisableHttp bool MetricsAddress string MetricsIntervalSec int + Port int } type FilerServer struct { @@ -85,8 +88,15 @@ func NewFilerServer(defaultMux, readonlyMux *http.ServeMux, option *FilerOption) readonlyMux.HandleFunc("/", fs.readonlyFilerHandler) } - startPushingMetric("filer", filerGather, option.MetricsAddress, option.MetricsIntervalSec) + stats.StartPushingMetric("filer", sourceName(option.Port), stats.FilerGather, option.MetricsAddress, option.MetricsIntervalSec) return fs, nil } +func sourceName(port int) string { + hostname, err := os.Hostname() + if err != nil { + return "unknown" + } + return fmt.Sprintf("%s_%d", hostname, port) +} diff --git a/weed/server/filer_server_handlers_read.go b/weed/server/filer_server_handlers_read.go index ca6269fd1..1d18bd75e 100644 --- a/weed/server/filer_server_handlers_read.go +++ b/weed/server/filer_server_handlers_read.go @@ -15,14 +15,15 @@ import ( "github.com/chrislusf/seaweedfs/weed/filer2" "github.com/chrislusf/seaweedfs/weed/glog" + "github.com/chrislusf/seaweedfs/weed/stats" "github.com/chrislusf/seaweedfs/weed/util" ) func (fs *FilerServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request, isGetMethod bool) { - filerRequestCounter.WithLabelValues("get").Inc() + stats.FilerRequestCounter.WithLabelValues("get").Inc() start := time.Now() - defer func() { filerRequestHistogram.WithLabelValues("get").Observe(time.Since(start).Seconds()) }() + defer func() { stats.FilerRequestHistogram.WithLabelValues("get").Observe(time.Since(start).Seconds()) }() path := r.URL.Path if strings.HasSuffix(path, "/") && len(path) > 1 { @@ -37,7 +38,7 @@ func (fs *FilerServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request, } glog.V(1).Infof("Not found %s: %v", path, err) - filerRequestCounter.WithLabelValues("read.notfound").Inc() + stats.FilerRequestCounter.WithLabelValues("read.notfound").Inc() w.WriteHeader(http.StatusNotFound) return } @@ -53,7 +54,7 @@ func (fs *FilerServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request, if len(entry.Chunks) == 0 { glog.V(1).Infof("no file chunks for %s, attr=%+v", path, entry.Attr) - filerRequestCounter.WithLabelValues("read.nocontent").Inc() + stats.FilerRequestCounter.WithLabelValues("read.nocontent").Inc() w.WriteHeader(http.StatusNoContent) return } diff --git a/weed/server/filer_server_handlers_write.go b/weed/server/filer_server_handlers_write.go index 214a77102..91996dd5e 100644 --- a/weed/server/filer_server_handlers_write.go +++ b/weed/server/filer_server_handlers_write.go @@ -20,6 +20,7 @@ import ( "github.com/chrislusf/seaweedfs/weed/operation" "github.com/chrislusf/seaweedfs/weed/pb/filer_pb" "github.com/chrislusf/seaweedfs/weed/security" + "github.com/chrislusf/seaweedfs/weed/stats" "github.com/chrislusf/seaweedfs/weed/util" ) @@ -70,9 +71,9 @@ func (fs *FilerServer) assignNewFileInfo(w http.ResponseWriter, r *http.Request, func (fs *FilerServer) PostHandler(w http.ResponseWriter, r *http.Request) { - filerRequestCounter.WithLabelValues("post").Inc() + stats.FilerRequestCounter.WithLabelValues("post").Inc() start := time.Now() - defer func() { filerRequestHistogram.WithLabelValues("post").Observe(time.Since(start).Seconds()) }() + defer func() { stats.FilerRequestHistogram.WithLabelValues("post").Observe(time.Since(start).Seconds()) }() ctx := context.Background() @@ -232,9 +233,9 @@ func (fs *FilerServer) PostHandler(w http.ResponseWriter, r *http.Request) { // curl -X DELETE http://localhost:8888/path/to?recursive=true func (fs *FilerServer) DeleteHandler(w http.ResponseWriter, r *http.Request) { - filerRequestCounter.WithLabelValues("delete").Inc() + stats.FilerRequestCounter.WithLabelValues("delete").Inc() start := time.Now() - defer func() { filerRequestHistogram.WithLabelValues("delete").Observe(time.Since(start).Seconds()) }() + defer func() { stats.FilerRequestHistogram.WithLabelValues("delete").Observe(time.Since(start).Seconds()) }() isRecursive := r.FormValue("recursive") == "true" diff --git a/weed/server/volume_server.go b/weed/server/volume_server.go index 454383d0c..4f21234dd 100644 --- a/weed/server/volume_server.go +++ b/weed/server/volume_server.go @@ -1,9 +1,12 @@ package weed_server import ( - "google.golang.org/grpc" + "fmt" "net/http" + "github.com/chrislusf/seaweedfs/weed/stats" + "google.golang.org/grpc" + "github.com/chrislusf/seaweedfs/weed/glog" "github.com/chrislusf/seaweedfs/weed/security" "github.com/chrislusf/seaweedfs/weed/storage" @@ -84,7 +87,8 @@ func NewVolumeServer(adminMux, publicMux *http.ServeMux, ip string, } go vs.heartbeat() - startPushingMetric("volumeServer", volumeServerGather, metricsAddress, metricsIntervalSec) + hostAddress := fmt.Sprintf("%s:%d", ip, port) + stats.StartPushingMetric("volumeServer", hostAddress, stats.VolumeServerGather, metricsAddress, metricsIntervalSec) return vs } diff --git a/weed/server/volume_server_handlers_read.go b/weed/server/volume_server_handlers_read.go index 18c315257..f30ffefaf 100644 --- a/weed/server/volume_server_handlers_read.go +++ b/weed/server/volume_server_handlers_read.go @@ -19,6 +19,7 @@ import ( "github.com/chrislusf/seaweedfs/weed/glog" "github.com/chrislusf/seaweedfs/weed/images" "github.com/chrislusf/seaweedfs/weed/operation" + "github.com/chrislusf/seaweedfs/weed/stats" "github.com/chrislusf/seaweedfs/weed/storage/needle" "github.com/chrislusf/seaweedfs/weed/util" ) @@ -27,9 +28,9 @@ var fileNameEscaper = strings.NewReplacer("\\", "\\\\", "\"", "\\\"") func (vs *VolumeServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request) { - volumeServerRequestCounter.WithLabelValues("get").Inc() + stats.VolumeServerRequestCounter.WithLabelValues("get").Inc() start := time.Now() - defer func() { volumeServerRequestHistogram.WithLabelValues("get").Observe(time.Since(start).Seconds()) }() + defer func() { stats.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 eb76e4414..0a4c93dac 100644 --- a/weed/server/volume_server_handlers_write.go +++ b/weed/server/volume_server_handlers_write.go @@ -10,15 +10,16 @@ import ( "github.com/chrislusf/seaweedfs/weed/glog" "github.com/chrislusf/seaweedfs/weed/operation" + "github.com/chrislusf/seaweedfs/weed/stats" "github.com/chrislusf/seaweedfs/weed/storage/needle" "github.com/chrislusf/seaweedfs/weed/topology" ) func (vs *VolumeServer) PostHandler(w http.ResponseWriter, r *http.Request) { - volumeServerRequestCounter.WithLabelValues("post").Inc() + stats.VolumeServerRequestCounter.WithLabelValues("post").Inc() start := time.Now() - defer func() { volumeServerRequestHistogram.WithLabelValues("post").Observe(time.Since(start).Seconds()) }() + defer func() { stats.VolumeServerRequestHistogram.WithLabelValues("post").Observe(time.Since(start).Seconds()) }() if e := r.ParseForm(); e != nil { glog.V(0).Infoln("form parse error:", e) @@ -66,9 +67,9 @@ func (vs *VolumeServer) PostHandler(w http.ResponseWriter, r *http.Request) { func (vs *VolumeServer) DeleteHandler(w http.ResponseWriter, r *http.Request) { - volumeServerRequestCounter.WithLabelValues("delete").Inc() + stats.VolumeServerRequestCounter.WithLabelValues("delete").Inc() start := time.Now() - defer func() { volumeServerRequestHistogram.WithLabelValues("delete").Observe(time.Since(start).Seconds()) }() + defer func() { stats.VolumeServerRequestHistogram.WithLabelValues("delete").Observe(time.Since(start).Seconds()) }() n := new(needle.Needle) vid, fid, _, _, _ := parseURLPath(r.URL.Path) diff --git a/weed/server/metrics.go b/weed/stats/metrics.go similarity index 61% rename from weed/server/metrics.go rename to weed/stats/metrics.go index 3f4be1a7d..46a2a43ec 100644 --- a/weed/server/metrics.go +++ b/weed/stats/metrics.go @@ -1,4 +1,4 @@ -package weed_server +package stats import ( "time" @@ -9,10 +9,10 @@ import ( ) var ( - filerGather = prometheus.NewRegistry() - volumeServerGather = prometheus.NewRegistry() + FilerGather = prometheus.NewRegistry() + VolumeServerGather = prometheus.NewRegistry() - filerRequestCounter = prometheus.NewCounterVec( + FilerRequestCounter = prometheus.NewCounterVec( prometheus.CounterOpts{ Namespace: "SeaweedFS", Subsystem: "filer", @@ -20,7 +20,7 @@ var ( Help: "Counter of filer requests.", }, []string{"type"}) - filerRequestHistogram = prometheus.NewHistogramVec( + FilerRequestHistogram = prometheus.NewHistogramVec( prometheus.HistogramOpts{ Namespace: "SeaweedFS", Subsystem: "filer", @@ -29,7 +29,7 @@ var ( Buckets: prometheus.ExponentialBuckets(0.0001, 2, 24), }, []string{"type"}) - volumeServerRequestCounter = prometheus.NewCounterVec( + VolumeServerRequestCounter = prometheus.NewCounterVec( prometheus.CounterOpts{ Namespace: "SeaweedFS", Subsystem: "volumeServer", @@ -37,7 +37,7 @@ var ( Help: "Counter of filer requests.", }, []string{"type"}) - volumeServerRequestHistogram = prometheus.NewHistogramVec( + VolumeServerRequestHistogram = prometheus.NewHistogramVec( prometheus.HistogramOpts{ Namespace: "SeaweedFS", Subsystem: "volumeServer", @@ -46,38 +46,38 @@ var ( Buckets: prometheus.ExponentialBuckets(0.0001, 2, 24), }, []string{"type"}) - volumeServerVolumeCounter = prometheus.NewGauge( + VolumeServerVolumeCounter = prometheus.NewGauge( prometheus.GaugeOpts{ Namespace: "SeaweedFS", Subsystem: "volumeServer", Name: "volumes", Help: "Number of volumes.", }) - ) func init() { - filerGather.MustRegister(filerRequestCounter) - filerGather.MustRegister(filerRequestHistogram) + FilerGather.MustRegister(FilerRequestCounter) + FilerGather.MustRegister(FilerRequestHistogram) - volumeServerGather.MustRegister(volumeServerRequestCounter) - volumeServerGather.MustRegister(volumeServerRequestHistogram) + VolumeServerGather.MustRegister(VolumeServerRequestCounter) + VolumeServerGather.MustRegister(VolumeServerRequestHistogram) + VolumeServerGather.MustRegister(VolumeServerVolumeCounter) } -func startPushingMetric(name string, gatherer *prometheus.Registry, addr string, intervalSeconds int) { +func StartPushingMetric(name, instance 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) + go loopPushMetrics(name, instance, gatherer, addr, intervalSeconds) } -func loopPushMetrics(name string, gatherer *prometheus.Registry, addr string, intervalSeconds int) { +func loopPushMetrics(name, instance string, gatherer *prometheus.Registry, addr string, intervalSeconds int) { - pusher := push.New(addr, name).Gatherer(gatherer) + pusher := push.New(addr, name).Gatherer(gatherer).Grouping("instance", instance) for { err := pusher.Push()