-
Notifications
You must be signed in to change notification settings - Fork 1
/
apm_monitor.go
80 lines (65 loc) · 1.72 KB
/
apm_monitor.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
74
75
76
77
78
79
80
package easycall
import (
"sync"
"sync/atomic"
"time"
)
const APM_COUNT_INTERVAL = 60
type ApmMonitorHandler interface {
OnData(data map[string]*ApmMonitorStatus)
}
func NewApmMonitor(handler ApmMonitorHandler) *ApmMonitor {
apmMonitor := &ApmMonitor{}
apmMonitor.handler = handler
apmMonitor.interval = APM_COUNT_INTERVAL * time.Second
apmMonitor.mutex = &sync.Mutex{}
apmMonitor.statuses = make(map[string]*ApmMonitorStatus)
go apmMonitor.reportAndReset()
return apmMonitor
}
type ApmMonitorStatus struct {
Total uint64
Error uint64
Time uint64
}
type ApmMonitor struct {
handler ApmMonitorHandler
interval time.Duration
statuses map[string]*ApmMonitorStatus
mutex *sync.Mutex
}
func (am *ApmMonitor) Middleware(req *Request, resp *Response, client *EasyConnection, next *MiddlewareInfo) {
am.mutex.Lock()
status := am.statuses[req.GetHead().GetMethod()]
if status == nil {
status = &ApmMonitorStatus{}
am.statuses[req.GetHead().GetMethod()] = status
}
am.mutex.Unlock()
atomic.AddUint64(&status.Total, 1)
start := time.Now()
next.Middleware(req, resp, client, next.Next)
end := time.Now()
spendTime := end.Sub(start)
atomic.AddUint64(&status.Time, uint64(spendTime.Milliseconds()))
head := resp.GetHead()
if head != nil {
if head.GetRet() != 0 {
atomic.AddUint64(&status.Error, 1)
}
}
}
func (am *ApmMonitor) reportAndReset() {
for range time.NewTicker(am.interval).C {
am.mutex.Lock()
statuses := am.statuses
am.statuses = make(map[string]*ApmMonitorStatus)
am.mutex.Unlock()
for _, v := range statuses {
if v.Total > 0 {
v.Time = v.Time / v.Total
}
}
am.handler.OnData(statuses)
}
}