1 Star 0 Fork 0

sqos/beats

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
drop_fields.go 1.28 KB
一键复制 编辑 原始数据 按行查看 历史
package actions
import (
"fmt"
"strings"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/processors"
)
type dropFields struct {
Fields []string
}
func init() {
processors.RegisterPlugin("drop_fields",
configChecked(newDropFields,
requireFields("fields"),
allowedFields("fields", "when")))
}
func newDropFields(c common.Config) (processors.Processor, error) {
config := struct {
Fields []string `config:"fields"`
}{}
err := c.Unpack(&config)
if err != nil {
return nil, fmt.Errorf("fail to unpack the drop_fields configuration: %s", err)
}
/* remove read only fields */
for _, readOnly := range processors.MandatoryExportedFields {
for i, field := range config.Fields {
if readOnly == field {
config.Fields = append(config.Fields[:i], config.Fields[i+1:]...)
}
}
}
f := dropFields{Fields: config.Fields}
return f, nil
}
func (f dropFields) Run(event common.MapStr) (common.MapStr, error) {
var errors []string
for _, field := range f.Fields {
err := event.Delete(field)
if err != nil {
errors = append(errors, err.Error())
}
}
if len(errors) > 0 {
return event, fmt.Errorf(strings.Join(errors, ", "))
}
return event, nil
}
func (f dropFields) String() string {
return "drop_fields=" + strings.Join(f.Fields, ", ")
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/sqos/beats.git
git@gitee.com:sqos/beats.git
sqos
beats
beats
v5.3.0

搜索帮助