refactoring

This commit is contained in:
Chris Lu 2019-06-14 13:06:01 -07:00
parent d67189921f
commit 5336008dcd
4 changed files with 43 additions and 30 deletions

View file

@ -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)
}
}

View file

@ -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)
}
}

View file

@ -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)

View file

@ -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)