Skip to content

Commit ac8f23d

Browse files
committed
event_to_metric: add do_if
1 parent 1a7ab96 commit ac8f23d

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

plugin/action/event_to_metrics/event_to_metrics.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/ozontech/file.d/cfg"
77
"github.com/ozontech/file.d/fd"
88
"github.com/ozontech/file.d/pipeline"
9+
"github.com/ozontech/file.d/pipeline/doif"
910
"github.com/ozontech/file.d/xtime"
1011
insaneJSON "github.com/ozontech/insane-json"
1112
"go.uber.org/zap"
@@ -47,6 +48,11 @@ type Metric struct {
4748
Type string `json:"type"`
4849
Value string `json:"value"`
4950
Labels map[string]string `json:"labels"`
51+
52+
DoIfCheckerMap map[string]any `json:"do_if"`
53+
DoIfChecker *doif.Checker
54+
55+
use bool
5056
}
5157

5258
func init() {
@@ -64,6 +70,7 @@ func (p *Plugin) Start(config pipeline.AnyConfig, params *pipeline.ActionPluginP
6470
p.config = config.(*Config)
6571
p.logger = params.Logger.Desugar()
6672
p.pluginController = params.Controller
73+
p.config.Metrics = prepareCheckersForMetrics(p.config.Metrics, p.logger)
6774

6875
format, err := xtime.ParseFormatName(p.config.TimeFieldFormat)
6976
if err != nil {
@@ -72,10 +79,33 @@ func (p *Plugin) Start(config pipeline.AnyConfig, params *pipeline.ActionPluginP
7279
p.format = format
7380
}
7481

82+
func prepareCheckersForMetrics(metrics []Metric, logger *zap.Logger) []Metric {
83+
for i := range metrics {
84+
m := &metrics[i]
85+
if m.DoIfCheckerMap != nil {
86+
var err error
87+
m.DoIfChecker, err = doif.NewFromMap(m.DoIfCheckerMap)
88+
if err != nil {
89+
logger.Fatal("can't init do_if for mask", zap.Error(err))
90+
}
91+
} else {
92+
m.use = true
93+
}
94+
}
95+
96+
return metrics
97+
}
98+
7599
func (p *Plugin) Stop() {
76100
}
77101

78102
func (p *Plugin) Do(event *pipeline.Event) pipeline.ActionResult {
103+
for i := range p.config.Metrics {
104+
if p.config.Metrics[i].DoIfChecker != nil {
105+
p.config.Metrics[i].use = p.config.Metrics[i].DoIfChecker.Check(event.Root)
106+
}
107+
}
108+
79109
var ts time.Time
80110

81111
if len(p.config.TimeField_) != 0 {
@@ -98,6 +128,10 @@ func (p *Plugin) Do(event *pipeline.Event) pipeline.ActionResult {
98128

99129
children := make([]*insaneJSON.Node, 0, len(p.config.Metrics))
100130
for _, metric := range p.config.Metrics {
131+
if !metric.use {
132+
continue
133+
}
134+
101135
elem := new(insaneJSON.Node)
102136
object := elem.MutateToObject()
103137

@@ -127,7 +161,7 @@ func (p *Plugin) Do(event *pipeline.Event) pipeline.ActionResult {
127161

128162
if len(children) == 0 {
129163
// zero array or an array that does not contain objects
130-
return pipeline.ActionPass
164+
return pipeline.ActionDiscard
131165
}
132166

133167
p.pluginController.Spawn(event, children)

0 commit comments

Comments
 (0)