forked from blendle/zapdriver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.go
243 lines (201 loc) · 6.12 KB
/
core.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
package zapdriver
import (
"strings"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// driverConfig is used to configure core.
type driverConfig struct {
// Report all logs with level error or above to stackdriver using
// `ErrorReport()` when set to true
ReportAllErrors bool
// ServiceName is added as `ServiceContext()` to all logs when set
ServiceName string
}
// Core is a zapdriver specific core wrapped around the default zap core. It
// allows to merge all defined labels
type core struct {
zapcore.Core
// permLabels is a collection of labels that have been added to the logger
// through the use of `With()`. These labels should never be cleared after
// logging a single entry, unlike `tempLabel`.
permLabels *labels
// tempLabels keeps a record of all the labels that need to be applied to the
// current log entry. Zap serializes log fields at different parts of the
// stack, one such location is when calling `core.With` and the other one is
// when calling `core.Write`. This makes it impossible to (for example) take
// all `labels.xxx` fields, and wrap them in the `labels` namespace in one go.
//
// Instead, we have to filter out these labels at both locations, and then add
// them back in the proper format right before we call `Write` on the original
// Zap core.
tempLabels *labels
// Configuration for the zapdriver core
config driverConfig
}
// zapdriver core option to report all logs with level error or above to stackdriver
// using `ErrorReport()` when set to true
func ReportAllErrors(report bool) func(*core) {
return func(c *core) {
c.config.ReportAllErrors = report
}
}
// zapdriver core option to add `ServiceContext()` to all logs with `name` as
// service name
func ServiceName(name string) func(*core) {
return func(c *core) {
c.config.ServiceName = name
}
}
// WrapCore returns a `zap.Option` that wraps the default core with the
// zapdriver one.
func WrapCore(options ...func(*core)) zap.Option {
return zap.WrapCore(func(c zapcore.Core) zapcore.Core {
newcore := &core{
Core: c,
permLabels: newLabels(),
tempLabels: newLabels(),
}
for _, option := range options {
option(newcore)
}
return newcore
})
}
// With adds structured context to the Core.
func (c *core) With(fields []zap.Field) zapcore.Core {
var lbls *labels
lbls, fields = c.extractLabels(fields)
lbls.mutex.RLock()
c.permLabels.mutex.Lock()
for k, v := range lbls.store {
c.permLabels.store[k] = v
}
c.permLabels.mutex.Unlock()
lbls.mutex.RUnlock()
return &core{
Core: c.Core.With(fields),
permLabels: c.permLabels,
tempLabels: newLabels(),
config: c.config,
}
}
// Check determines whether the supplied Entry should be logged (using the
// embedded LevelEnabler and possibly some extra logic). If the entry
// should be logged, the Core adds itself to the CheckedEntry and returns
// the result.
//
// Callers must use Check before calling Write.
func (c *core) Check(ent zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.CheckedEntry {
if c.Enabled(ent.Level) {
return ce.AddCore(ent, c)
}
return ce
}
func (c *core) Write(ent zapcore.Entry, fields []zapcore.Field) error {
var lbls *labels
lbls, fields = c.extractLabels(fields)
lbls.mutex.RLock()
c.tempLabels.mutex.Lock()
for k, v := range lbls.store {
c.tempLabels.store[k] = v
}
c.tempLabels.mutex.Unlock()
lbls.mutex.RUnlock()
fields = append(fields, labelsField(c.allLabels()))
fields = c.withSourceLocation(ent, fields)
if c.config.ServiceName != "" {
fields = c.withServiceContext(c.config.ServiceName, fields)
}
if c.config.ReportAllErrors && zapcore.ErrorLevel.Enabled(ent.Level) {
fields = c.withErrorReport(ent, fields)
if c.config.ServiceName == "" {
// A service name was not set but error report needs it
// So attempt to add a generic service name
fields = c.withServiceContext("unknown", fields)
}
}
c.tempLabels.reset()
return c.Core.Write(ent, fields)
}
// Sync flushes buffered logs (if any).
func (c *core) Sync() error {
return c.Core.Sync()
}
func (c *core) allLabels() *labels {
lbls := newLabels()
lbls.mutex.Lock()
c.permLabels.mutex.RLock()
for k, v := range c.permLabels.store {
lbls.store[k] = v
}
c.permLabels.mutex.RUnlock()
c.tempLabels.mutex.RLock()
for k, v := range c.tempLabels.store {
lbls.store[k] = v
}
c.tempLabels.mutex.RUnlock()
lbls.mutex.Unlock()
return lbls
}
func (c *core) extractLabels(fields []zapcore.Field) (*labels, []zapcore.Field) {
lbls := newLabels()
out := []zapcore.Field{}
lbls.mutex.Lock()
for i := range fields {
if !isLabelField(fields[i]) {
out = append(out, fields[i])
continue
}
lbls.store[strings.Replace(fields[i].Key, "labels.", "", 1)] = fields[i].String
}
lbls.mutex.Unlock()
return lbls, out
}
func (c *core) withLabels(fields []zapcore.Field) []zapcore.Field {
lbls := newLabels()
out := []zapcore.Field{}
lbls.mutex.Lock()
for i := range fields {
if isLabelField(fields[i]) {
lbls.store[strings.Replace(fields[i].Key, "labels.", "", 1)] = fields[i].String
continue
}
out = append(out, fields[i])
}
lbls.mutex.Unlock()
return append(out, labelsField(lbls))
}
func (c *core) withSourceLocation(ent zapcore.Entry, fields []zapcore.Field) []zapcore.Field {
// If the source location was manually set, don't overwrite it
for i := range fields {
if fields[i].Key == sourceKey {
return fields
}
}
if !ent.Caller.Defined {
return fields
}
return append(fields, SourceLocation(ent.Caller.PC, ent.Caller.File, ent.Caller.Line, true))
}
func (c *core) withServiceContext(name string, fields []zapcore.Field) []zapcore.Field {
// If the service context was manually set, don't overwrite it
for i := range fields {
if fields[i].Key == serviceContextKey {
return fields
}
}
return append(fields, ServiceContext(name))
}
func (c *core) withErrorReport(ent zapcore.Entry, fields []zapcore.Field) []zapcore.Field {
// If the error report was manually set, don't overwrite it
for i := range fields {
if fields[i].Key == contextKey {
return fields
}
}
if !ent.Caller.Defined {
return fields
}
return append(fields, ErrorReport(ent.Caller.PC, ent.Caller.File, ent.Caller.Line, true))
}