-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathstat.go
73 lines (65 loc) · 1.94 KB
/
stat.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package eredis
import (
"encoding/json"
"net/http"
"sync"
"time"
"github.com/go-redis/redis/v8"
"github.com/gotomicro/ego/core/elog"
"github.com/gotomicro/ego/core/emetric"
"github.com/gotomicro/ego/server/egovernor"
)
var instances = sync.Map{}
type storeRedis struct {
ClientCluster *redis.ClusterClient
ClientStub *redis.Client
}
func init() {
egovernor.HandleFunc("/debug/redis/stats", func(w http.ResponseWriter, r *http.Request) {
if err := json.NewEncoder(w).Encode(stats()); err != nil {
elog.Error("encode stats fail", elog.FieldErr(err))
}
})
go monitor()
}
func monitor() {
for {
instances.Range(func(key, val interface{}) bool {
name := key.(string)
obj := val.(*storeRedis)
var poolStats *redis.PoolStats
if obj.ClientStub != nil {
poolStats = obj.ClientStub.PoolStats()
}
if obj.ClientCluster != nil {
poolStats = obj.ClientCluster.PoolStats()
}
if poolStats != nil {
emetric.ClientStatsGauge.Set(float64(poolStats.Hits), emetric.TypeRedis, name, "hits")
emetric.ClientStatsGauge.Set(float64(poolStats.Misses), emetric.TypeRedis, name, "misses")
emetric.ClientStatsGauge.Set(float64(poolStats.Timeouts), emetric.TypeRedis, name, "timeouts")
emetric.ClientStatsGauge.Set(float64(poolStats.TotalConns), emetric.TypeRedis, name, "total_conns")
emetric.ClientStatsGauge.Set(float64(poolStats.IdleConns), emetric.TypeRedis, name, "idle_conns")
emetric.ClientStatsGauge.Set(float64(poolStats.StaleConns), emetric.TypeRedis, name, "stale_conns")
}
return true
})
time.Sleep(time.Second * 10)
}
}
// stats
func stats() (stats map[string]interface{}) {
stats = make(map[string]interface{})
instances.Range(func(key, val interface{}) bool {
name := key.(string)
obj := val.(*storeRedis)
if obj.ClientStub != nil {
stats[name] = obj.ClientStub.PoolStats()
}
if obj.ClientCluster != nil {
stats[name] = obj.ClientCluster.PoolStats()
}
return true
})
return
}