forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
factory.go
115 lines (102 loc) · 3.12 KB
/
factory.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
package fileset
import (
"github.com/mitchellh/hashstructure"
"github.com/elastic/beats/filebeat/channel"
"github.com/elastic/beats/filebeat/prospector"
"github.com/elastic/beats/filebeat/registrar"
"github.com/elastic/beats/libbeat/cfgfile"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/libbeat/outputs/elasticsearch"
)
// Factory for modules
type Factory struct {
outlet channel.OutleterFactory
registrar *registrar.Registrar
beatVersion string
pipelineLoaderFactory PipelineLoaderFactory
beatDone chan struct{}
}
// Wrap an array of prospectors and implements cfgfile.Runner interface
type prospectorsRunner struct {
id uint64
moduleRegistry *ModuleRegistry
prospectors []*prospector.Prospector
pipelineLoaderFactory PipelineLoaderFactory
}
// NewFactory instantiates a new Factory
func NewFactory(outlet channel.OutleterFactory, registrar *registrar.Registrar, beatVersion string,
pipelineLoaderFactory PipelineLoaderFactory, beatDone chan struct{}) *Factory {
return &Factory{
outlet: outlet,
registrar: registrar,
beatVersion: beatVersion,
beatDone: beatDone,
pipelineLoaderFactory: pipelineLoaderFactory,
}
}
// Create creates a module based on a config
func (f *Factory) Create(c *common.Config) (cfgfile.Runner, error) {
// Start a registry of one module:
m, err := NewModuleRegistry([]*common.Config{c}, f.beatVersion, false)
if err != nil {
return nil, err
}
pConfigs, err := m.GetProspectorConfigs()
if err != nil {
return nil, err
}
// Hash module ID
var h map[string]interface{}
c.Unpack(&h)
id, err := hashstructure.Hash(h, nil)
if err != nil {
return nil, err
}
prospectors := make([]*prospector.Prospector, len(pConfigs))
for i, pConfig := range pConfigs {
prospectors[i], err = prospector.NewProspector(pConfig, f.outlet, f.beatDone, f.registrar.GetStates())
if err != nil {
logp.Err("Error creating prospector: %s", err)
return nil, err
}
}
return &prospectorsRunner{
id: id,
moduleRegistry: m,
prospectors: prospectors,
pipelineLoaderFactory: f.pipelineLoaderFactory,
}, nil
}
func (p *prospectorsRunner) Start() {
// Load pipelines
if p.pipelineLoaderFactory != nil {
// Load pipelines instantly and then setup a callback for reconnections:
pipelineLoader, err := p.pipelineLoaderFactory()
if err != nil {
logp.Err("Error loading pipeline: %s", err)
} else {
err := p.moduleRegistry.LoadPipelines(pipelineLoader)
if err != nil {
// Log error and continue
logp.Err("Error loading pipeline: %s", err)
}
}
// Callback:
callback := func(esClient *elasticsearch.Client) error {
return p.moduleRegistry.LoadPipelines(esClient)
}
elasticsearch.RegisterConnectCallback(callback)
}
for _, prospector := range p.prospectors {
prospector.Start()
}
}
func (p *prospectorsRunner) Stop() {
for _, prospector := range p.prospectors {
prospector.Stop()
}
}
func (p *prospectorsRunner) ID() uint64 {
return p.id
}