diff options
| author | yourchanges <yourchanges@gmail.com> | 2020-07-10 09:44:32 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-07-10 09:44:32 +0800 |
| commit | e67096656b0fcdc313c7d8983b6ce36a54d794a3 (patch) | |
| tree | 4d6cfd722cf6e19b5aa8253e477ddc596ea5e193 /weed/stats/metrics.go | |
| parent | 2b3cef7780a5e91d2072a33411926f9b30c88ee2 (diff) | |
| parent | 1b680c06c1de27e6a3899c089ec354a9eb08ea44 (diff) | |
| download | seaweedfs-e67096656b0fcdc313c7d8983b6ce36a54d794a3.tar.xz seaweedfs-e67096656b0fcdc313c7d8983b6ce36a54d794a3.zip | |
Merge pull request #1 from chrislusf/master
update
Diffstat (limited to 'weed/stats/metrics.go')
| -rw-r--r-- | weed/stats/metrics.go | 147 |
1 files changed, 147 insertions, 0 deletions
diff --git a/weed/stats/metrics.go b/weed/stats/metrics.go new file mode 100644 index 000000000..7ff09a388 --- /dev/null +++ b/weed/stats/metrics.go @@ -0,0 +1,147 @@ +package stats + +import ( + "fmt" + "os" + "strings" + "time" + + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/push" + + "github.com/chrislusf/seaweedfs/weed/glog" +) + +var ( + FilerGather = prometheus.NewRegistry() + VolumeServerGather = prometheus.NewRegistry() + + FilerRequestCounter = prometheus.NewCounterVec( + prometheus.CounterOpts{ + Namespace: "SeaweedFS", + Subsystem: "filer", + Name: "request_total", + Help: "Counter of filer requests.", + }, []string{"type"}) + + FilerRequestHistogram = prometheus.NewHistogramVec( + prometheus.HistogramOpts{ + Namespace: "SeaweedFS", + Subsystem: "filer", + Name: "request_seconds", + Help: "Bucketed histogram of filer request processing time.", + Buckets: prometheus.ExponentialBuckets(0.0001, 2, 24), + }, []string{"type"}) + + FilerStoreCounter = prometheus.NewCounterVec( + prometheus.CounterOpts{ + Namespace: "SeaweedFS", + Subsystem: "filerStore", + Name: "request_total", + Help: "Counter of filer store requests.", + }, []string{"store", "type"}) + + FilerStoreHistogram = prometheus.NewHistogramVec( + prometheus.HistogramOpts{ + Namespace: "SeaweedFS", + Subsystem: "filerStore", + Name: "request_seconds", + Help: "Bucketed histogram of filer store request processing time.", + Buckets: prometheus.ExponentialBuckets(0.0001, 2, 24), + }, []string{"store", "type"}) + + VolumeServerRequestCounter = prometheus.NewCounterVec( + prometheus.CounterOpts{ + Namespace: "SeaweedFS", + Subsystem: "volumeServer", + Name: "request_total", + Help: "Counter of volume server requests.", + }, []string{"type"}) + + VolumeServerRequestHistogram = prometheus.NewHistogramVec( + prometheus.HistogramOpts{ + Namespace: "SeaweedFS", + Subsystem: "volumeServer", + Name: "request_seconds", + Help: "Bucketed histogram of volume server request processing time.", + Buckets: prometheus.ExponentialBuckets(0.0001, 2, 24), + }, []string{"type"}) + + VolumeServerVolumeCounter = prometheus.NewGaugeVec( + prometheus.GaugeOpts{ + Namespace: "SeaweedFS", + Subsystem: "volumeServer", + Name: "volumes", + Help: "Number of volumes or shards.", + }, []string{"collection", "type"}) + + VolumeServerMaxVolumeCounter = prometheus.NewGauge( + prometheus.GaugeOpts{ + Namespace: "SeaweedFS", + Subsystem: "volumeServer", + Name: "max_volumes", + Help: "Maximum number of volumes.", + }) + + VolumeServerDiskSizeGauge = prometheus.NewGaugeVec( + prometheus.GaugeOpts{ + Namespace: "SeaweedFS", + Subsystem: "volumeServer", + Name: "total_disk_size", + Help: "Actual disk size used by volumes.", + }, []string{"collection", "type"}) +) + +func init() { + + FilerGather.MustRegister(FilerRequestCounter) + FilerGather.MustRegister(FilerRequestHistogram) + FilerGather.MustRegister(FilerStoreCounter) + FilerGather.MustRegister(FilerStoreHistogram) + FilerGather.MustRegister(prometheus.NewGoCollector()) + + VolumeServerGather.MustRegister(VolumeServerRequestCounter) + VolumeServerGather.MustRegister(VolumeServerRequestHistogram) + VolumeServerGather.MustRegister(VolumeServerVolumeCounter) + VolumeServerGather.MustRegister(VolumeServerMaxVolumeCounter) + VolumeServerGather.MustRegister(VolumeServerDiskSizeGauge) + +} + +func LoopPushingMetric(name, instance string, gatherer *prometheus.Registry, fnGetMetricsDest func() (addr string, intervalSeconds int)) { + + if fnGetMetricsDest == nil { + return + } + + addr, intervalSeconds := fnGetMetricsDest() + pusher := push.New(addr, name).Gatherer(gatherer).Grouping("instance", instance) + currentAddr := addr + + for { + if currentAddr != "" { + err := pusher.Push() + 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) + } + } + if intervalSeconds <= 0 { + intervalSeconds = 15 + } + 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 + } + + } +} + +func SourceName(port uint32) string { + hostname, err := os.Hostname() + if err != nil { + return "unknown" + } + return fmt.Sprintf("%s:%d", hostname, port) +} |
