-
Notifications
You must be signed in to change notification settings - Fork 0
/
span.go
71 lines (55 loc) · 1.64 KB
/
span.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
package log
import (
"time"
"github.com/uber-go/zap"
)
// SpanLog handles log messages for a given span
type SpanLog struct {
opName string
fields []Field
startedAt time.Time
logger zap.Logger
}
// Close should be called when when exiting the span
func (s *SpanLog) Close(err error, msg string, fields ...Field) error {
elapsed := time.Now().Sub(s.startedAt) / time.Millisecond
f := zapFields(s.fields, fields, Error(err), Int64("elapsed", int64(elapsed)))
s.logger.Info(msg, f...)
return err
}
// Debug just prints output
func (s *SpanLog) Debug(msg string, fields ...Field) {
f := zapFields(s.fields, fields)
s.logger.Debug(msg, f...)
}
// Info just prints output
func (s *SpanLog) Info(msg string, fields ...Field) {
f := zapFields(s.fields, fields)
s.logger.Info(msg, f...)
}
func (s *SpanLog) zapFields(fields []Field, additional ...Field) []zap.Field {
zapFields := make([]zap.Field, 0, len(s.fields)+len(fields)+len(additional))
for _, field := range additional {
zapFields = append(zapFields, zap.Field(field))
}
for _, field := range s.fields {
zapFields = append(zapFields, zap.Field(field))
}
for _, field := range fields {
zapFields = append(zapFields, zap.Field(field))
}
return zapFields
}
func zapFields(parent, local []Field, additional ...Field) []zap.Field {
zapFields := make([]zap.Field, 0, len(parent)+len(local)+len(additional))
for _, field := range additional {
zapFields = append(zapFields, zap.Field(field))
}
for _, field := range parent {
zapFields = append(zapFields, zap.Field(field))
}
for _, field := range local {
zapFields = append(zapFields, zap.Field(field))
}
return zapFields
}