1 Star 0 Fork 0

sqos / beats

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
factory.go 3.11 KB
一键复制 编辑 原始数据 按行查看 历史
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)
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
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/sqos/beats.git
git@gitee.com:sqos/beats.git
sqos
beats
beats
v6.0.0-beta1

搜索帮助

344bd9b3 5694891 D2dac590 5694891