-
Notifications
You must be signed in to change notification settings - Fork 3
/
bytes.go
335 lines (258 loc) · 6.64 KB
/
bytes.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package dmetering
import (
"context"
"fmt"
"sync"
"github.com/streamingfast/logging"
tracing "github.com/streamingfast/sf-tracing"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type bytesMeterKey string
const contextKey = bytesMeterKey("bytesMeter")
func GetBytesMeter(ctx context.Context) Meter {
if bm, ok := ctx.Value(contextKey).(Meter); ok && bm != nil {
return bm
}
return NoopBytesMeter
}
func WithBytesMeter(ctx context.Context) context.Context {
//check if meter already exists and that it is not nil or a noop
if bm, ok := ctx.Value(contextKey).(Meter); ok && bm != nil && bm != NoopBytesMeter {
return ctx
}
bm := NewBytesMeter()
return WithExistingBytesMeter(ctx, bm)
}
func WithCounter(ctx context.Context, name string) context.Context {
bm := GetBytesMeter(ctx)
bm.AddCounter(name)
return WithExistingBytesMeter(ctx, bm)
}
func WithExistingBytesMeter(ctx context.Context, bm Meter) context.Context {
if bm == nil {
return ctx
}
return context.WithValue(ctx, contextKey, bm)
}
type Meter interface {
AddBytesWritten(n int)
AddBytesRead(n int)
AddBytesWrittenCtx(ctx context.Context, n int)
AddBytesReadCtx(ctx context.Context, n int)
BytesWritten() uint64
BytesRead() uint64
BytesWrittenDelta() uint64
BytesReadDelta() uint64
AddCounter(name string)
CountInc(name string, n int)
CountDec(name string, n int)
GetCount(name string) int
GetCountAndReset(name string) int
ResetCount(name string)
}
type meter struct {
bytesWritten uint64
bytesRead uint64
bytesWrittenDelta uint64
bytesReadDelta uint64
counterMap map[string]int
mu sync.RWMutex
}
func NewBytesMeter() Meter {
return &meter{}
}
func (b *meter) String() string {
b.mu.RLock()
defer b.mu.RUnlock()
return fmt.Sprintf("bytes written: %d, bytes read: %d", b.bytesWritten, b.bytesRead)
}
func (b *meter) AddBytesWritten(n int) {
b.mu.Lock()
defer b.mu.Unlock()
if n < 0 {
panic("negative value")
}
b.bytesWrittenDelta += uint64(n)
b.bytesWritten += uint64(n)
}
type mode string
const (
modeRead mode = "read"
modeWrite mode = "write"
)
func (b *meter) AddBytesRead(n int) {
b.mu.Lock()
defer b.mu.Unlock()
b.bytesReadDelta += uint64(n)
b.bytesRead += uint64(n)
}
func (b *meter) AddBytesWrittenCtx(ctx context.Context, n int) {
logDataFromCtx(ctx, n, modeWrite)
b.AddBytesWritten(n)
}
func (b *meter) AddBytesReadCtx(ctx context.Context, n int) {
logDataFromCtx(ctx, n, modeRead)
b.AddBytesRead(n)
}
func (b *meter) BytesWritten() uint64 {
b.mu.RLock()
defer b.mu.RUnlock()
return b.bytesWritten
}
func (b *meter) BytesRead() uint64 {
b.mu.RLock()
defer b.mu.RUnlock()
return b.bytesRead
}
func (b *meter) BytesWrittenDelta() uint64 {
b.mu.Lock()
defer b.mu.Unlock()
result := b.bytesWrittenDelta
b.bytesWrittenDelta = 0
return result
}
func (b *meter) BytesReadDelta() uint64 {
b.mu.Lock()
defer b.mu.Unlock()
result := b.bytesReadDelta
b.bytesReadDelta = 0
return result
}
func (b *meter) AddCounter(name string) {
b.mu.Lock()
defer b.mu.Unlock()
if b.counterMap == nil {
b.counterMap = make(map[string]int)
}
if _, ok := b.counterMap[name]; ok {
return
}
b.counterMap[name] = 0
}
func (b *meter) CountInc(name string, n int) {
b.mu.Lock()
defer b.mu.Unlock()
if b.counterMap == nil {
b.counterMap = make(map[string]int)
}
if _, ok := b.counterMap[name]; !ok {
b.counterMap[name] = 0
}
b.counterMap[name] += n
}
func (b *meter) CountDec(name string, n int) {
b.mu.Lock()
defer b.mu.Unlock()
if b.counterMap == nil {
b.counterMap = make(map[string]int)
}
if _, ok := b.counterMap[name]; !ok {
b.counterMap[name] = 0
}
b.counterMap[name] -= n
}
func (b *meter) GetCount(name string) int {
b.mu.RLock()
defer b.mu.RUnlock()
if b.counterMap == nil {
b.counterMap = make(map[string]int)
}
if val, ok := b.counterMap[name]; ok {
return val
}
return 0
}
func (b *meter) GetCountAndReset(name string) int {
b.mu.Lock()
defer b.mu.Unlock()
if b.counterMap == nil {
b.counterMap = make(map[string]int)
}
if val, ok := b.counterMap[name]; ok {
b.counterMap[name] = 0
return val
}
return 0
}
func (b *meter) ResetCount(name string) {
b.mu.Lock()
defer b.mu.Unlock()
if b.counterMap == nil {
b.counterMap = make(map[string]int)
}
if _, ok := b.counterMap[name]; ok {
b.counterMap[name] = 0
}
return
}
type noopMeter struct{}
func (_ *noopMeter) ResetCount(name string) { return }
func (_ *noopMeter) AddBytesWritten(n int) { return }
func (_ *noopMeter) AddBytesRead(n int) { return }
func (_ *noopMeter) AddBytesWrittenCtx(ctx context.Context, n int) {}
func (_ *noopMeter) AddBytesReadCtx(ctx context.Context, n int) {}
func (_ *noopMeter) BytesWritten() uint64 { return 0 }
func (_ *noopMeter) BytesRead() uint64 { return 0 }
func (_ *noopMeter) BytesWrittenDelta() uint64 { return 0 }
func (_ *noopMeter) BytesReadDelta() uint64 { return 0 }
func (_ *noopMeter) AddCounter(name string) { return }
func (_ *noopMeter) CountInc(name string, n int) { return }
func (_ *noopMeter) CountDec(name string, n int) { return }
func (_ *noopMeter) GetCount(name string) int { return 0 }
func (_ *noopMeter) GetCountAndReset(name string) int { return 0 }
var NoopBytesMeter Meter = &noopMeter{}
type MeterLogData struct {
Filename *string
Store *string
TraceId *string
Mode mode
NBytes int
}
func (mld *MeterLogData) MarshalLogObject(enc zapcore.ObjectEncoder) error {
if mld.Filename != nil {
enc.AddString("file", *mld.Filename)
}
if mld.Store != nil {
enc.AddString("store", *mld.Store)
}
if mld.TraceId != nil {
enc.AddString("trace_id", *mld.TraceId)
}
enc.AddString("mode", string(mld.Mode))
enc.AddInt("bytes", mld.NBytes)
return nil
}
func logDataFromCtx(ctx context.Context, nBytes int, mode mode) {
var logger *zap.Logger
if val, ok := ctx.Value("logger").(*zap.Logger); ok {
logger = val
} else {
return
}
var tracer logging.Tracer
if val, ok := ctx.Value("tracer").(logging.Tracer); ok {
tracer = val
} else {
return
}
var filename, store, traceId *string
if val, ok := ctx.Value("file").(string); ok {
filename = &val
}
if val, ok := ctx.Value("store").(string); ok {
store = &val
}
traceIdVal := tracing.GetTraceID(ctx).String()
traceId = &traceIdVal
mld := &MeterLogData{
Filename: filename,
Store: store,
TraceId: traceId,
Mode: mode,
NBytes: nBytes,
}
if tracer.Enabled() {
logger.Debug("bytes metering", zap.Object("meter_log_data", mld))
}
}