-
Notifications
You must be signed in to change notification settings - Fork 3
/
event_zap.go
787 lines (651 loc) · 20.3 KB
/
event_zap.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
// 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 (
"bytes"
"encoding/json"
"fmt"
"github.com/spf13/cast"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"strings"
"text/tabwriter"
"time"
)
type eventStatus int
const (
// NotStarted will be assigned to Event before SetStartTime() was called.
NotStarted eventStatus = 0
// InProgress will be assigned to Event after SetStartTime() was called.
InProgress eventStatus = 1
// Ended will be assigned to Event after SetEndTime() was called.
Ended eventStatus = 2
)
// String will return string value of event status.
func (status eventStatus) String() string {
names := [...]string{
"NotStarted",
"InProgress",
"Ended",
}
if status < NotStarted || status > Ended {
return "Unknown"
}
return names[status]
}
// Encoding supported format of Console and JSON currently.
type Encoding int
const (
// CONSOLE is human readable format.
CONSOLE Encoding = 0
// JSON format.
JSON Encoding = 1
// FLATTEN format.
FLATTEN Encoding = 2
)
// String will return string value of Encoding types.
func (ec Encoding) String() string {
names := [...]string{"console", "json", "flatten"}
// Please do not forget to change the boundary while adding a new config file types
if ec > FLATTEN || ec < CONSOLE {
return "UNKNOWN"
}
return names[ec]
}
// ToEncoding returns Encoding type from string value.
func ToEncoding(f string) Encoding {
f = strings.ToLower(f)
switch f {
case "json":
return JSON
case "console":
return CONSOLE
case "flatten":
return FLATTEN
default:
return CONSOLE
}
return CONSOLE
}
// It is not thread safe.
type eventZap struct {
logger *zap.Logger
encoding Encoding
quietMode bool
serviceName string // Application
serviceVersion string // Application
entryName string // Application
entryKind string // Application
eventId string // Ids
traceId string // Ids
requestId string // Ids
endTime time.Time // Time
startTime time.Time // Time
timeZone string // Time
payloads []zap.Field // Payloads
errors *zapcore.MapObjectEncoder // Error
operation string // Event
remoteAddr string // Event
resCode string // Event
status eventStatus // Event
pairs *zapcore.MapObjectEncoder // Event
counters *zapcore.MapObjectEncoder // Event
tracker map[string]*timeTracker // Event
}
// ************* 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 *eventZap) SetStartTime(curr time.Time) {
event.startTime = curr
event.status = InProgress
}
// GetStartTime Get start time of current event data.
func (event *eventZap) GetStartTime() time.Time {
return event.startTime
}
// 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 *eventZap) SetEndTime(curr time.Time) {
if event.status != InProgress {
return
}
event.endTime = curr
event.status = Ended
}
// GetEndTime returns end time of current event data.
func (event *eventZap) GetEndTime() time.Time {
return event.endTime
}
// ************* 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 *eventZap) AddPayloads(fields ...zap.Field) {
event.payloads = append(event.payloads, fields...)
}
// ListPayloads will lists payloads.
func (event *eventZap) ListPayloads() []zap.Field {
return event.payloads
}
// ************* Identity *************
// GetEventId returns event id of current event.
func (event *eventZap) GetEventId() string {
return event.eventId
}
// 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 *eventZap) SetEventId(id string) {
event.eventId = id
}
// GetTraceId returns trace id of current event.
func (event *eventZap) GetTraceId() string {
return event.traceId
}
// SetTraceId set trace id of current event.
func (event *eventZap) SetTraceId(id string) {
event.traceId = id
}
// GetRequestId returns request id of current event.
func (event *eventZap) GetRequestId() string {
return event.requestId
}
// SetRequestId set request id of current event.
func (event *eventZap) SetRequestId(id string) {
event.requestId = id
}
// ************* Error *************
// AddErr function adds an error into event which could be printed with error.Error() function.
func (event *eventZap) AddErr(err error) {
if err == nil {
return
}
name := err.Error()
if len(name) < 1 {
name = "unknown"
}
val, ok := event.errors.Fields[name]
if !ok {
event.errors.AddInt64(name, 1)
} else {
event.errors.AddInt64(name, cast.ToInt64(val)+1)
}
}
// GetErrCount returns error count.
// We will use value of error.Error() as the key.
func (event *eventZap) GetErrCount(err error) int64 {
name := err.Error()
if len(name) < 1 {
name = "unknown"
}
val, ok := event.errors.Fields[name]
if ok {
return cast.ToInt64(val)
}
return 0
}
// ************* Event *************
// GetOperation returns operation of current event.
func (event *eventZap) GetOperation() string {
return event.operation
}
// SetOperation sets operation of current event.
func (event *eventZap) SetOperation(operation string) {
event.operation = operation
}
// GetRemoteAddr returns remote address of current event.
func (event *eventZap) GetRemoteAddr() string {
return event.remoteAddr
}
// 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 *eventZap) SetRemoteAddr(addr string) {
event.remoteAddr = addr
}
// GetResCode returns response code of current event.
// Mainly used in RPC calls.
func (event *eventZap) GetResCode() string {
return event.resCode
}
// SetResCode sets response code of current event.
func (event *eventZap) SetResCode(resCode string) {
event.resCode = resCode
}
// GetEventStatus returns event status of current event.
// Available event status as bellow:
// 1: NotStarted
// 2: InProgress
// 3: Ended
func (event *eventZap) GetEventStatus() eventStatus {
return event.status
}
// StartTimer starts timer of current sub event.
func (event *eventZap) StartTimer(name string) {
if !event.inProgress() || len(name) < 1 {
return
}
_, contains := event.tracker[name]
if !contains {
tracker := newTimeTracker(name)
if tracker == nil {
return
}
event.tracker[name] = tracker
}
nowMs := toMillisecond(time.Now())
tracker := event.tracker[name]
tracker.Start(nowMs)
}
// EndTimer ends timer of current sub event.
func (event *eventZap) EndTimer(name string) {
if !event.inProgress() || len(name) < 1 {
return
}
tracker, contains := event.tracker[name]
if !contains {
return
}
nowMs := toMillisecond(time.Now())
tracker.End(nowMs)
}
// UpdateTimerMs updates timer of current sub event with time elapsed in milli seconds.
func (event *eventZap) UpdateTimerMs(name string, elapsedMs int64) {
event.UpdateTimerMsWithSample(name, elapsedMs, 1)
}
// UpdateTimerMsWithSample updates timer of current sub event with time elapsed in milli seconds.
func (event *eventZap) UpdateTimerMsWithSample(name string, elapsedMs, sample int64) {
if !event.inProgress() || len(name) < 1 {
return
}
_, contains := event.tracker[name]
if !contains {
tracker := newTimeTracker(name)
if tracker == nil {
return
}
event.tracker[name] = tracker
}
tracker := event.tracker[name]
tracker.ElapseWithSample(elapsedMs, sample)
}
// GetTimeElapsedMs returns timer elapsed in milli seconds.
func (event *eventZap) GetTimeElapsedMs(name string) int64 {
timer, contains := event.tracker[name]
if !contains {
return -1
}
return timer.GetElapsedMs()
}
// GetValueFromPair returns value with key in pairs.
func (event *eventZap) GetValueFromPair(key string) string {
val, ok := event.pairs.Fields[key]
str := cast.ToString(val)
if ok && len(str) > 0 {
return str
}
return ""
}
// AddPair adds value with key in pairs.
func (event *eventZap) AddPair(key, value string) {
event.pairs.AddString(key, value)
}
// GetCounter returns counter of current event.
func (event *eventZap) GetCounter(key string) int64 {
val, ok := event.counters.Fields[key]
if ok {
return cast.ToInt64(val)
}
return -1
}
// SetCounter sets counter of current event.
func (event *eventZap) SetCounter(key string, value int64) {
event.counters.AddInt64(key, value)
}
// IncCounter increases counter of current event.
func (event *eventZap) IncCounter(key string, delta int64) {
val, ok := event.counters.Fields[key]
if ok {
event.counters.AddInt64(key, cast.ToInt64(val)+delta)
} else {
event.counters.AddInt64(key, delta)
}
}
// Finish sets event status and flush to logger.
func (event *eventZap) Finish() {
if event.quietMode {
return
}
switch event.encoding {
case JSON:
event.logger.With(event.toJsonFormat()...).Info("")
case CONSOLE:
event.logger.Info(event.toConsoleFormat())
case FLATTEN:
event.logger.Info(event.toFlattenFormat())
default:
event.logger.Info(event.toConsoleFormat())
}
// finish any Time Aggregators that may not be done
for _, v := range event.tracker {
v.Finish()
}
}
// Sync flushes logs in buffer, mainly used for external syncer
func (event *eventZap) Sync() {
event.logger.Sync()
}
// ************* Internal *************
// Marshal to FLATTEN format.
func (event *eventZap) toFlattenFormat() string {
builder := &bytes.Buffer{}
writer := tabwriter.NewWriter(builder, 2, 0, 4, ' ', tabwriter.TabIndent|tabwriter.StripEscape)
// timestamp
fmt.Fprint(writer, fmt.Sprintf("%s", event.GetEndTime().Format("2006-01-02T15:04:05.000Z0700")))
// res code
fmt.Fprint(writer, fmt.Sprintf("\t[%s]", getDefaultIfEmptyString(event.resCode, "[X]")))
// elapsed
fmt.Fprint(writer, fmt.Sprintf("\t%dms", event.GetEndTime().Sub(event.GetStartTime()).Milliseconds()))
// API method
// distinguish restful API and gRPC
var grpcMethod, grpcServer, grpcType, apiPath, apiMethod, apiProtocol *zap.Field
for i := range event.payloads {
field := event.payloads[i]
switch field.Key {
case "grpcMethod":
grpcMethod = &field
case "grpcServer":
grpcServer = &field
case "grpcType":
grpcType = &field
case "apiPath":
apiPath = &field
case "apiMethod":
apiMethod = &field
case "apiProtocol":
apiProtocol = &field
}
}
method, operation, protocol := "", "", ""
if grpcMethod != nil && grpcServer != nil {
method = grpcServer.String
operation = grpcMethod.String
protocol = grpcType.String
} else if apiPath != nil && apiMethod != nil {
method = apiMethod.String
operation = apiPath.String
protocol = apiProtocol.String
} else {
operation = event.operation
method = event.entryName
protocol = event.entryKind
}
// operation
fmt.Fprint(writer, fmt.Sprintf("\t%s", getDefaultIfEmptyString(operation, "[X]")))
// method
fmt.Fprint(writer, fmt.Sprintf("\t%s", getDefaultIfEmptyString(method, "[X]")))
// protocol
fmt.Fprint(writer, fmt.Sprintf("\t%s", getDefaultIfEmptyString(protocol, "[X]")))
// remote addr
fmt.Fprint(writer, fmt.Sprintf("\t%s", getDefaultIfEmptyString(event.remoteAddr, "[X]")))
// ids
ids := make([]string, 0)
if len(event.eventId) > 0 {
ids = append(ids, event.eventId)
}
if len(event.traceId) > 0 {
ids = append(ids, event.traceId)
}
fmt.Fprint(writer, fmt.Sprintf("\t[%s]", getDefaultIfEmptyString(strings.Join(ids, ","), "[X]")))
writer.Flush()
return builder.String()
}
// Marshal to CONSOLE format.
func (event *eventZap) toConsoleFormat() string {
builder := &bytes.Buffer{}
builder.WriteString(scopeDelimiter + "\n")
// We would expect bellow format of event data as RK format.
// ------------------------------------------------------------------------
// endTime=2021-06-13T00:24:20.256315+08:00
// startTime=2021-06-13T00:24:19.251056+08:00
// elapsedNano=1005258286
// timezone=CST
// ids={"eventId":"6a2f84a8-a09a-42dc-bc9e-cabc7977345d"}
// service={"serviceName":"serviceName","serviceVersion":"v0.0.1","entryName":"entry-example","entryKind":"example"}
// env={"arch":"amd64","hostname":"lark.local","localIP":"localhost","realm":"rk","region":"ap-guangzhou","az":"ap-guangzhou-1","domain":"beta","os":"darwin"}
// payloads={"f1":"f2","t2":"2021-06-13T00:24:20.256276+08:00"}
// error={"my error":1}
// counters={"count":1}
// pairs={"key":"value"}
// timing={"t1.count":1,"t1.elapsed_ms":1005}
// remoteAddr=localhost
// operation=op
// resCode=200
// eventStatus=Ended
// EOE
// ************* Time *************
// endTime
if event.GetEndTime().IsZero() {
event.SetEndTime(time.Now())
}
builder.WriteString(fmt.Sprintf("%s=%s\n", endTimeKey, event.GetEndTime().Format(time.RFC3339Nano)))
// startTime
if event.GetStartTime().IsZero() {
event.SetStartTime(time.Now())
}
builder.WriteString(fmt.Sprintf("%s=%s\n", startTimeKey, event.GetStartTime().Format(time.RFC3339Nano)))
// elapsedNano
builder.WriteString(fmt.Sprintf("%s=%d\n", elapsedKey, event.GetEndTime().Sub(event.GetStartTime()).Nanoseconds()))
// timeZone
builder.WriteString(fmt.Sprintf("%s=%s\n", timezoneKey, event.timeZone))
// ************* Ids *************
builder.WriteString(fmt.Sprintf("%s=%s\n", idsKey, event.marshalIds()))
// ************* Service *************
builder.WriteString(fmt.Sprintf("%s=%s\n", serviceKey, event.marshalService()))
// ************* Env *************
builder.WriteString(fmt.Sprintf("%s=%s\n", envKey, event.marshalEnv()))
// ************* Payloads *************
builder.WriteString(fmt.Sprintf("%s=%s\n", payloadsKey, event.marshalPayloads()))
// ************* Error *************
if len(event.errors.Fields) > 0 {
builder.WriteString(fmt.Sprintf("%s=%s\n", errKey, event.marshalEncoder(event.errors)))
}
// ************* Counter *************
builder.WriteString(fmt.Sprintf("%s=%s\n", countersKey, event.marshalEncoder(event.counters)))
// ************* Pairs *************
builder.WriteString(fmt.Sprintf("%s=%s\n", pairsKey, event.marshalEncoder(event.pairs)))
// ************* Timing *************
builder.WriteString(fmt.Sprintf("%s=%s\n", timingKey, event.marshalTiming()))
// ************* Event *************
// remote address
builder.WriteString(fmt.Sprintf("%s=%s\n", remoteAddrKey, event.GetRemoteAddr()))
// operation
builder.WriteString(fmt.Sprintf("%s=%s\n", operationKey, event.GetOperation()))
// resCode
if len(event.resCode) > 0 {
builder.WriteString(fmt.Sprintf("%s=%s\n", resCodeKey, event.resCode))
}
// status
builder.WriteString(fmt.Sprintf("%s=%s\n", eventStatusKey, event.GetEventStatus().String()))
builder.WriteString(eoe)
return builder.String()
}
// Marshal to JSON format.
func (event *eventZap) toJsonFormat() []zap.Field {
fields := make([]zapcore.Field, 0)
// We would expect bellow format of event data as JSON format.
//{
// "endTime":"2021-06-13T00:24:21.261+0800",
// "startTime":"2021-06-13T00:24:20.257+0800",
// "elapsedNano":1004326112,
// "timezone":"CST",
// "ids":{
// "eventId":"72a59682-230f-4ba2-a9fc-e99a031e4d8c",
// "requestId":"",
// "traceId":""
// },
// "service":{
// "serviceName":"serviceName",
// "serviceVersion":"unknown",
// "entryName":"unknown",
// "entryKind":"unknown"
// },
// "env":{
// "arch":"amd64",
// "hostname":"lark.local",
// "localIP":"localhost"
// "realm":"*",
// "region":"*",
// "az":"*",
// "domain":"*",
// "os":"darwin"
// },
// "payloads":{
// "f1":"f2",
// "t2":"2021-06-13T00:24:21.261768+08:00"
// },
// "error":{
// "my error":1
// },
// "counters":{
// "count":1
// },
// "pairs":{
// "key":"value"
// },
// "timing":{
// "t1.count":1,
// "t1.elapsed_ms":1004
// },
// "remoteAddr":"localhost",
// "operation":"op",
// "eventStatus":"Ended",
// "resCode":"200"
//}
// endTime
if event.GetEndTime().IsZero() {
event.SetEndTime(time.Now())
}
// startTime
if event.GetStartTime().IsZero() {
event.SetStartTime(time.Now())
}
fields = append(fields,
zap.Time(endTimeKey, event.GetEndTime()),
zap.Time(startTimeKey, event.GetStartTime()),
zap.Int64(elapsedKey, event.GetEndTime().Sub(event.GetStartTime()).Nanoseconds()),
zap.String(timezoneKey, event.timeZone),
zap.Any(idsKey, event.idsToMapObjectEncoder().Fields),
zap.Any(serviceKey, event.serviceToMapObjectEncoder().Fields),
zap.Any(envKey, event.envToMapObjectEncoder().Fields),
zap.Any(payloadsKey, event.payloadsToMapObjectEncoder().Fields),
zap.Any(errKey, event.errors.Fields),
zap.Any(countersKey, event.counters.Fields),
zap.Any(pairsKey, event.pairs.Fields),
zap.Any(timingKey, event.timingToMapObjectEncoder().Fields),
zap.String(remoteAddrKey, event.GetRemoteAddr()),
zap.String(operationKey, event.GetOperation()),
zap.String(eventStatusKey, event.GetEventStatus().String()))
if len(event.errors.Fields) > 0 {
fields = append(fields, zap.Any(errKey, event.errors.Fields))
}
// resCode
if len(event.resCode) > 0 {
fields = append(fields, zap.String(resCodeKey, event.resCode))
}
return fields
}
// Construct payloads to zapcore.MapObjectEncoder
func (event *eventZap) payloadsToMapObjectEncoder() *zapcore.MapObjectEncoder {
enc := zapcore.NewMapObjectEncoder()
for i := range event.payloads {
event.payloads[i].AddTo(enc)
}
return enc
}
// Marshal payloads.
func (event *eventZap) marshalPayloads() string {
return event.marshalEncoder(event.payloadsToMapObjectEncoder())
}
// Construct env to zapcore.MapObjectEncoder
func (event *eventZap) envToMapObjectEncoder() *zapcore.MapObjectEncoder {
enc := zapcore.NewMapObjectEncoder()
enc.AddString(hostnameKey, hostname)
enc.AddString(localIpKey, localIp)
enc.AddString(domainKey, domain)
enc.AddString(goosKey, goos)
enc.AddString(goArchKey, goArch)
return enc
}
// Marshal env.
func (event *eventZap) marshalEnv() string {
return event.marshalEncoder(event.envToMapObjectEncoder())
}
// Construct ids to zapcore.MapObjectEncoder
func (event *eventZap) idsToMapObjectEncoder() *zapcore.MapObjectEncoder {
enc := zapcore.NewMapObjectEncoder()
if len(event.eventId) > 0 {
enc.AddString(eventIdKey, event.eventId)
}
if len(event.traceId) > 0 {
enc.AddString(traceIdKey, event.traceId)
}
if len(event.requestId) > 0 {
enc.AddString(requestIdKey, event.requestId)
}
return enc
}
// Marshal ids.
func (event *eventZap) marshalIds() string {
return event.marshalEncoder(event.idsToMapObjectEncoder())
}
// Construct service to zapcore.MapObjectEncoder
func (event *eventZap) serviceToMapObjectEncoder() *zapcore.MapObjectEncoder {
enc := zapcore.NewMapObjectEncoder()
enc.AddString(serviceNameKey, event.serviceName)
enc.AddString(serviceVersionKey, event.serviceVersion)
enc.AddString(entryNameKey, event.entryName)
enc.AddString(entryKindKey, event.entryKind)
return enc
}
// Marshal service.
func (event *eventZap) marshalService() string {
return event.marshalEncoder(event.serviceToMapObjectEncoder())
}
// Construct timing to zapcore.MapObjectEncoder
func (event *eventZap) timingToMapObjectEncoder() *zapcore.MapObjectEncoder {
enc := zapcore.NewMapObjectEncoder()
for _, v := range event.tracker {
v.ToZapFields(enc)
}
return enc
}
// Marshal timing.
func (event *eventZap) marshalTiming() string {
return event.marshalEncoder(event.timingToMapObjectEncoder())
}
// Marshal zapcore.MapObjectEncoder.
func (event *eventZap) marshalEncoder(enc *zapcore.MapObjectEncoder) string {
builder := &bytes.Buffer{}
bytes, err := json.Marshal(enc.Fields)
if err != nil {
builder.WriteString("{}")
} else {
_, err := builder.Write(bytes)
if err != nil {
builder.WriteString("{}")
}
}
return builder.String()
}
// Is Event in progress?
func (event *eventZap) inProgress() bool {
if event.status != InProgress {
return false
}
return true
}
// Convert time.Time to milliseconds.
func toMillisecond(curr time.Time) int64 {
return curr.UnixNano() / 1e6
}