Skip to content

Commit

Permalink
Fix loading processors from autodiscover hints (#16348)
Browse files Browse the repository at this point in the history
* Parse processors from hints as JSON.

* Add test.

* Add changelog entry.
  • Loading branch information
blakerouse authored Feb 18, 2020
1 parent ba3f0b0 commit d1f383f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Upgrade go-ucfg to latest v0.8.1. {pull}15937{15937}
- Fix index names for indexing not always guaranteed to be lower case. {pull}16081[16081]
- Add `ssl.ca_sha256` option to the supported TLS option, this allow to check that a specific certificate is used as part of the verified chain. {issue}15717[15717]
- Fix loading processors from annotation hints. {pull}16348[16348]

*Auditbeat*

Expand Down
15 changes: 14 additions & 1 deletion libbeat/autodiscover/builder/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,20 @@ func GetHintAsList(hints common.MapStr, key, config string) []string {

// GetProcessors gets processor definitions from the hints and returns a list of configs as a MapStr
func GetProcessors(hints common.MapStr, key string) []common.MapStr {
return GetConfigs(hints, key, "processors")
processors := GetConfigs(hints, key, "processors")
for _, proc := range processors {
for key, value := range proc {
if str, ok := value.(string); ok {
cfg := common.MapStr{}
if err := json.Unmarshal([]byte(str), &cfg); err != nil {
logp.Debug("autodiscover.builder", "unable to unmarshal json due to error: %v", err)
continue
}
proc[key] = cfg
}
}
}
return processors
}

// GetConfigs takes in a key and returns a list of configs as a slice of MapStr
Expand Down
27 changes: 26 additions & 1 deletion libbeat/autodiscover/builder/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,30 @@ import (
"github.com/elastic/beats/libbeat/common"
)

func TestGetProcessors(t *testing.T) {
hints := common.MapStr{
"co": common.MapStr{
"elastic": common.MapStr{
"logs": common.MapStr{
"processors": common.MapStr{
"add_fields": `{"fields": {"foo": "bar"}}`,
},
},
},
},
}
procs := GetProcessors(hints, "co.elastic.logs")
assert.Equal(t, []common.MapStr{
common.MapStr{
"add_fields": common.MapStr{
"fields": map[string]interface{}{
"foo": "bar",
},
},
},
}, procs)
}

func TestGenerateHints(t *testing.T) {
tests := []struct {
annotations map[string]string
Expand All @@ -38,6 +62,7 @@ func TestGenerateHints(t *testing.T) {

// Scenarios being tested:
// logs/multiline.pattern must be a nested common.MapStr under hints.logs
// logs/processors.add_fields must be nested common.MapStr under hints.logs
// logs/json.keys_under_root must be a nested common.MapStr under hints.logs
// metrics/module must be found in hints.metrics
// not.to.include must not be part of hints
Expand Down Expand Up @@ -191,6 +216,6 @@ func TestGenerateHints(t *testing.T) {
for k, v := range test.annotations {
annMap.Put(k, v)
}
assert.Equal(t, GenerateHints(annMap, "foobar", "co.elastic"), test.result)
assert.Equal(t, test.result, GenerateHints(annMap, "foobar", "co.elastic"))
}
}

0 comments on commit d1f383f

Please sign in to comment.