Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for optional metricsets in xpack mode #34273

Merged
merged 7 commits into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions metricbeat/helper/elastic/elastic.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func FixTimestampField(m mapstr.M, field string) error {
}

// NewModule returns a new Elastic stack module with the appropriate metricsets configured.
func NewModule(base *mb.BaseModule, xpackEnabledMetricsets []string, logger *logp.Logger) (*mb.BaseModule, error) {
func NewModule(base *mb.BaseModule, xpackEnabledMetricsets []string, optionalXpackMetricsets []string, logger *logp.Logger) (*mb.BaseModule, error) {
moduleName := base.Name()

config := struct {
Expand All @@ -158,8 +158,31 @@ func NewModule(base *mb.BaseModule, xpackEnabledMetricsets []string, logger *log
return nil, errors.Wrapf(err, "could not unpack configuration for module %v", moduleName)
}

// These metricsets are exactly the ones required if xpack.enabled == true
raw["metricsets"] = xpackEnabledMetricsets
// Ensure all required metricsets are enabled when xpack.enabled == true
cfgdMetricsets, err := raw.GetValue("metricsets")
if err != nil || cfgdMetricsets == nil || len(cfgdMetricsets.([]interface{})) == 0 {
raw["metricsets"] = xpackEnabledMetricsets
} else {
// Allow some optional metricsets to be enabled when xpack.enabled == true
cfgdMetricsetsSlice := cfgdMetricsets.([]interface{})
metricsets := xpackEnabledMetricsets

for _, cfgdMs := range cfgdMetricsetsSlice {
found := false
for _, optMs := range optionalXpackMetricsets {
if cfgdMs == optMs {
found = true
break
}
}

if found {
metricsets = append(metricsets, cfgdMs.(string))
}
}

raw["metricsets"] = metricsets
}
joshdover marked this conversation as resolved.
Show resolved Hide resolved

newConfig, err := conf.NewConfigFrom(raw)
if err != nil {
Expand Down
27 changes: 23 additions & 4 deletions metricbeat/helper/elastic/elastic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,18 +157,22 @@ func TestConfigureModule(t *testing.T) {
require.NoError(t, err)
err = mockRegistry.AddMetricSet(moduleName, "baz", mockMetricSetFactory)
require.NoError(t, err)
err = mockRegistry.AddMetricSet(moduleName, "thud", mockMetricSetFactory)
require.NoError(t, err)

tests := map[string]struct {
initConfig metricSetConfig
xpackEnabledMetricsets []string
newConfig metricSetConfig
initConfig metricSetConfig
xpackEnabledMetricsets []string
optionalXpackMetricsets []string
newConfig metricSetConfig
}{
"no_xpack_enabled": {
metricSetConfig{
Module: moduleName,
MetricSets: []string{"foo", "bar"},
},
[]string{"baz", "qux", "foo"},
[]string{},
metricSetConfig{
Module: moduleName,
MetricSets: []string{"foo", "bar"},
Expand All @@ -181,12 +185,27 @@ func TestConfigureModule(t *testing.T) {
MetricSets: []string{"foo", "bar"},
},
[]string{"baz", "qux", "foo"},
[]string{},
metricSetConfig{
Module: moduleName,
XPackEnabled: true,
MetricSets: []string{"baz", "qux", "foo"},
},
},
"xpack_enabled_with_optional": {
metricSetConfig{
Module: moduleName,
XPackEnabled: true,
MetricSets: []string{"foo", "bar", "thud"},
},
[]string{"baz", "qux", "foo"},
[]string{"bar"},
metricSetConfig{
Module: moduleName,
XPackEnabled: true,
MetricSets: []string{"baz", "qux", "foo", "bar"}, // include optional, exclude others
},
},
}

for name, test := range tests {
Expand All @@ -200,7 +219,7 @@ func TestConfigureModule(t *testing.T) {
require.Fail(t, "expecting module to be base module")
}

newM, err := NewModule(bm, test.xpackEnabledMetricsets, logp.L())
newM, err := NewModule(bm, test.xpackEnabledMetricsets, test.optionalXpackMetricsets, logp.L())
require.NoError(t, err)

var newConfig metricSetConfig
Expand Down
2 changes: 1 addition & 1 deletion metricbeat/module/beat/beat.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var metricSets = []string{"state", "stats"}

// NewModule creates a new module
func NewModule(base mb.BaseModule) (mb.Module, error) {
return elastic.NewModule(&base, metricSets, logp.NewLogger(ModuleName))
return elastic.NewModule(&base, metricSets, []string{}, logp.NewLogger(ModuleName))
}

// ModuleName is the name of this module.
Expand Down
2 changes: 1 addition & 1 deletion metricbeat/module/elasticsearch/elasticsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func NewModule(base mb.BaseModule) (mb.Module, error) {
"node_stats",
"shard",
}
return elastic.NewModule(&base, xpackEnabledMetricSets, logp.NewLogger(ModuleName))
return elastic.NewModule(&base, xpackEnabledMetricSets, []string{}, logp.NewLogger(ModuleName))
}

var (
Expand Down
2 changes: 1 addition & 1 deletion metricbeat/module/kibana/kibana.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func NewModule(base mb.BaseModule) (mb.Module, error) {
xpackEnabledMetricSets := []string{
"stats", "cluster_rules", "node_rules", "cluster_actions", "node_actions",
}
return elastic.NewModule(&base, xpackEnabledMetricSets, logp.NewLogger(ModuleName))
return elastic.NewModule(&base, xpackEnabledMetricSets, []string{}, logp.NewLogger(ModuleName))
}

// GetVersion returns the version of the Kibana instance
Expand Down
2 changes: 1 addition & 1 deletion metricbeat/module/logstash/logstash.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func init() {

// NewModule creates a new module
func NewModule(base mb.BaseModule) (mb.Module, error) {
return elastic.NewModule(&base, []string{"node", "node_stats"}, logp.NewLogger(ModuleName))
return elastic.NewModule(&base, []string{"node", "node_stats"}, []string{}, logp.NewLogger(ModuleName))
}

// ModuleName is the name of this module.
Expand Down