Skip to content

Commit

Permalink
Field registry: Don't overwrite fields with same name (elastic#13914) (
Browse files Browse the repository at this point in the history
…elastic#14037)

When fields are registered (fields.go, autogenerated), they are grouped
by (Beatname, Name and Priority). Currently, if the same triplet is used
more than once, the previous asset is overwritten.

This is the case with Filebeat inputs and modules sharing the same name:

module/netflow/fields.go:
  asset.SetFields("filebeat", "netflow", asset.ModuleFieldsPri, AssetNetflow)

input/netflow/fields.go:
  asset.SetFields("filebeat", "netflow", asset.ModuleFieldsPri, AssetNetflow)

Also this introduces an inconsistence as the order of registration can
vary.

Fixes elastic#13768

(cherry picked from commit 5fec6bc)
  • Loading branch information
adriansr authored Oct 14, 2019
1 parent 6584e1d commit 24ef6fb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- cisco asa and ftd filesets: Fix parsing of message 106001. {issue}13891[13891] {pull}13903[13903]
- Fix merging of fields specified in global scope with fields specified under an input's scope. {issue}3628[3628] {pull}13909[13909]
- Fix delay in enforcing close_renamed and close_removed options. {issue}13488[13488] {pull}13907[13907]
- Fix missing netflow fields in index template. {issue}13768[13768] {pull}13914[13914]

*Heartbeat*

Expand Down
22 changes: 12 additions & 10 deletions libbeat/asset/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
// As each entry is an array of bytes multiple fields.yml can be added under one path.
// This can become useful as we don't have to generate anymore the fields.yml but can
// package each local fields.yml from things like processors.
var FieldsRegistry = map[string]map[int]map[string]string{}
var FieldsRegistry = map[string]map[int]map[string][]string{}

// SetFields sets the fields for a given beat and asset name
func SetFields(beat, name string, p Priority, asset func() string) error {
Expand All @@ -38,14 +38,14 @@ func SetFields(beat, name string, p Priority, asset func() string) error {
priority := int(p)

if _, ok := FieldsRegistry[beat]; !ok {
FieldsRegistry[beat] = map[int]map[string]string{}
FieldsRegistry[beat] = map[int]map[string][]string{}
}

if _, ok := FieldsRegistry[beat][priority]; !ok {
FieldsRegistry[beat][priority] = map[string]string{}
FieldsRegistry[beat][priority] = map[string][]string{}
}

FieldsRegistry[beat][priority][name] = data
FieldsRegistry[beat][priority][name] = append(FieldsRegistry[beat][priority][name], data)

return nil
}
Expand Down Expand Up @@ -74,13 +74,15 @@ func GetFields(beat string) ([]byte, error) {
sort.Strings(entries)

for _, entry := range entries {
data := priorityRegistry[entry]
output, err := DecodeData(data)
if err != nil {
return nil, err
list := priorityRegistry[entry]
for _, data := range list {
output, err := DecodeData(data)
if err != nil {
return nil, err
}

fields = append(fields, output...)
}

fields = append(fields, output...)
}
}
return fields, nil
Expand Down

0 comments on commit 24ef6fb

Please sign in to comment.