forked from hyperledger-archives/burrow
/
sinks.go
386 lines (341 loc) · 10.6 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
package config
import (
"fmt"
"os"
"net/url"
"github.com/eapache/channels"
kitlog "github.com/go-kit/kit/log"
"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 = ""
Graylog outputType = "graylog"
Syslog outputType = "syslog"
File outputType = "file"
Stdout outputType = "stdout"
Stderr outputType = "stderr"
// 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"
// 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
IncludeWhenAllMatch filterMode = "include_when_all_match"
IncludeWhenAnyMatches filterMode = "include_when_any_matches"
ExcludeWhenAllMatch filterMode = "exclude_when_all_match"
ExcludeWhenAnyMatches filterMode = "exclude_when_any_matches"
)
// 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.MatchAny()
}
// Sink configuration types
type (
// Outputs
GraylogConfig struct {
}
SyslogConfig struct {
Url string `toml:"url"`
Tag string `toml:"tag"`
}
FileConfig struct {
Path string `toml:"path"`
}
OutputConfig struct {
OutputType outputType `toml:"output_type"`
Format string `toml:"format,omitempty"`
*GraylogConfig
*FileConfig
*SyslogConfig
}
// Transforms
LabelConfig struct {
Labels map[string]string `toml:"labels"`
Prefix bool `toml:"prefix"`
}
PruneConfig struct {
Keys []string `toml:"keys"`
}
CaptureConfig struct {
Name string `toml:"name"`
BufferCap int `toml:"buffer_cap"`
Passthrough bool `toml:"passthrough"`
}
// 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 `toml:"key_regex"`
ValueRegex string `toml:"value_regex"`
}
// Filter types
FilterConfig struct {
FilterMode filterMode `toml:"filter_mode"`
// Predicates to match a log line against using FilterMode
Predicates []*KeyValuePredicateConfig `toml:"predicates"`
}
TransformConfig struct {
TransformType transformType `toml:"transform_type"`
*LabelConfig
*PruneConfig
*CaptureConfig
*FilterConfig
}
// 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 `toml:"transform"`
Sinks []*SinkConfig `toml:"sinks"`
Output *OutputConfig `toml:"output"`
}
)
// Builders
func Sink() *SinkConfig {
return &SinkConfig{}
}
func (sinkConfig *SinkConfig) AddSinks(sinks ...*SinkConfig) *SinkConfig {
sinkConfig.Sinks = append(sinkConfig.Sinks, sinks...)
return sinkConfig
}
func (sinkConfig *SinkConfig) SetTransform(transform *TransformConfig) *SinkConfig {
sinkConfig.Transform = transform
return sinkConfig
}
func (sinkConfig *SinkConfig) SetOutput(output *OutputConfig) *SinkConfig {
sinkConfig.Output = output
return sinkConfig
}
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 SyslogOutput(tag string) *OutputConfig {
return RemoteSyslogOutput(tag, "")
}
func FileOutput(path string) *OutputConfig {
return &OutputConfig{
OutputType: File,
FileConfig: &FileConfig{
Path: path,
},
}
}
func RemoteSyslogOutput(tag, remoteUrl string) *OutputConfig {
return &OutputConfig{
OutputType: Syslog,
SyslogConfig: &SyslogConfig{
Url: remoteUrl,
Tag: tag,
},
}
}
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 PruneTransform(keys ...string) *TransformConfig {
return &TransformConfig{
TransformType: Prune,
PruneConfig: &PruneConfig{
Keys: keys,
},
}
}
func FilterTransform(fmode filterMode, keyvalueRegexes ...string) *TransformConfig {
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,
},
}
}
// Logger formation
func (sinkConfig *SinkConfig) BuildLogger() (kitlog.Logger, map[string]*loggers.CaptureLogger, error) {
return BuildLoggerFromSinkConfig(sinkConfig, make(map[string]*loggers.CaptureLogger))
}
func BuildLoggerFromSinkConfig(sinkConfig *SinkConfig,
captures map[string]*loggers.CaptureLogger) (kitlog.Logger, map[string]*loggers.CaptureLogger, error) {
if sinkConfig == nil {
return kitlog.NewNopLogger(), captures, nil
}
numSinks := len(sinkConfig.Sinks)
outputLoggers := make([]kitlog.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 {
l, captures, err := BuildLoggerFromSinkConfig(sc, captures)
if err != nil {
return nil, captures, err
}
outputLoggers[i] = l
}
// 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 BuildTransformLogger(sinkConfig.Transform, captures, outputLogger)
}
return outputLogger, captures, nil
}
func BuildOutputLogger(outputConfig *OutputConfig) (kitlog.Logger, error) {
switch outputConfig.OutputType {
case NoOutput:
return kitlog.NewNopLogger(), nil
//case Graylog:
case Syslog:
urlString := outputConfig.SyslogConfig.Url
if urlString != "" {
remoteUrl, err := url.Parse(urlString)
if err != nil {
return nil, fmt.Errorf("Error parsing remote syslog URL: %s, "+
"error: %s",
urlString, err)
}
return loggers.NewRemoteSyslogLogger(remoteUrl,
outputConfig.SyslogConfig.Tag, outputConfig.Format)
}
return loggers.NewSyslogLogger(outputConfig.SyslogConfig.Tag,
outputConfig.Format)
case Stdout:
return loggers.NewStreamLogger(os.Stdout, outputConfig.Format), nil
case Stderr:
return loggers.NewStreamLogger(os.Stderr, outputConfig.Format), nil
case File:
return loggers.NewFileLogger(outputConfig.FileConfig.Path, outputConfig.Format)
default:
return nil, fmt.Errorf("Could not build logger for output: '%s'",
outputConfig.OutputType)
}
}
func BuildTransformLogger(transformConfig *TransformConfig,
captures map[string]*loggers.CaptureLogger,
outputLogger kitlog.Logger) (kitlog.Logger, map[string]*loggers.CaptureLogger, error) {
switch transformConfig.TransformType {
case NoTransform:
return outputLogger, captures, nil
case Label:
keyvals := make([]interface{}, 0, len(transformConfig.Labels)*2)
for k, v := range transformConfig.LabelConfig.Labels {
keyvals = append(keyvals, k, v)
}
if transformConfig.LabelConfig.Prefix {
return kitlog.WithPrefix(outputLogger, keyvals...), captures, nil
} else {
return kitlog.With(outputLogger, keyvals...), captures, nil
}
case Prune:
keys := make([]interface{}, len(transformConfig.PruneConfig.Keys))
for i, k := range transformConfig.PruneConfig.Keys {
keys[i] = k
}
return kitlog.LoggerFunc(func(keyvals ...interface{}) error {
return outputLogger.Log(structure.RemoveKeys(keyvals, keys...)...)
}), captures, nil
case Capture:
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:
predicate, err := BuildFilterPredicate(transformConfig.FilterConfig)
if err != nil {
return nil, captures, fmt.Errorf("Could not build filter predicate: '%s'", err)
}
return loggers.NewFilterLogger(outputLogger, predicate), captures, nil
default:
return nil, captures, fmt.Errorf("Could not build logger for transform: '%s'", transformConfig.TransformType)
}
}