This repository has been archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 346
/
sinks.go
478 lines (419 loc) · 13.5 KB
/
sinks.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
package logconfig
import (
"fmt"
"os"
"github.com/eapache/channels"
"github.com/go-kit/kit/log"
"github.com/hyperledger/burrow/logging"
"github.com/hyperledger/burrow/logging/loggers"
"github.com/hyperledger/burrow/logging/structure"
)
// This file contains definitions for a configurable output graph for the
// logging system.
type outputType string
type transformType string
type filterMode string
const (
// OutputType
NoOutput outputType = ""
Stdout outputType = "stdout"
Stderr outputType = "stderr"
File outputType = "file"
// TransformType
NoTransform transformType = ""
// Filter log lines
Filter transformType = "filter"
// Remove key-val pairs from each log line
Prune transformType = "prune"
// Add key value pairs to each log line
Label transformType = "label"
Capture transformType = "capture"
Sort transformType = "sort"
Vectorise transformType = "vectorise"
// TODO [Silas]: add 'flush on exit' transform which flushes the buffer of
// CaptureLogger to its OutputLogger a non-passthrough capture when an exit
// signal is detected or some other exceptional thing happens
NoFilterMode filterMode = ""
IncludeWhenAllMatch filterMode = "include_when_all_match"
IncludeWhenAnyMatches filterMode = "include_when_any_match"
ExcludeWhenAllMatch filterMode = "exclude_when_all_match"
ExcludeWhenAnyMatches filterMode = "exclude_when_any_match"
)
// Only include log lines matching the filter so negate the predicate in filter
func (mode filterMode) Include() bool {
switch mode {
case IncludeWhenAllMatch, IncludeWhenAnyMatches:
return true
default:
return false
}
}
// The predicate should only evaluate true if all the key value predicates match
func (mode filterMode) MatchAll() bool {
switch mode {
case IncludeWhenAllMatch, ExcludeWhenAllMatch:
return true
default:
return false
}
}
// Exclude log lines that match the predicate
func (mode filterMode) Exclude() bool {
return !mode.Include()
}
// The predicate should evaluate true if at least one of the key value predicates matches
func (mode filterMode) MatchAny() bool {
return !mode.MatchAll()
}
// Sink configuration types
type (
// Outputs
// TODO: reintroduce syslog removed when we dropped log15 dependency
SyslogConfig struct {
Url string
Tag string
}
FileConfig struct {
Path string
}
OutputConfig struct {
OutputType outputType
Format string
*FileConfig `json:",omitempty" toml:",omitempty"`
*SyslogConfig `json:",omitempty" toml:",omitempty"`
}
// Transforms
LabelConfig struct {
Labels map[string]string
Prefix bool
}
PruneConfig struct {
Keys []string
IncludeKeys bool
}
CaptureConfig struct {
Name string
BufferCap int
Passthrough bool
}
// Generates true if KeyRegex matches a log line key and ValueRegex matches that key's value.
// If ValueRegex is empty then returns true if any key matches
// If KeyRegex is empty then returns true if any value matches
KeyValuePredicateConfig struct {
KeyRegex string
ValueRegex string
}
// Filter types
FilterConfig struct {
FilterMode filterMode
// Predicates to match a log line against using FilterMode
Predicates []*KeyValuePredicateConfig
}
SortConfig struct {
// Sort keys-values with keys in this list first
Keys []string
}
TransformConfig struct {
TransformType transformType
LabelConfig *LabelConfig `json:",omitempty" toml:",omitempty"`
PruneConfig *PruneConfig `json:",omitempty" toml:",omitempty"`
CaptureConfig *CaptureConfig `json:",omitempty" toml:",omitempty"`
FilterConfig *FilterConfig `json:",omitempty" toml:",omitempty"`
SortConfig *SortConfig `json:",omitempty" toml:",omitempty"`
}
// Sink
// A Sink describes a logger that logs to zero or one output and logs to zero or more child sinks.
// before transmitting its log it applies zero or one transforms to the stream of log lines.
// by chaining together many Sinks arbitrary transforms to and multi
SinkConfig struct {
Transform *TransformConfig `json:",omitempty" toml:",omitempty"`
Sinks []*SinkConfig `json:",omitempty" toml:",omitempty"`
Output *OutputConfig `json:",omitempty" toml:",omitempty"`
}
)
// Builders
func Sink() *SinkConfig {
return &SinkConfig{}
}
func (sc *SinkConfig) MustLogger() *logging.Logger {
return sc.LoggingConfig().MustLogger()
}
func (sc *SinkConfig) Logger() (*logging.Logger, error) {
return sc.LoggingConfig().Logger()
}
// Wrap this sink as RootSink of LoggingCOnfig
func (sc *SinkConfig) LoggingConfig() *LoggingConfig {
lc := New()
lc.RootSink = sc
return lc
}
func (sc *SinkConfig) AddSinks(sinks ...*SinkConfig) *SinkConfig {
sc.Sinks = append(sc.Sinks, sinks...)
return sc
}
func (sc *SinkConfig) SetTransform(transform *TransformConfig) *SinkConfig {
sc.Transform = transform
return sc
}
func (sc *SinkConfig) FilterScope(scope string) *SinkConfig {
return sc.SetTransform(FilterTransform(IncludeWhenAllMatch, structure.ScopeKey, scope))
}
func (sc *SinkConfig) Terminal() *SinkConfig {
return sc.SetOutput(StderrOutput().SetFormat(TerminalFormat))
}
func (sc *SinkConfig) SetOutput(output *OutputConfig) *SinkConfig {
sc.Output = output
return sc
}
func (outputConfig *OutputConfig) SetFormat(format string) *OutputConfig {
outputConfig.Format = format
return outputConfig
}
func StdoutOutput() *OutputConfig {
return &OutputConfig{
OutputType: Stdout,
}
}
func StderrOutput() *OutputConfig {
return &OutputConfig{
OutputType: Stderr,
}
}
func FileOutput(path string) *OutputConfig {
return &OutputConfig{
OutputType: File,
FileConfig: &FileConfig{
Path: path,
},
}
}
// Transforms
func CaptureTransform(name string, bufferCap int, passthrough bool) *TransformConfig {
return &TransformConfig{
TransformType: Capture,
CaptureConfig: &CaptureConfig{
Name: name,
BufferCap: bufferCap,
Passthrough: passthrough,
},
}
}
func LabelTransform(prefix bool, labelKeyvals ...string) *TransformConfig {
length := len(labelKeyvals) / 2
labels := make(map[string]string, length)
for i := 0; i < 2*length; i += 2 {
labels[labelKeyvals[i]] = labelKeyvals[i+1]
}
return &TransformConfig{
TransformType: Label,
LabelConfig: &LabelConfig{
Prefix: prefix,
Labels: labels,
},
}
}
func OnlyTransform(keys ...string) *TransformConfig {
return &TransformConfig{
TransformType: Prune,
PruneConfig: &PruneConfig{
Keys: keys,
IncludeKeys: true,
},
}
}
func PruneTransform(keys ...string) *TransformConfig {
return &TransformConfig{
TransformType: Prune,
PruneConfig: &PruneConfig{
Keys: keys,
},
}
}
func FilterTransform(fmode filterMode, keyValueRegexes ...string) *TransformConfig {
if len(keyValueRegexes)%2 == 1 {
keyValueRegexes = append(keyValueRegexes, "")
}
length := len(keyValueRegexes) / 2
predicates := make([]*KeyValuePredicateConfig, length)
for i := 0; i < length; i++ {
kv := i * 2
predicates[i] = &KeyValuePredicateConfig{
KeyRegex: keyValueRegexes[kv],
ValueRegex: keyValueRegexes[kv+1],
}
}
return &TransformConfig{
TransformType: Filter,
FilterConfig: &FilterConfig{
FilterMode: fmode,
Predicates: predicates,
},
}
}
func (filterConfig *FilterConfig) SetFilterMode(filterMode filterMode) *FilterConfig {
filterConfig.FilterMode = filterMode
return filterConfig
}
func (filterConfig *FilterConfig) AddPredicate(keyRegex, valueRegex string) *FilterConfig {
filterConfig.Predicates = append(filterConfig.Predicates, &KeyValuePredicateConfig{
KeyRegex: keyRegex,
ValueRegex: valueRegex,
})
return filterConfig
}
func SortTransform(keys ...string) *TransformConfig {
return &TransformConfig{
TransformType: Sort,
SortConfig: &SortConfig{
Keys: keys,
},
}
}
func VectoriseTransform() *TransformConfig {
return &TransformConfig{
TransformType: Vectorise,
}
}
// Logger formation
func (sc *SinkConfig) BuildLogger() (log.Logger, map[string]*loggers.CaptureLogger, error) {
if sc == nil {
return log.NewNopLogger(), nil, nil
}
return BuildLoggerFromSinkConfig(sc, make(map[string]*loggers.CaptureLogger))
}
func BuildLoggerFromSinkConfig(sinkConfig *SinkConfig, captures map[string]*loggers.CaptureLogger) (log.Logger,
map[string]*loggers.CaptureLogger, error) {
if sinkConfig == nil {
return log.NewNopLogger(), captures, nil
}
numSinks := len(sinkConfig.Sinks)
outputLoggers := make([]log.Logger, numSinks, numSinks+1)
// We need a depth-first post-order over the output loggers so we'll keep
// recurring into children sinks we reach a terminal sink (with no children)
for i, sc := range sinkConfig.Sinks {
var err error
outputLoggers[i], captures, err = BuildLoggerFromSinkConfig(sc, captures)
if err != nil {
return nil, nil, err
}
}
// Grab the outputs after we have terminated any children sinks above
if sinkConfig.Output != nil && sinkConfig.Output.OutputType != NoOutput {
l, err := BuildOutputLogger(sinkConfig.Output)
if err != nil {
return nil, captures, err
}
outputLoggers = append(outputLoggers, l)
}
outputLogger := loggers.NewMultipleOutputLogger(outputLoggers...)
if sinkConfig.Transform != nil && sinkConfig.Transform.TransformType != NoTransform {
return BuildTransformLoggerPassthrough(sinkConfig.Transform, captures, outputLogger)
}
return outputLogger, captures, nil
}
func BuildOutputLogger(outputConfig *OutputConfig) (log.Logger, error) {
switch outputConfig.OutputType {
case NoOutput:
return log.NewNopLogger(), nil
case File:
return loggers.NewFileLogger(outputConfig.FileConfig.Path, outputConfig.Format)
case Stdout:
return loggers.NewStreamLogger(os.Stdout, outputConfig.Format)
case Stderr:
return loggers.NewStreamLogger(os.Stderr, outputConfig.Format)
default:
return nil, fmt.Errorf("could not build logger for output: '%s'",
outputConfig.OutputType)
}
}
func BuildTransformLoggerPassthrough(transformConfig *TransformConfig, captures map[string]*loggers.CaptureLogger,
outputLogger log.Logger) (log.Logger, map[string]*loggers.CaptureLogger, error) {
transformThenOutputLogger, captures, err := BuildTransformLogger(transformConfig, captures, outputLogger)
if err != nil {
return nil, nil, err
}
// send signals through captures so they can be flushed
if transformConfig.TransformType == Capture {
return transformThenOutputLogger, captures, nil
}
return signalPassthroughLogger(outputLogger, transformThenOutputLogger), captures, nil
}
func BuildTransformLogger(transformConfig *TransformConfig, captures map[string]*loggers.CaptureLogger,
outputLogger log.Logger) (log.Logger, map[string]*loggers.CaptureLogger, error) {
switch transformConfig.TransformType {
case NoTransform:
return outputLogger, captures, nil
case Label:
if transformConfig.LabelConfig == nil {
return nil, nil, fmt.Errorf("label transform specified but no LabelConfig provided")
}
keyvals := make([]interface{}, 0, len(transformConfig.LabelConfig.Labels)*2)
for k, v := range transformConfig.LabelConfig.Labels {
keyvals = append(keyvals, k, v)
}
if transformConfig.LabelConfig.Prefix {
return log.WithPrefix(outputLogger, keyvals...), captures, nil
} else {
return log.With(outputLogger, keyvals...), captures, nil
}
case Prune:
if transformConfig.PruneConfig == nil {
return nil, nil, fmt.Errorf("prune transform specified but no PruneConfig provided")
}
keys := make([]interface{}, len(transformConfig.PruneConfig.Keys))
for i, k := range transformConfig.PruneConfig.Keys {
keys[i] = k
}
return log.LoggerFunc(func(keyvals ...interface{}) error {
if transformConfig.PruneConfig.IncludeKeys {
return outputLogger.Log(structure.OnlyKeys(keyvals, keys...)...)
} else {
return outputLogger.Log(structure.RemoveKeys(keyvals, keys...)...)
}
}), captures, nil
case Capture:
if transformConfig.CaptureConfig == nil {
return nil, nil, fmt.Errorf("capture transform specified but no CaptureConfig provided")
}
name := transformConfig.CaptureConfig.Name
if _, ok := captures[name]; ok {
return nil, captures, fmt.Errorf("could not register new logging capture since name '%s' already "+
"registered", name)
}
// Create a capture logger according to configuration (it may tee the output)
// or capture it to be flushed later
captureLogger := loggers.NewCaptureLogger(outputLogger,
channels.BufferCap(transformConfig.CaptureConfig.BufferCap),
transformConfig.CaptureConfig.Passthrough)
// Register the capture
captures[name] = captureLogger
// Pass it upstream to be logged to
return captureLogger, captures, nil
case Filter:
if transformConfig.FilterConfig == nil {
return nil, nil, fmt.Errorf("filter transform specified but no FilterConfig provided")
}
predicate, err := BuildFilterPredicate(transformConfig.FilterConfig)
if err != nil {
return nil, captures, fmt.Errorf("could not build filter predicate: '%s'", err)
}
return loggers.FilterLogger(outputLogger, predicate), captures, nil
case Sort:
if transformConfig.SortConfig == nil {
return nil, nil, fmt.Errorf("sort transform specified but no SortConfig provided")
}
return loggers.SortLogger(outputLogger, transformConfig.SortConfig.Keys...), captures, nil
case Vectorise:
return loggers.VectorValuedLogger(outputLogger), captures, nil
default:
return nil, captures, fmt.Errorf("could not build logger for transform: '%s'", transformConfig.TransformType)
}
}
func signalPassthroughLogger(ifSignalLogger log.Logger, otherwiseLogger log.Logger) log.Logger {
return log.LoggerFunc(func(keyvals ...interface{}) error {
if structure.Signal(keyvals) != "" {
return ifSignalLogger.Log(keyvals...)
}
return otherwiseLogger.Log(keyvals...)
})
}