Skip to content

Commit

Permalink
Check validity of logstash module configuration when `xpack.en… (#12670)
Browse files Browse the repository at this point in the history
When the `logstash` metricbeat module is enabled and the `xpack.enabled` flag in the module configuration is set to `true`, an exact set of metricsets is required to be configured for Stack Monitoring to work correctly. This PR implements the necessary validation.
  • Loading branch information
ycombinator authored Jun 26, 2019
1 parent ca90e10 commit 5b1c1ab
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions metricbeat/module/logstash/logstash.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,54 @@ import (

"github.com/pkg/errors"

"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/metricbeat/helper"
"github.com/elastic/beats/metricbeat/mb"
)

func init() {
// Register the ModuleFactory function for this module.
if err := mb.Registry.AddModule(ModuleName, NewModule); err != nil {
panic(err)
}
}

// NewModule creates a new module after performing validation.
func NewModule(base mb.BaseModule) (mb.Module, error) {
if err := validateXPackMetricsets(base); err != nil {
return nil, err
}

return &base, nil
}

// Validate that correct metricsets have been specified if xpack.enabled = true.
func validateXPackMetricsets(base mb.BaseModule) error {
config := struct {
Metricsets []string `config:"metricsets"`
XPackEnabled bool `config:"xpack.enabled"`
}{}
if err := base.UnpackConfig(&config); err != nil {
return err
}

// Nothing to validate if xpack.enabled != true
if !config.XPackEnabled {
return nil
}

expectedXPackMetricsets := []string{
"node",
"node_stats",
}

if !common.MakeStringSet(config.Metricsets...).Equals(common.MakeStringSet(expectedXPackMetricsets...)) {
return errors.Errorf("The %v module with xpack.enabled: true must have metricsets: %v", ModuleName, expectedXPackMetricsets)
}

return nil
}

// ModuleName is the name of this module.
const ModuleName = "logstash"

Expand Down

0 comments on commit 5b1c1ab

Please sign in to comment.