-
Notifications
You must be signed in to change notification settings - Fork 837
/
Copy pathrpcstats.go
290 lines (247 loc) · 6.76 KB
/
rpcstats.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
* Copyright 2021 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rpcinfo
import (
"context"
"sync"
"sync/atomic"
"time"
"github.com/cloudwego/kitex/internal"
"github.com/cloudwego/kitex/pkg/stats"
)
var (
_ RPCStats = (*rpcStats)(nil)
_ MutableRPCStats = &rpcStats{}
_ internal.Reusable = (*rpcStats)(nil)
_ internal.Reusable = (*event)(nil)
rpcStatsPool sync.Pool
eventPool sync.Pool
once sync.Once
maxEventNum int
)
type event struct {
event stats.Event
status stats.Status
info string
time time.Time
}
// Event implements the Event interface.
func (e *event) Event() stats.Event {
return e.event
}
// Status implements the Event interface.
func (e *event) Status() stats.Status {
return e.status
}
// Info implements the Event interface.
func (e *event) Info() string {
return e.info
}
// Time implements the Event interface.
func (e *event) Time() time.Time {
return e.time
}
// IsNil implements the Event interface.
func (e *event) IsNil() bool {
return e == nil
}
func newEvent() interface{} {
return &event{}
}
func (e *event) zero() {
e.event = nil
e.status = 0
e.info = ""
e.time = time.Time{}
}
// Recycle reuses the event.
func (e *event) Recycle() {
e.zero()
eventPool.Put(e)
}
type atomicErr struct {
err error
}
type atomicPanicErr struct {
panicErr interface{}
}
type rpcStats struct {
sync.RWMutex
level stats.Level
eventMap []Event
sendSize uint64
lastSendSize uint64 // for Streaming APIs, record the size of the last sent message
recvSize uint64
lastRecvSize uint64 // for Streaming APIs, record the size of the last received message
err atomic.Value
panicErr atomic.Value
}
func init() {
rpcStatsPool.New = newRPCStats
eventPool.New = newEvent
}
func newRPCStats() interface{} {
return &rpcStats{
eventMap: make([]Event, maxEventNum),
}
}
// Record implements the RPCStats interface.
func (r *rpcStats) Record(ctx context.Context, e stats.Event, status stats.Status, info string) {
if e.Level() > r.level {
return
}
eve := NewEvent(e, status, info)
idx := e.Index()
r.Lock()
r.eventMap[idx] = eve
r.Unlock()
}
// NewEvent creates a new Event based on the given event, status and info.
func NewEvent(statsEvent stats.Event, status stats.Status, info string) Event {
eve := eventPool.Get().(*event)
eve.event = statsEvent
eve.status = status
eve.info = info
eve.time = time.Now()
return eve
}
// SendSize implements the RPCStats interface.
func (r *rpcStats) SendSize() uint64 {
return atomic.LoadUint64(&r.sendSize)
}
// LastSendSize implements the RPCStats interface.
func (r *rpcStats) LastSendSize() uint64 {
return atomic.LoadUint64(&r.lastSendSize)
}
// RecvSize implements the RPCStats interface.
func (r *rpcStats) RecvSize() uint64 {
return atomic.LoadUint64(&r.recvSize)
}
// LastRecvSize implements the RPCStats interface.
func (r *rpcStats) LastRecvSize() uint64 {
return atomic.LoadUint64(&r.lastRecvSize)
}
// Error implements the RPCStats interface.
func (r *rpcStats) Error() error {
ae, _ := r.err.Load().(atomicErr)
return ae.err
}
// Panicked implements the RPCStats interface.
func (r *rpcStats) Panicked() (bool, interface{}) {
ape, _ := r.panicErr.Load().(atomicPanicErr)
return ape.panicErr != nil, ape.panicErr
}
// GetEvent implements the RPCStats interface.
func (r *rpcStats) GetEvent(e stats.Event) Event {
idx := e.Index()
r.RLock()
evt := r.eventMap[idx]
r.RUnlock()
if evt == nil || evt.IsNil() {
return nil
}
return evt
}
// Level implements the RPCStats interface.
func (r *rpcStats) Level() stats.Level {
return r.level
}
// CopyForRetry implements the RPCStats interface, it copies a RPCStats from the origin one
// to pass through info of the first request to retrying requests.
func (r *rpcStats) CopyForRetry() RPCStats {
// Copied rpc stats is for request retrying and cannot be reused, so no need to get from pool.
nr := newRPCStats().(*rpcStats)
r.Lock()
startIdx := int(stats.RPCStart.Index())
userIdx := stats.PredefinedEventNum()
for i := 0; i < len(nr.eventMap); i++ {
// Ignore none RPCStart events to avoid incorrect tracing.
if i == startIdx || i >= userIdx {
nr.eventMap[i] = r.eventMap[i]
}
}
r.Unlock()
return nr
}
// SetSendSize sets send size.
// This should be called by Ping-Pong APIs which only send once.
func (r *rpcStats) SetSendSize(size uint64) {
atomic.StoreUint64(&r.sendSize, size)
}
// IncrSendSize increments send size.
// This should be called by Streaming APIs which may send multiple times.
func (r *rpcStats) IncrSendSize(size uint64) {
atomic.AddUint64(&r.sendSize, size)
atomic.StoreUint64(&r.lastSendSize, size)
}
// SetRecvSize sets recv size.
// This should be called by Ping-Pong APIs which only recv once.
func (r *rpcStats) SetRecvSize(size uint64) {
atomic.StoreUint64(&r.recvSize, size)
}
// IncrRecvSize increments recv size.
// This should be called by Streaming APIs which may recv multiple times.
func (r *rpcStats) IncrRecvSize(size uint64) {
atomic.AddUint64(&r.recvSize, size)
atomic.StoreUint64(&r.lastRecvSize, size)
}
// SetError sets error.
func (r *rpcStats) SetError(err error) {
r.err.Store(atomicErr{err: err})
}
// SetPanicked sets if panicked.
func (r *rpcStats) SetPanicked(x interface{}) {
r.panicErr.Store(atomicPanicErr{panicErr: x})
}
// SetLevel sets the level.
func (r *rpcStats) SetLevel(level stats.Level) {
r.level = level
}
// Reset resets the stats.
func (r *rpcStats) Reset() {
r.level = 0
if ae, _ := r.err.Load().(atomicErr); ae.err != nil {
r.err.Store(atomicErr{})
}
if ape, _ := r.panicErr.Load().(atomicPanicErr); ape.panicErr != nil {
r.panicErr.Store(atomicPanicErr{})
}
atomic.StoreUint64(&r.recvSize, 0)
atomic.StoreUint64(&r.sendSize, 0)
for i := range r.eventMap {
if r.eventMap[i] != nil {
r.eventMap[i].(*event).Recycle()
r.eventMap[i] = nil
}
}
}
// ImmutableView restricts the rpcStats into a read-only rpcinfo.RPCStats.
func (r *rpcStats) ImmutableView() RPCStats {
return r
}
// Recycle reuses the rpcStats.
func (r *rpcStats) Recycle() {
r.Reset()
rpcStatsPool.Put(r)
}
// NewRPCStats creates a new RPCStats.
func NewRPCStats() RPCStats {
once.Do(func() {
stats.FinishInitialization()
maxEventNum = stats.MaxEventNum()
})
return rpcStatsPool.Get().(*rpcStats)
}