1 Star 0 Fork 0

sqos/beats

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
config.go 2.56 KB
一键复制 编辑 原始数据 按行查看 历史
package template
import (
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/common/bus"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/libbeat/processors"
ucfg "github.com/elastic/go-ucfg"
)
// Mapper maps config templates with conditions, if a match happens on a discover event
// the given template will be used as config
type Mapper []*ConditionMap
// ConditionMap maps a condition to the configs to use when it's triggered
type ConditionMap struct {
Condition *processors.Condition
Configs []*common.Config
}
// MapperSettings holds user settings to build Mapper
type MapperSettings []*struct {
ConditionConfig *processors.ConditionConfig `config:"condition"`
Configs []*common.Config `config:"config"`
}
// NewConfigMapper builds a template Mapper from given settings
func NewConfigMapper(configs MapperSettings) (*Mapper, error) {
var mapper Mapper
for _, c := range configs {
condition, err := processors.NewCondition(c.ConditionConfig)
if err != nil {
return nil, err
}
mapper = append(mapper, &ConditionMap{
Condition: condition,
Configs: c.Configs,
})
}
return &mapper, nil
}
// Event adapts MapStr to processors.ValuesMap interface
type Event common.MapStr
// GetValue extracts given key from an Event
func (e Event) GetValue(key string) (interface{}, error) {
val, err := common.MapStr(e).GetValue(key)
if err != nil {
return nil, err
}
return val, nil
}
// GetConfig returns a matching Config if any, nil otherwise
func (c *Mapper) GetConfig(event bus.Event) []*common.Config {
var result []*common.Config
for _, mapping := range *c {
if mapping.Configs != nil && !mapping.Condition.Check(Event(event)) {
continue
}
// unpack input
vars, err := ucfg.NewFrom(map[string]interface{}{
"data": event,
})
if err != nil {
logp.Err("Error building config: %v", err)
}
opts := []ucfg.Option{
ucfg.PathSep("."),
ucfg.Env(vars),
ucfg.ResolveEnv,
ucfg.VarExp,
}
for _, config := range mapping.Configs {
c, err := ucfg.NewFrom(config, opts...)
if err != nil {
logp.Err("Error parsing config: %v", err)
continue
}
// Unpack config to process any vars in the template:
var unpacked map[string]interface{}
c.Unpack(&unpacked, opts...)
if err != nil {
logp.Err("Error unpacking config: %v", err)
continue
}
// Repack again:
res, err := common.NewConfigFrom(unpacked)
if err != nil {
logp.Err("Error creating config from unpack: %v", err)
continue
}
result = append(result, res)
}
}
return result
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/sqos/beats.git
git@gitee.com:sqos/beats.git
sqos
beats
beats
v6.2.4

搜索帮助