6
6
"github.com/ozontech/file.d/cfg"
7
7
"github.com/ozontech/file.d/fd"
8
8
"github.com/ozontech/file.d/pipeline"
9
+ "github.com/ozontech/file.d/pipeline/doif"
9
10
"github.com/ozontech/file.d/xtime"
10
11
insaneJSON "github.com/ozontech/insane-json"
11
12
"go.uber.org/zap"
@@ -47,6 +48,11 @@ type Metric struct {
47
48
Type string `json:"type"`
48
49
Value string `json:"value"`
49
50
Labels map [string ]string `json:"labels"`
51
+
52
+ DoIfCheckerMap map [string ]any `json:"do_if"`
53
+ DoIfChecker * doif.Checker
54
+
55
+ use bool
50
56
}
51
57
52
58
func init () {
@@ -64,6 +70,7 @@ func (p *Plugin) Start(config pipeline.AnyConfig, params *pipeline.ActionPluginP
64
70
p .config = config .(* Config )
65
71
p .logger = params .Logger .Desugar ()
66
72
p .pluginController = params .Controller
73
+ p .config .Metrics = prepareCheckersForMetrics (p .config .Metrics , p .logger )
67
74
68
75
format , err := xtime .ParseFormatName (p .config .TimeFieldFormat )
69
76
if err != nil {
@@ -72,10 +79,33 @@ func (p *Plugin) Start(config pipeline.AnyConfig, params *pipeline.ActionPluginP
72
79
p .format = format
73
80
}
74
81
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
+
75
99
func (p * Plugin ) Stop () {
76
100
}
77
101
78
102
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
+
79
109
var ts time.Time
80
110
81
111
if len (p .config .TimeField_ ) != 0 {
@@ -98,6 +128,10 @@ func (p *Plugin) Do(event *pipeline.Event) pipeline.ActionResult {
98
128
99
129
children := make ([]* insaneJSON.Node , 0 , len (p .config .Metrics ))
100
130
for _ , metric := range p .config .Metrics {
131
+ if ! metric .use {
132
+ continue
133
+ }
134
+
101
135
elem := new (insaneJSON.Node )
102
136
object := elem .MutateToObject ()
103
137
@@ -127,7 +161,7 @@ func (p *Plugin) Do(event *pipeline.Event) pipeline.ActionResult {
127
161
128
162
if len (children ) == 0 {
129
163
// zero array or an array that does not contain objects
130
- return pipeline .ActionPass
164
+ return pipeline .ActionDiscard
131
165
}
132
166
133
167
p .pluginController .Spawn (event , children )
0 commit comments