-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[metricbeat] [auditbeat] Add formatted index option to metricbeat / auditbeat modules #15100
Changes from 5 commits
05d4a64
bac2b8c
6d0dc1b
0dd03aa
1a9b9ec
bfb772b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,9 @@ package module | |
import ( | ||
"github.com/elastic/beats/libbeat/beat" | ||
"github.com/elastic/beats/libbeat/common" | ||
"github.com/elastic/beats/libbeat/common/fmtstr" | ||
"github.com/elastic/beats/libbeat/processors" | ||
"github.com/elastic/beats/libbeat/processors/add_formatted_index" | ||
) | ||
|
||
// Connector configures and establishes a beat.Client for publishing events | ||
|
@@ -36,20 +38,25 @@ type Connector struct { | |
|
||
type connectorConfig struct { | ||
Processors processors.PluginConfig `config:"processors"` | ||
// ES output index pattern | ||
Index fmtstr.EventFormatString `config:"index"` | ||
|
||
// KeepNull determines whether published events will keep null values or omit them. | ||
KeepNull bool `config:"keep_null"` | ||
|
||
common.EventMetadata `config:",inline"` // Fields and tags to add to events. | ||
} | ||
|
||
func NewConnector(pipeline beat.Pipeline, c *common.Config, dynFields *common.MapStrPointer) (*Connector, error) { | ||
func NewConnector( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. exported function NewConnector should have comment or be unexported |
||
beatInfo beat.Info, pipeline beat.Pipeline, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've been torn about this sort of change in one of my related PRs as well. Basically, we're now passing an extra module.NewConnector(b.Info, b.Publisher, ...) where I wonder if should just make this constructor take There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My usual preference is to pass the simpler information, both to reduce contextual load (a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup, that's fair. I think my concern of adding more arguments is not as strong in comparison, at least not at this point, where the function would be taking in only 4 arguments. If that number ever grows to be really unwieldy, we can revist. +1 for keeping this change as-is. |
||
c *common.Config, dynFields *common.MapStrPointer, | ||
) (*Connector, error) { | ||
config := connectorConfig{} | ||
if err := c.Unpack(&config); err != nil { | ||
return nil, err | ||
} | ||
|
||
processors, err := processors.New(config.Processors) | ||
processors, err := processorsForConfig(beatInfo, config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
@@ -73,3 +80,31 @@ func (c *Connector) Connect() (beat.Client, error) { | |
}, | ||
}) | ||
} | ||
|
||
// processorsForConfig assembles the Processors for a Connector. | ||
func processorsForConfig( | ||
beatInfo beat.Info, config connectorConfig, | ||
) (*processors.Processors, error) { | ||
procs := processors.NewList(nil) | ||
|
||
// Processor order is important! The index processor, if present, must be | ||
// added before the user processors. | ||
if !config.Index.IsEmpty() { | ||
staticFields := fmtstr.FieldsForBeat(beatInfo.Beat, beatInfo.Version) | ||
timestampFormat, err := | ||
fmtstr.NewTimestampFormatString(&config.Index, staticFields) | ||
if err != nil { | ||
return nil, err | ||
} | ||
indexProcessor := add_formatted_index.New(timestampFormat) | ||
procs.AddProcessor(indexProcessor) | ||
} | ||
|
||
userProcs, err := processors.New(config.Processors) | ||
if err != nil { | ||
return nil, err | ||
} | ||
procs.AddProcessors(*userProcs) | ||
|
||
return procs, nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: I quite liked the comments you had in the Filebeat implementation, numbering the processor additions and also noting that ordering was significant. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I removed it because metricbeat processor config is simpler (only one configurable processor source), but you're right, an indication of intent here is good so I re-added a comment :-) |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// 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 module | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/elastic/beats/libbeat/beat" | ||
"github.com/elastic/beats/libbeat/common" | ||
) | ||
|
||
func TestProcessorsForConfig(t *testing.T) { | ||
testCases := map[string]struct { | ||
beatInfo beat.Info | ||
configStr string | ||
event beat.Event | ||
expectedFields map[string]string | ||
}{ | ||
"Simple static index": { | ||
configStr: "index: 'test'", | ||
expectedFields: map[string]string{ | ||
"@metadata.raw_index": "test", | ||
}, | ||
}, | ||
"Index with agent info + timestamp": { | ||
beatInfo: beat.Info{Beat: "TestBeat", Version: "3.9.27"}, | ||
configStr: "index: 'beat-%{[agent.name]}-%{[agent.version]}-%{+yyyy.MM.dd}'", | ||
event: beat.Event{Timestamp: time.Date(1999, time.December, 31, 23, 0, 0, 0, time.UTC)}, | ||
expectedFields: map[string]string{ | ||
"@metadata.raw_index": "beat-TestBeat-3.9.27-1999.12.31", | ||
}, | ||
}, | ||
} | ||
for description, test := range testCases { | ||
if test.event.Fields == nil { | ||
test.event.Fields = common.MapStr{} | ||
} | ||
config, err := connectorConfigFromString(test.configStr) | ||
if err != nil { | ||
t.Errorf("[%s] %v", description, err) | ||
continue | ||
} | ||
processors, err := processorsForConfig(test.beatInfo, config) | ||
if err != nil { | ||
t.Errorf("[%s] %v", description, err) | ||
continue | ||
} | ||
processedEvent, err := processors.Run(&test.event) | ||
// We don't check if err != nil, because we are testing the final outcome | ||
// of running the processors, including when some of them fail. | ||
if processedEvent == nil { | ||
t.Errorf("[%s] Unexpected fatal error running processors: %v\n", | ||
description, err) | ||
} | ||
for key, value := range test.expectedFields { | ||
field, err := processedEvent.GetValue(key) | ||
if err != nil { | ||
t.Errorf("[%s] Couldn't get field %s from event: %v", description, key, err) | ||
continue | ||
} | ||
assert.Equal(t, field, value) | ||
fieldStr, ok := field.(string) | ||
if !ok { | ||
// Note that requiring a string here is just to simplify the test setup, | ||
// not a requirement of the underlying api. | ||
t.Errorf("[%s] Field [%s] should be a string", description, key) | ||
continue | ||
} | ||
if fieldStr != value { | ||
t.Errorf("[%s] Event field [%s]: expected [%s], got [%s]", description, key, value, fieldStr) | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Helper function to convert from YML input string to an unpacked | ||
// connectorConfig | ||
func connectorConfigFromString(s string) (connectorConfig, error) { | ||
config := connectorConfig{} | ||
cfg, err := common.NewConfigFrom(s) | ||
if err != nil { | ||
return config, err | ||
} | ||
if err := cfg.Unpack(&config); err != nil { | ||
return config, err | ||
} | ||
return config, nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
exported function NewConnector should have comment or be unexported