-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathdefault.go
390 lines (336 loc) · 11.3 KB
/
default.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package processing
import (
"fmt"
"github.com/elastic/beats/v7/libbeat/asset"
"github.com/elastic/beats/v7/libbeat/beat"
"github.com/elastic/beats/v7/libbeat/common"
"github.com/elastic/beats/v7/libbeat/ecs"
"github.com/elastic/beats/v7/libbeat/logp"
"github.com/elastic/beats/v7/libbeat/mapping"
"github.com/elastic/beats/v7/libbeat/processors"
"github.com/elastic/beats/v7/libbeat/processors/actions"
"github.com/elastic/beats/v7/libbeat/processors/timeseries"
)
// builder is used to create the event processing pipeline in Beats. The
// builder orders and merges global and local (per client) event annotation
// settings, with the configured event processors into one common event
// processor for use with the publisher pipeline.
// Also See: (*builder).Create
type builder struct {
info beat.Info
log *logp.Logger
skipNormalize bool
// global pipeline fields and tags configurations
modifiers []modifier
builtinMeta common.MapStr
fields common.MapStr
tags []string
// Time series id will be calculated for Events with the TimeSeries flag if this
// is enabled (disabled by default)
timeSeries bool
timeseriesFields mapping.Fields
// global pipeline processors
processors *group
drop bool // disabled is set if outputs have been disabled via CLI
alwaysCopy bool
}
type modifier interface {
// BuiltinFields defines global fields to be added to every event.
BuiltinFields(beat.Info) common.MapStr
// ClientFields defines connection local fields to be added to each event
// of a pipeline client.
ClientFields(beat.Info, beat.ProcessingConfig) common.MapStr
}
type builtinModifier func(beat.Info) common.MapStr
// MakeDefaultBeatSupport creates a new SupportFactory based on NewDefaultSupport.
// MakeDefaultBeatSupport automatically adds the `ecs.version`, `host.name` and `agent.X` fields
// to each event.
func MakeDefaultBeatSupport(normalize bool) SupportFactory {
return MakeDefaultSupport(normalize, WithECS, WithHost, WithAgentMeta())
}
// MakeDefaultObserverSupport creates a new SupportFactory based on NewDefaultSupport.
// MakeDefaultObserverSupport automatically adds the `ecs.version` and `observer.X` fields
// to each event.
func MakeDefaultObserverSupport(normalize bool) SupportFactory {
return MakeDefaultSupport(normalize, WithECS, WithObserverMeta())
}
// MakeDefaultSupport creates a new SupportFactory for use with the publisher pipeline.
// If normalize is set, events will be normalized first before being presented
// to the actual processors.
// The Supporter will apply the global `fields`, `fields_under_root`, `tags`
// and `processor` settings to the event processing pipeline to be generated.
// Use WithFields, WithBeatMeta, and other to declare the builtin fields to be added
// to each event. Builtin fields can be modified using global `processors`, and `fields` only.
func MakeDefaultSupport(
normalize bool,
modifiers ...modifier,
) SupportFactory {
return func(info beat.Info, log *logp.Logger, beatCfg *common.Config) (Supporter, error) {
cfg := struct {
common.EventMetadata `config:",inline"` // Fields and tags to add to each event.
Processors processors.PluginConfig `config:"processors"`
TimeSeries bool `config:"timeseries.enabled"`
}{}
if err := beatCfg.Unpack(&cfg); err != nil {
return nil, err
}
processors, err := processors.New(cfg.Processors)
if err != nil {
return nil, fmt.Errorf("error initializing processors: %v", err)
}
return newBuilder(info, log, processors, cfg.EventMetadata, modifiers, !normalize, cfg.TimeSeries)
}
}
// WithFields creates a modifier with the given default builtin fields.
func WithFields(fields common.MapStr) modifier {
return builtinModifier(func(_ beat.Info) common.MapStr {
return fields
})
}
// WithECS modifier adds `ecs.version` builtin fields to a processing pipeline.
var WithECS modifier = WithFields(common.MapStr{
"ecs": common.MapStr{
"version": ecs.Version,
},
})
// WithHost modifier adds `host.name` builtin fields to a processing pipeline
var WithHost modifier = builtinModifier(func(info beat.Info) common.MapStr {
return common.MapStr{
"host": common.MapStr{
"name": info.Name,
},
}
})
// WithAgentMeta adds agent meta information as builtin fields to a processing
// pipeline.
func WithAgentMeta() modifier {
return builtinModifier(func(info beat.Info) common.MapStr {
metadata := common.MapStr{
"ephemeral_id": info.EphemeralID.String(),
"id": info.ID.String(),
"name": info.Hostname,
"type": info.Beat,
"version": info.Version,
}
if info.Name != "" {
metadata["name"] = info.Name
}
return common.MapStr{"agent": metadata}
})
}
// WithObserverMeta adds beat meta information as builtin fields to a processing
// pipeline.
func WithObserverMeta() modifier {
return builtinModifier(func(info beat.Info) common.MapStr {
metadata := common.MapStr{
"type": info.Beat, // Per ECS this is not a valid type value.
"ephemeral_id": info.EphemeralID.String(), // Not in ECS.
"hostname": info.Hostname,
"id": info.ID.String(), // Not in ECS.
"version": info.Version,
}
if info.Name != info.Hostname {
metadata.Put("name", info.Name)
}
return common.MapStr{"observer": metadata}
})
}
func newBuilder(
info beat.Info,
log *logp.Logger,
processors *processors.Processors,
eventMeta common.EventMetadata,
modifiers []modifier,
skipNormalize bool,
timeSeries bool,
) (*builder, error) {
b := &builder{
skipNormalize: skipNormalize,
modifiers: modifiers,
log: log,
info: info,
timeSeries: timeSeries,
}
hasProcessors := processors != nil && len(processors.List) > 0
if hasProcessors {
tmp := newGroup("global", log)
for _, p := range processors.List {
tmp.add(p)
}
b.processors = tmp
}
builtin := common.MapStr{}
for _, mod := range modifiers {
m := mod.BuiltinFields(info)
if len(m) > 0 {
builtin.DeepUpdate(m.Clone())
}
}
if len(builtin) > 0 {
b.builtinMeta = builtin
}
if fields := eventMeta.Fields; len(fields) > 0 {
b.fields = common.MapStr{}
common.MergeFields(b.fields, fields.Clone(), eventMeta.FieldsUnderRoot)
}
if timeSeries {
rawFields, err := asset.GetFields(info.Beat)
if err != nil {
return nil, err
}
fields, err := mapping.LoadFields(rawFields)
if err != nil {
return nil, err
}
b.timeseriesFields = fields
}
if t := eventMeta.Tags; len(t) > 0 {
b.tags = t
}
return b, nil
}
// Create combines the builder configuration with the client settings
// in order to build the event processing pipeline.
//
// Processing order (C=client, P=pipeline)
// 1. (P) generalize/normalize event
// 2. (C) add Meta from client Config to event.Meta
// 3. (C) add Fields from client config to event.Fields
// 4. (P) add pipeline fields + tags
// 5. (C) add client fields + tags
// 6. (C) client processors list
// 7. (P) add builtins
// 8. (P) pipeline processors list
// 9. (P) timeseries mangling
// 10. (P) (if publish/debug enabled) log event
// 11. (P) (if output disabled) dropEvent
func (b *builder) Create(cfg beat.ProcessingConfig, drop bool) (beat.Processor, error) {
var (
// pipeline processors
processors = newGroup("processPipeline", b.log)
// client fields and metadata
clientMeta = cfg.Meta
localProcessors = makeClientProcessors(b.log, cfg)
)
needsCopy := b.alwaysCopy || localProcessors != nil || b.processors != nil
builtin := b.builtinMeta
if cfg.DisableHost {
tmp := builtin.Clone()
tmp.Delete("host")
builtin = tmp
}
var clientFields common.MapStr
for _, mod := range b.modifiers {
m := mod.ClientFields(b.info, cfg)
if len(m) > 0 {
if clientFields == nil {
clientFields = common.MapStr{}
}
clientFields.DeepUpdate(m.Clone())
}
}
if len(clientFields) > 0 {
tmp := builtin.Clone()
tmp.DeepUpdate(clientFields)
builtin = tmp
}
if !b.skipNormalize {
// setup 1: generalize/normalize output (P)
processors.add(newGeneralizeProcessor(cfg.KeepNull))
}
// setup 2: add Meta from client config (C)
if m := clientMeta; len(m) > 0 {
processors.add(clientEventMeta(m, needsCopy))
}
// setup 4, 5: pipeline tags + client tags
var tags []string
tags = append(tags, b.tags...)
tags = append(tags, cfg.EventMetadata.Tags...)
if len(tags) > 0 {
processors.add(actions.NewAddTags("tags", tags))
}
// setup 3, 4, 5: client config fields + pipeline fields + client fields + dyn metadata
fields := cfg.Fields.Clone()
fields.DeepUpdate(b.fields.Clone())
if em := cfg.EventMetadata; len(em.Fields) > 0 {
common.MergeFieldsDeep(fields, em.Fields.Clone(), em.FieldsUnderRoot)
}
if len(fields) > 0 {
// Enforce a copy of fields if dynamic fields are configured or agent
// metadata will be merged into the fields.
// With dynamic fields potentially changing at any time, we need to copy,
// so we do not change shared structures be accident.
fieldsNeedsCopy := needsCopy || cfg.DynamicFields != nil || hasKeyAnyOf(fields, builtin)
processors.add(actions.NewAddFields(fields, fieldsNeedsCopy, true))
}
if cfg.DynamicFields != nil {
checkCopy := func(m common.MapStr) bool {
return needsCopy || hasKeyAnyOf(m, builtin)
}
processors.add(makeAddDynMetaProcessor("dynamicFields", cfg.DynamicFields, checkCopy))
}
// setup 5: client processor list
processors.add(localProcessors)
// setup 6: add beats and host metadata
if meta := builtin; len(meta) > 0 {
processors.add(actions.NewAddFields(meta, needsCopy, false))
}
// setup 8: pipeline processors list
if b.processors != nil {
// Add the global pipeline as a function processor, so clients cannot close it
processors.add(newProcessor(b.processors.title, b.processors.Run))
}
// setup 9: time series metadata
if b.timeSeries {
processors.add(timeseries.NewTimeSeriesProcessor(b.timeseriesFields))
}
// setup 10: debug print final event (P)
if b.log.IsDebug() {
processors.add(debugPrintProcessor(b.info, b.log))
}
// setup 11: drop all events if outputs are disabled (P)
if drop {
processors.add(dropDisabledProcessor)
}
return processors, nil
}
func (b *builder) Close() error {
if b.processors != nil {
return b.processors.Close()
}
return nil
}
func makeClientProcessors(
log *logp.Logger,
cfg beat.ProcessingConfig,
) processors.Processor {
procs := cfg.Processor
if procs == nil || len(procs.All()) == 0 {
return nil
}
p := newGroup("client", log)
p.list = procs.All()
return p
}
func (b builtinModifier) BuiltinFields(info beat.Info) common.MapStr {
return b(info)
}
func (b builtinModifier) ClientFields(_ beat.Info, _ beat.ProcessingConfig) common.MapStr {
return nil
}