Skip to content

Commit

Permalink
Update idxmgmt to libbeat changes
Browse files Browse the repository at this point in the history
  • Loading branch information
simitt committed Jan 20, 2022
1 parent d8c0bff commit 5111d3e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
31 changes: 28 additions & 3 deletions idxmgmt/supporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ type supporter struct {

type indexSelector outil.Selector

type ilmSelector outil.Selector

// autoSelector is an outputs.IndexSelector that delegates to either an
// unmanaged or ILM index selector depending on whether ILM is enabled.
type autoSelector struct {
Expand Down Expand Up @@ -101,7 +103,7 @@ func (s *supporter) BuildSelector(_ *common.Config) (outputs.IndexSelector, erro
if err != nil {
return nil, err
}
ilmSelector, err := s.buildSelector(s.ilmConfig.SelectorConfig())
managedSelector, err := s.buildSelector(s.ilmConfig.SelectorConfig())
if err != nil {
return nil, err
}
Expand All @@ -110,9 +112,9 @@ func (s *supporter) BuildSelector(_ *common.Config) (outputs.IndexSelector, erro
case libilm.ModeDisabled:
return indexSelector(unmanagedSelector), nil
case libilm.ModeEnabled:
return indexSelector(ilmSelector), nil
return ilmSelector(managedSelector), nil
}
return &autoSelector{ilmEnabled: &s.ilmEnabled, unmanaged: unmanagedSelector, ilm: ilmSelector}, nil
return &autoSelector{ilmEnabled: &s.ilmEnabled, unmanaged: unmanagedSelector, ilm: managedSelector}, nil
}

func (s *supporter) buildSelector(cfg *common.Config, err error) (outil.Selector, error) {
Expand Down Expand Up @@ -142,6 +144,13 @@ func (s *autoSelector) Select(evt *beat.Event) (string, error) {
return s.unmanaged.Select(evt)
}

func (s *autoSelector) IsAlias() bool {
if s.ilmEnabled.Load() {
return true
}
return s.unmanaged.IsAlias()
}

// Select either returns the index from the event's metadata or the regular index.
func (s indexSelector) Select(evt *beat.Event) (string, error) {
if idx := getEventCustomIndex(evt); idx != "" {
Expand All @@ -150,6 +159,22 @@ func (s indexSelector) Select(evt *beat.Event) (string, error) {
return outil.Selector(s).Select(evt)
}

func (s indexSelector) IsAlias() bool {
return outil.Selector(s).IsAlias()
}

// Select either returns the index from the event's metadata or the regular index.
func (s ilmSelector) Select(evt *beat.Event) (string, error) {
if idx := getEventCustomIndex(evt); idx != "" {
return idx, nil
}
return outil.Selector(s).Select(evt)
}

func (s ilmSelector) IsAlias() bool {
return true
}

// this logic is copied and aligned with handling in beats.
func getEventCustomIndex(evt *beat.Event) string {
if len(evt.Meta) == 0 {
Expand Down
17 changes: 14 additions & 3 deletions idxmgmt/supporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func TestIndexSupport_BuildSelector(t *testing.T) {
expected string
cfg common.MapStr
fields common.MapStr
alias bool
}

cases := map[string]testdata{
Expand Down Expand Up @@ -131,16 +132,18 @@ func TestIndexSupport_BuildSelector(t *testing.T) {
withIlm: "apm-7.0.0-sourcemap",
fields: common.MapStr{"processor.event": "sourcemap"},
},
"MetaInformationAlia-lowercased": {
"MetaInformationAlias": {
noIlm: "apm-7.0.0-meta",
withIlm: "apm-7.0.0-meta", //meta overwrites ilm
withIlm: "apm-7.0.0-meta", //meta alias overwrite ilm
ilmAuto: "apm-7.0.0-meta",
fields: common.MapStr{"processor.event": "span"},
meta: common.MapStr{"alias": "APM-7.0.0-meta", "index": "test-123"},
cfg: common.MapStr{"output.elasticsearch.index": "apm-customized"},
},
"MetaInformationIndex": {
noIlm: fmt.Sprintf("apm-7.0.0-%s", day),
withIlm: fmt.Sprintf("apm-7.0.0-%s", day), //meta overwrites ilm
withIlm: fmt.Sprintf("apm-7.0.0-%s", day), //meta index overwrites ilm
ilmAuto: fmt.Sprintf("apm-7.0.0-%s", day),
fields: common.MapStr{"processor.event": "span"},
meta: common.MapStr{"index": "APM-7.0.0"},
cfg: common.MapStr{"output.elasticsearch.index": "apm-customized"},
Expand Down Expand Up @@ -196,6 +199,9 @@ func TestIndexSupport_BuildSelector(t *testing.T) {
idx, err := s.Select(testEvent(test.fields, test.meta))
require.NoError(t, err)
assert.Equal(t, test.expected, idx)

// test if alias
assert.Equal(t, test.alias, s.IsAlias())
})
}

Expand All @@ -207,24 +213,29 @@ func TestIndexSupport_BuildSelector(t *testing.T) {
//ilm true and supported
test.cfg["apm-server.ilm.enabled"] = true
test.expected = test.withIlm
test.alias = true
checkIndexSelector(t, "ILMTrueSupported"+name, test, ilmSupportedHandler)

//ilm=false
test.cfg["apm-server.ilm.enabled"] = false
test.expected = test.noIlm
test.alias = false
checkIndexSelector(t, "ILMFalse"+name, test, ilmSupportedHandler)

//ilm=auto and supported
test.cfg["apm-server.ilm.enabled"] = "auto"
test.expected = test.withIlm
test.alias = true
if test.ilmAuto != "" {
test.expected = test.ilmAuto
test.alias = false
}
checkIndexSelector(t, "ILMAutoSupported"+name, test, ilmSupportedHandler)

//ilm auto but unsupported
test.cfg["apm-server.ilm.enabled"] = "auto"
test.expected = test.noIlm
test.alias = false
checkIndexSelector(t, "ILMAutoUnsupported"+name, test, ilmUnsupportedHandler)
}
}
Expand Down

0 comments on commit 5111d3e

Please sign in to comment.