Skip to content

Commit

Permalink
filebeat: minor cleanup and performance improvement (#37644)
Browse files Browse the repository at this point in the history
* filebeat/generator/fields: replace for loop with single instruction

Improve performance by replacing a loop with a single instruction.

Signed-off-by: Florian Lehner <florian.lehner@elastic.co>

* filebeat/fileset/flags: remove duplicate import

Simplify code by replacing a duplicate import.

Signed-off-by: Florian Lehner <florian.lehner@elastic.co>

* filebeat/generator/fields: precompile regex

Precompile the fixed regex once instead for each iteration.

Signed-off-by: Florian Lehner <florian.lehner@elastic.co>

---------

Signed-off-by: Florian Lehner <florian.lehner@elastic.co>
Co-authored-by: Denis <denis.rechkunov@elastic.co>
  • Loading branch information
florianl and rdner authored Jan 25, 2024
1 parent d6bed8d commit db4c44a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 13 deletions.
7 changes: 3 additions & 4 deletions filebeat/fileset/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"strings"

"github.com/elastic/elastic-agent-libs/config"
conf "github.com/elastic/elastic-agent-libs/config"
)

// Modules related command line flags.
Expand All @@ -32,11 +31,11 @@ var (
moduleOverrides = config.SettingFlag(nil, "M", "Module configuration overwrite")
)

type ModuleOverrides map[string]map[string]*conf.C // module -> fileset -> Config
type ModuleOverrides map[string]map[string]*config.C // module -> fileset -> Config

// Get returns an array of configuration overrides that should be merged in order.
func (mo *ModuleOverrides) Get(module, fileset string) []*conf.C {
ret := []*conf.C{}
func (mo *ModuleOverrides) Get(module, fileset string) []*config.C {
ret := []*config.C{}

moduleWildcard := (*mo)["*"]["*"]
if moduleWildcard != nil {
Expand Down
13 changes: 4 additions & 9 deletions filebeat/generator/fields/fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,12 @@ func addNewField(fs []field, f field) []field {
return append(fs, f)
}

func getSemanticElementsFromPatterns(patterns []string) ([]field, error) {
r, err := regexp.Compile("{[\\.\\w\\:]*}")
if err != nil {
return nil, err
}
var semanticElementsRegex = regexp.MustCompile(`{[\.\w\:]*}`)

func getSemanticElementsFromPatterns(patterns []string) ([]field, error) {
var fs []field
for _, lp := range patterns {
pp := r.FindAllString(lp, -1)
pp := semanticElementsRegex.FindAllString(lp, -1)
for _, p := range pp {
f := newField(p)
if f.SemanticElements == nil {
Expand Down Expand Up @@ -221,9 +218,7 @@ func accumulateRemoveFields(remove interface{}, out []string) []string {
case string:
return append(out, vs)
case []string:
for _, vv := range vs {
out = append(out, vv)
}
out = append(out, vs...)
case []interface{}:
for _, vv := range vs {
vvs := vv.(string)
Expand Down

0 comments on commit db4c44a

Please sign in to comment.