From 5dbe4c1a00201ee653efdb68f6610a7ce5aa0da8 Mon Sep 17 00:00:00 2001 From: urso Date: Wed, 30 Jan 2019 16:02:49 +0100 Subject: [PATCH] Move default index name creation to indexSupport --- libbeat/idxmgmt/idxmgmt.go | 2 +- libbeat/idxmgmt/idxmgmt_test.go | 2 +- libbeat/idxmgmt/std.go | 33 +++++++++++++++++++-------------- libbeat/outputs/output_reg.go | 2 +- 4 files changed, 22 insertions(+), 17 deletions(-) diff --git a/libbeat/idxmgmt/idxmgmt.go b/libbeat/idxmgmt/idxmgmt.go index b736ef97773..b2f7f38568e 100644 --- a/libbeat/idxmgmt/idxmgmt.go +++ b/libbeat/idxmgmt/idxmgmt.go @@ -53,7 +53,7 @@ type Supporter interface { // The defaultIndex string is interpreted as format string. It is used // as default index if the configuration provided does not define an index or // has no default fallback if all indices are guarded by conditionals. - BuildSelector(defaultIndex string, cfg *common.Config) (outputs.IndexSelector, error) + BuildSelector(cfg *common.Config) (outputs.IndexSelector, error) // Manager creates a new manager that can be used to execute the required steps // for initializing an index, ILM policies, and write aliases. diff --git a/libbeat/idxmgmt/idxmgmt_test.go b/libbeat/idxmgmt/idxmgmt_test.go index ea38c54119d..3d7a841b797 100644 --- a/libbeat/idxmgmt/idxmgmt_test.go +++ b/libbeat/idxmgmt/idxmgmt_test.go @@ -251,7 +251,7 @@ func TestDefaultSupport_BuildSelector(t *testing.T) { im, err := factory(nil, info, common.MustNewConfigFrom(test.imCfg)) require.NoError(t, err) - sel, err := im.BuildSelector("", common.MustNewConfigFrom(test.cfg)) + sel, err := im.BuildSelector(common.MustNewConfigFrom(test.cfg)) require.NoError(t, err) meta := test.meta diff --git a/libbeat/idxmgmt/std.go b/libbeat/idxmgmt/std.go index da87361157a..a2e8149e7bb 100644 --- a/libbeat/idxmgmt/std.go +++ b/libbeat/idxmgmt/std.go @@ -32,11 +32,12 @@ import ( ) type indexSupport struct { - log *logp.Logger - ilm ilm.Supporter - info beat.Info - migration bool - templateCfg template.TemplateConfig + log *logp.Logger + ilm ilm.Supporter + info beat.Info + migration bool + templateCfg template.TemplateConfig + defaultIndex string st indexState } @@ -84,11 +85,12 @@ func newIndexSupport( } return &indexSupport{ - log: log, - ilm: ilm, - info: info, - templateCfg: tmplCfg, - migration: migration, + log: log, + ilm: ilm, + info: info, + templateCfg: tmplCfg, + migration: migration, + defaultIndex: fmt.Sprintf("%v-%v-%%{+yyyy.MM.dd}", info.IndexPrefix, info.Version), }, nil } @@ -132,7 +134,7 @@ func (s *indexSupport) Manager( } } -func (s *indexSupport) BuildSelector(defaultIndex string, cfg *common.Config) (outputs.IndexSelector, error) { +func (s *indexSupport) BuildSelector(cfg *common.Config) (outputs.IndexSelector, error) { var err error log := s.log @@ -155,9 +157,6 @@ func (s *indexSupport) BuildSelector(defaultIndex string, cfg *common.Config) (o return nil, err } } - if indexName == "" { - indexName = defaultIndex - } var alias string mode := s.ilm.Mode() @@ -169,6 +168,12 @@ func (s *indexSupport) BuildSelector(defaultIndex string, cfg *common.Config) (o indexName = alias } + // no index name configuration found yet -> define default index name based on + // beat.Info provided to the indexSupport on during setup. + if indexName == "" { + indexName = s.defaultIndex + } + selCfg.SetString("index", -1, indexName) buildSettings := outil.Settings{ Key: "index", diff --git a/libbeat/outputs/output_reg.go b/libbeat/outputs/output_reg.go index bb39837ea99..6625f1125b1 100644 --- a/libbeat/outputs/output_reg.go +++ b/libbeat/outputs/output_reg.go @@ -39,7 +39,7 @@ type IndexManager interface { // the outputs configuration. // The defaultIndex is interpreted as format string and used as default fallback // if no index is configured or all indices are guarded using conditionals. - BuildSelector(defaultIndex string, cfg *common.Config) (IndexSelector, error) + BuildSelector(cfg *common.Config) (IndexSelector, error) } // IndexSelector is used to find the index name an event shall be indexed to.