This commit is contained in:
Denis Zheleztsov 2017-01-13 13:20:11 +03:00
parent 1a6eac67b2
commit 46df5bc0bf
2 changed files with 62 additions and 0 deletions

View File

@ -133,6 +133,20 @@ func (s ServerConf) ServeHTTP(z ZooNode, fqdn string) {
w.Write(state)
})
// Get metrics
http.HandleFunc("/metrics", func(w http.ResponseWriter, r *http.Request) {
m, err := GetMetrics(z)
if err != nil {
w.WriteHeader(500)
return
}
metrics, err := json.MarshalIndent(m, "", " ")
if err != nil {
log.Fatal(err)
}
w.Write(metrics)
})
wr := wrr{
fqdn,
z,

48
src/rbmd/metrics.go Normal file
View File

@ -0,0 +1,48 @@
package rbmd
import (
"encoding/json"
"runtime"
"strings"
)
//Metrics metrics statistic
type Metrics struct {
Goroutines int `json:"goroutines"`
Nodes int `json:"nodes"`
MountsTotal int `json:"mountstotal"`
Cgocall int64 `json:"cgocall"`
}
// GetMetrics ...
func GetMetrics(z ZooNode) (Metrics, error) {
var q Quorum
var m Metrics
curQuorum, _, err := z.Conn.Get(strings.Join([]string{z.Path, "log", "quorum"}, "/"))
if err != nil {
return m, err
}
err = json.Unmarshal(curQuorum, &q)
if err != nil {
return m, err
}
m.Nodes = len(q.Quorum)
m.Goroutines = runtime.NumGoroutine()
m.Cgocall = runtime.NumCgoCall()
m.MountsTotal = GetTotalMounts(q.Quorum)
return m, nil
}
// GetTotalMounts ...
func GetTotalMounts(n []Node) int {
mounts := 0
for _, node := range n {
mounts = mounts + len(node.Mounts)
}
return mounts
}