forked from rookie-ninja/rk-query
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_threadsafe.go
295 lines (223 loc) · 7.44 KB
/
event_threadsafe.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
291
292
293
294
295
// Copyright (c) 2021 rookie-ninja
//
// Use of this source code is governed by an Apache-style
// license that can be found in the LICENSE file.
package rkquery
import (
"go.uber.org/zap"
"sync"
"time"
)
type eventThreadSafe struct {
delegate *eventZap
lock *sync.Mutex
}
// ************* Time *************
// SetStartTime sets start timer of current event. This can be overridden by user.
// We keep this function open in order to mock event during unit test.
func (event *eventThreadSafe) SetStartTime(curr time.Time) {
event.lock.Lock()
defer event.lock.Unlock()
event.delegate.SetStartTime(curr)
}
// GetStartTime returns start time of current event data.
func (event *eventThreadSafe) GetStartTime() time.Time {
event.lock.Lock()
defer event.lock.Unlock()
return event.delegate.GetStartTime()
}
// SetEndTime sets end timer of current event. This can be overridden by user.
// We keep this function open in order to mock event during unit test.
func (event *eventThreadSafe) SetEndTime(curr time.Time) {
event.lock.Lock()
defer event.lock.Unlock()
event.delegate.SetEndTime(curr)
}
// GetEndTime returns end time of current event data.
func (event *eventThreadSafe) GetEndTime() time.Time {
event.lock.Lock()
defer event.lock.Unlock()
return event.delegate.GetEndTime()
}
// ************* Payload *************
// AddPayloads function add payload as zap.Field.
// Payload could be anything with RPC requests or user event such as http request param.
func (event *eventThreadSafe) AddPayloads(fields ...zap.Field) {
event.lock.Lock()
defer event.lock.Unlock()
event.delegate.AddPayloads(fields...)
}
// ListPayloads will lists payloads.
func (event *eventThreadSafe) ListPayloads() []zap.Field {
event.lock.Lock()
defer event.lock.Unlock()
return event.delegate.ListPayloads()
}
// ************* Identity *************
// GetEventId returns event id of current event.
func (event *eventThreadSafe) GetEventId() string {
event.lock.Lock()
defer event.lock.Unlock()
return event.delegate.GetEventId()
}
// SetEventId sets event id of current event.
// A new event id would be created while event data was created from EventFactory.
// User could override event id with this function.
func (event *eventThreadSafe) SetEventId(id string) {
event.lock.Lock()
defer event.lock.Unlock()
event.delegate.SetEventId(id)
}
// GetTraceId returns trace id of current event.
func (event *eventThreadSafe) GetTraceId() string {
event.lock.Lock()
defer event.lock.Unlock()
return event.delegate.GetTraceId()
}
// SetTraceId set trace id of current event.
func (event *eventThreadSafe) SetTraceId(id string) {
event.lock.Lock()
defer event.lock.Unlock()
event.delegate.SetTraceId(id)
}
// GetRequestId returns request id of current event.
func (event *eventThreadSafe) GetRequestId() string {
event.lock.Lock()
defer event.lock.Unlock()
return event.delegate.GetRequestId()
}
// SetRequestId set request id of current event.
func (event *eventThreadSafe) SetRequestId(id string) {
event.lock.Lock()
defer event.lock.Unlock()
event.delegate.SetRequestId(id)
}
// ************* Error *************
// AddErr function adds an error into event which could be printed with error.Error() function.
func (event *eventThreadSafe) AddErr(err error) {
event.lock.Lock()
defer event.lock.Unlock()
event.delegate.AddErr(err)
}
// GetErrCount returns error count.
// We will use value of error.Error() as the key.
func (event *eventThreadSafe) GetErrCount(err error) int64 {
event.lock.Lock()
defer event.lock.Unlock()
return event.delegate.GetErrCount(err)
}
// ************* Event *************
// GetOperation returns operation of current event.
func (event *eventThreadSafe) GetOperation() string {
event.lock.Lock()
defer event.lock.Unlock()
return event.delegate.GetOperation()
}
// SetOperation sets operation of current event.
func (event *eventThreadSafe) SetOperation(operation string) {
event.lock.Lock()
defer event.lock.Unlock()
event.delegate.SetOperation(operation)
}
// GetRemoteAddr returns remote address of current event.
func (event *eventThreadSafe) GetRemoteAddr() string {
event.lock.Lock()
defer event.lock.Unlock()
return event.delegate.GetRemoteAddr()
}
// SetRemoteAddr sets remote address of current event, mainly used in RPC calls.
// Default value of <localhost> would be assigned while creating event via EventFactory.
func (event *eventThreadSafe) SetRemoteAddr(addr string) {
event.lock.Lock()
defer event.lock.Unlock()
event.delegate.SetRemoteAddr(addr)
}
// GetResCode returns response code of current event.
// Mainly used in RPC calls.
func (event *eventThreadSafe) GetResCode() string {
event.lock.Lock()
defer event.lock.Unlock()
return event.delegate.GetResCode()
}
// SetResCode sets response code of current event.
func (event *eventThreadSafe) SetResCode(resCode string) {
event.lock.Lock()
defer event.lock.Unlock()
event.delegate.SetResCode(resCode)
}
// GetEventStatus returns event status of current event.
// Available event status as bellow:
// 1: NotStarted
// 2: InProgress
// 3: Ended
func (event *eventThreadSafe) GetEventStatus() eventStatus {
event.lock.Lock()
defer event.lock.Unlock()
return event.delegate.GetEventStatus()
}
// StartTimer starts timer of current sub event.
func (event *eventThreadSafe) StartTimer(name string) {
event.lock.Lock()
defer event.lock.Unlock()
event.delegate.StartTimer(name)
}
// EndTimer ends timer of current sub event.
func (event *eventThreadSafe) EndTimer(name string) {
event.lock.Lock()
defer event.lock.Unlock()
event.delegate.EndTimer(name)
}
// UpdateTimerMs updates timer of current sub event with time elapsed in milli seconds.
func (event *eventThreadSafe) UpdateTimerMs(name string, elapsedMs int64) {
event.lock.Lock()
defer event.lock.Unlock()
event.delegate.UpdateTimerMs(name, elapsedMs)
}
// UpdateTimerMsWithSample updates timer of current sub event with time elapsed in milli seconds.
func (event *eventThreadSafe) UpdateTimerMsWithSample(name string, elapsedMs, sample int64) {
event.lock.Lock()
defer event.lock.Unlock()
event.delegate.UpdateTimerMsWithSample(name, elapsedMs, sample)
}
// GetTimeElapsedMs returns timer elapsed in milli seconds.
func (event *eventThreadSafe) GetTimeElapsedMs(name string) int64 {
event.lock.Lock()
defer event.lock.Unlock()
return event.delegate.GetTimeElapsedMs(name)
}
// GetValueFromPair returns value with key in pairs.
func (event *eventThreadSafe) GetValueFromPair(key string) string {
event.lock.Lock()
defer event.lock.Unlock()
return event.delegate.GetValueFromPair(key)
}
// AddPair adds value with key in pairs.
func (event *eventThreadSafe) AddPair(key, value string) {
event.lock.Lock()
defer event.lock.Unlock()
event.delegate.AddPair(key, value)
}
// GetCounter returns counter of current event.
func (event *eventThreadSafe) GetCounter(key string) int64 {
event.lock.Lock()
defer event.lock.Unlock()
return event.delegate.GetCounter(key)
}
// SetCounter sets counter of current event.
func (event *eventThreadSafe) SetCounter(key string, value int64) {
event.lock.Lock()
defer event.lock.Unlock()
event.delegate.SetCounter(key, value)
}
// IncCounter increases counter of current event.
func (event *eventThreadSafe) IncCounter(key string, delta int64) {
event.lock.Lock()
defer event.lock.Unlock()
event.delegate.IncCounter(key, delta)
}
// Finish sets event status and flush to logger.
func (event *eventThreadSafe) Finish() {
event.lock.Lock()
defer event.lock.Unlock()
event.delegate.Finish()
}