forked from alibaba/IOC-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
191 lines (164 loc) · 5.04 KB
/
context.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package monitor
import (
"sort"
"sync"
"time"
"github.com/cc-cheunggg/ioc-golang/aop"
"github.com/cc-cheunggg/ioc-golang/aop/common"
monitorPB "github.com/cc-cheunggg/ioc-golang/extension/aop/monitor/api/ioc_golang/aop/monitor"
)
// +ioc:autowire=true
// +ioc:autowire:type=normal
// +ioc:autowire:paramType=contextParam
// +ioc:autowire:constructFunc=init
// +ioc:autowire:proxy:autoInjection=false
type context struct {
sdid string
methodName string
ch chan *monitorPB.MonitorResponse
ticker *time.Ticker
stopCh chan struct{}
methodUniqueNameInvocationRecordMap map[string]methodInvocationRecordIOCInterface // methodUniqueName -> methodInvocationRecord
methodUniqueNameInvocationRecordMapLock sync.Mutex
destroyed bool
}
type contextParam struct {
SDID string
MethodName string
Ch chan *monitorPB.MonitorResponse
Period time.Duration
}
func (p *contextParam) init(c *context) (*context, error) {
c.ch = p.Ch
c.methodName = p.MethodName
c.sdid = p.SDID
c.methodUniqueNameInvocationRecordMap = make(map[string]methodInvocationRecordIOCInterface)
c.stopCh = make(chan struct{})
c.destroyed = false
c.ticker = time.NewTicker(p.Period)
go c.run()
return c, nil
}
func (c *context) run() {
for {
select {
case <-c.stopCh:
return
case <-c.ticker.C:
// collect data
monitorResponseItemSorter := make(monitorResponseItemsSorter, 0)
c.methodUniqueNameInvocationRecordMapLock.Lock()
for invocationMethodKey, invocationMethodRecord := range c.methodUniqueNameInvocationRecordMap {
sdid, methodName := common.ParseSDIDAndMethodFromUniqueKey(invocationMethodKey)
total, success, fail, agRT, failedRate := invocationMethodRecord.DescribeAndReset()
if total == 0 {
continue
}
monitorResponseItemSorter = append(monitorResponseItemSorter, &monitorPB.MonitorResponseItem{
Sdid: sdid,
Method: methodName,
Total: int64(total),
Success: int64(success),
Fail: int64(fail),
AvgRT: agRT,
FailRate: failedRate,
})
}
c.methodUniqueNameInvocationRecordMapLock.Unlock()
sort.Sort(monitorResponseItemSorter)
c.ch <- &monitorPB.MonitorResponse{
MonitorResponseItems: monitorResponseItemSorter,
}
}
}
}
func (c *context) filterAndGetRecord(ctx *aop.InvocationContext) (methodInvocationRecordIOCInterface, bool) {
// filter invocations that is not monitored
if c.sdid != "" && ctx.SDID != c.sdid {
return nil, false
} else if c.methodName != "" && ctx.MethodName != c.methodName {
return nil, false
}
// monitor the invocation
invocationMethodKey := common.GetMethodUniqueKey(ctx.SDID, ctx.MethodName)
c.methodUniqueNameInvocationRecordMapLock.Lock()
defer c.methodUniqueNameInvocationRecordMapLock.Unlock()
methodRecord, ok := c.methodUniqueNameInvocationRecordMap[invocationMethodKey]
if !ok {
methodRecord, _ = GetmethodInvocationRecordIOCInterface()
c.methodUniqueNameInvocationRecordMap[invocationMethodKey] = methodRecord
}
return methodRecord, true
}
func (c *context) BeforeInvoke(ctx *aop.InvocationContext) {
monitorMethodRecord, match := c.filterAndGetRecord(ctx)
if match {
monitorMethodRecord.BeforeRequest(ctx)
}
}
func (c *context) AfterInvoke(ctx *aop.InvocationContext) {
monitorMethodRecord, match := c.filterAndGetRecord(ctx)
if match {
monitorMethodRecord.AfterRequest(ctx)
}
}
func (c *context) Destroy() {
if !c.destroyed {
c.destroyed = true
c.ticker.Stop()
close(c.stopCh)
}
}
// +ioc:autowire=true
// +ioc:autowire:type=normal
// +ioc:autowire:constructFunc=newMethodInvocationRecord
// +ioc:autowire:proxy:autoInjection=false
type methodInvocationRecord struct {
total int
success int
fail int
rts []int64
grIDReqMap sync.Map // grID -> req before invoke time
lock sync.RWMutex
}
func newMethodInvocationRecord(record *methodInvocationRecord) (*methodInvocationRecord, error) {
record.rts = make([]int64, 0)
return record, nil
}
func (m *methodInvocationRecord) DescribeAndReset() (int, int, int, float32, float32) {
m.lock.Lock()
defer m.lock.Unlock()
agRT := getAverageInt64(m.rts)
failedRate := float32(0)
if m.fail > 0 {
failedRate = float32(m.fail) / float32(m.total)
}
total := m.total
success := m.success
fail := m.fail
m.total = 0
m.success = 0
m.fail = 0
m.rts = make([]int64, 0)
return total, success, fail, agRT, failedRate
}
func (m *methodInvocationRecord) BeforeRequest(ctx *aop.InvocationContext) {
m.grIDReqMap.Store(ctx.ID, time.Now().UnixMicro())
}
func (m *methodInvocationRecord) AfterRequest(ctx *aop.InvocationContext) {
val, ok := m.grIDReqMap.LoadAndDelete(ctx.ID)
if !ok {
return
}
startTime := val.(int64)
duration := time.Now().UnixMicro() - startTime
m.lock.Lock()
defer m.lock.Unlock()
m.rts = append(m.rts, duration)
m.total += 1
if isFailed, _ := common.IsInvocationFailed(ctx.ReturnValues); isFailed {
m.fail += 1
} else {
m.success += 1
}
}