-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathbuilder.go
More file actions
102 lines (87 loc) · 2.92 KB
/
builder.go
File metadata and controls
102 lines (87 loc) · 2.92 KB
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
package agent
import (
"time"
"github.com/observiq/stanza/database"
"github.com/observiq/stanza/errors"
"github.com/observiq/stanza/operator"
"github.com/observiq/stanza/plugin"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// LogAgentBuilder is a construct used to build a log agent
type LogAgentBuilder struct {
configFiles []string
config *Config
logger *zap.SugaredLogger
pluginDir string
databaseFile string
defaultOutput operator.Operator
}
// NewBuilder creates a new LogAgentBuilder
func NewBuilder(logger *zap.SugaredLogger) *LogAgentBuilder {
return &LogAgentBuilder{
logger: logger,
}
}
// WithPluginDir adds the specified plugin directory when building a log agent
func (b *LogAgentBuilder) WithPluginDir(pluginDir string) *LogAgentBuilder {
b.pluginDir = pluginDir
return b
}
// WithConfigFiles adds a list of globs to the search path for config files
func (b *LogAgentBuilder) WithConfigFiles(files []string) *LogAgentBuilder {
b.configFiles = files
return b
}
// WithConfig builds the agent with a given, pre-built config
func (b *LogAgentBuilder) WithConfig(cfg *Config) *LogAgentBuilder {
b.config = cfg
return b
}
// WithDatabaseFile adds the specified database file when building a log agent
func (b *LogAgentBuilder) WithDatabaseFile(databaseFile string) *LogAgentBuilder {
b.databaseFile = databaseFile
return b
}
// WithDefaultOutput adds a default output when building a log agent
func (b *LogAgentBuilder) WithDefaultOutput(defaultOutput operator.Operator) *LogAgentBuilder {
b.defaultOutput = defaultOutput
return b
}
// Build will build a new log agent using the values defined on the builder
func (b *LogAgentBuilder) Build() (*LogAgent, error) {
db, err := database.OpenDatabase(b.databaseFile)
if err != nil {
return nil, errors.Wrap(err, "open database")
}
if b.pluginDir != "" {
if errs := plugin.RegisterPlugins(b.pluginDir, operator.DefaultRegistry); len(errs) != 0 {
b.logger.Errorw("Got errors parsing plugins", "errors", errs)
}
}
if b.config != nil && len(b.configFiles) > 0 {
return nil, errors.NewError("agent can be built WithConfig or WithConfigFiles, but not both", "")
} else if b.config == nil && len(b.configFiles) == 0 {
return nil, errors.NewError("agent cannot be built without WithConfig or WithConfigFiles", "")
} else if len(b.configFiles) > 0 {
b.config, err = NewConfigFromGlobs(b.configFiles)
if err != nil {
return nil, errors.Wrap(err, "read configs from globs")
}
}
sampledLogger := b.logger.Desugar().WithOptions(
zap.WrapCore(func(core zapcore.Core) zapcore.Core {
return zapcore.NewSamplerWithOptions(core, time.Second, 1, 10000)
}),
).Sugar()
buildContext := operator.NewBuildContext(db, sampledLogger)
pipeline, err := b.config.Pipeline.BuildPipeline(buildContext, b.defaultOutput)
if err != nil {
return nil, err
}
return &LogAgent{
pipeline: pipeline,
database: db,
SugaredLogger: b.logger,
}, nil
}