Skip to content

Commit

Permalink
refactor filebeat and metric module namespace (#40660)
Browse files Browse the repository at this point in the history
makes it possible to configure filebeat and metricbeat so the module
namespaces don't collide.  This is necessary so you can have filebeat
and metricbeat in the same process.
  • Loading branch information
leehinman authored Aug 30, 2024
1 parent a3db002 commit 2f9345f
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 14 deletions.
11 changes: 8 additions & 3 deletions filebeat/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,13 @@ const Name = "filebeat"
// RootCmd to handle beats cli
var RootCmd *cmd.BeatsRootCmd

// FilebeatSettings contains the default settings for filebeat
func FilebeatSettings() instance.Settings {
// FilebeatSettings contains the default settings for filebeat.
// moduleNameSpace allows you to override the default setting of
// "module" for the module metrics to avoid name collisions.
func FilebeatSettings(moduleNameSpace string) instance.Settings {
if moduleNameSpace == "" {
moduleNameSpace = "module"
}
runFlags := pflag.NewFlagSet(Name, pflag.ExitOnError)
runFlags.AddGoFlag(flag.CommandLine.Lookup("once"))
runFlags.AddGoFlag(flag.CommandLine.Lookup("modules"))
Expand All @@ -51,7 +56,7 @@ func FilebeatSettings() instance.Settings {
HasDashboards: true,
Initialize: []func(){
include.InitializeModule,
fileset.RegisterMonitoringModules,
func() { fileset.RegisterMonitoringModules(moduleNameSpace) },
input.RegisterMonitoringInputs,
},
}
Expand Down
4 changes: 2 additions & 2 deletions filebeat/fileset/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ var moduleList = monitoring.NewUniqueList()
var moduleListMetricsOnce sync.Once

// RegisterMonitoringModules registers the modules list with the monitoring system.
func RegisterMonitoringModules() {
func RegisterMonitoringModules(namespace string) {
moduleListMetricsOnce.Do(func() {
monitoring.NewFunc(monitoring.GetNamespace("state").GetRegistry(), "module", moduleList.Report, monitoring.Report)
monitoring.NewFunc(monitoring.GetNamespace("state").GetRegistry(), namespace, moduleList.Report, monitoring.Report)
})
}

Expand Down
2 changes: 1 addition & 1 deletion filebeat/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
// Finally, input uses the registrar information, on restart, to
// determine where in each file to restart a harvester.
func main() {
if err := cmd.Filebeat(inputs.Init, cmd.FilebeatSettings()).Execute(); err != nil {
if err := cmd.Filebeat(inputs.Init, cmd.FilebeatSettings("")).Execute(); err != nil {
os.Exit(1)
}
}
2 changes: 1 addition & 1 deletion filebeat/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var (
func init() {
testing.Init()
systemTest = flag.Bool("systemTest", false, "Set to true when running system tests")
fbCommand = fbcmd.Filebeat(inputs.Init, fbcmd.FilebeatSettings())
fbCommand = fbcmd.Filebeat(inputs.Init, fbcmd.FilebeatSettings(""))
fbCommand.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("systemTest"))
fbCommand.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("test.coverprofile"))
}
Expand Down
11 changes: 8 additions & 3 deletions metricbeat/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ var withECSVersion = processing.WithFields(mapstr.M{
})

// MetricbeatSettings contains the default settings for metricbeat
func MetricbeatSettings() instance.Settings {
// moduleNameSpace allows you to override the default setting of
// "module" for the module metrics to avoid name collisions.
func MetricbeatSettings(moduleNameSpace string) instance.Settings {
if moduleNameSpace == "" {
moduleNameSpace = "module"
}
var runFlags = pflag.NewFlagSet(Name, pflag.ExitOnError)
runFlags.AddGoFlag(flag.CommandLine.Lookup("system.hostfs"))
return instance.Settings{
Expand All @@ -63,7 +68,7 @@ func MetricbeatSettings() instance.Settings {
Processing: processing.MakeDefaultSupport(true, nil, withECSVersion, processing.WithHost, processing.WithAgentMeta()),
Initialize: []func(){
include.InitializeModule,
module.RegisterMonitoringModules,
func() { module.RegisterMonitoringModules(moduleNameSpace) },
},
}
}
Expand All @@ -77,5 +82,5 @@ func Initialize(settings instance.Settings) *cmd.BeatsRootCmd {
}

func init() {
RootCmd = Initialize(MetricbeatSettings())
RootCmd = Initialize(MetricbeatSettings(""))
}
4 changes: 2 additions & 2 deletions metricbeat/mb/module/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ var moduleList = monitoring.NewUniqueList()
var moduleListMetricsOnce sync.Once

// RegisterMonitoringModules registers the modules list with the monitoring system.
func RegisterMonitoringModules() {
func RegisterMonitoringModules(namespace string) {
moduleListMetricsOnce.Do(func() {
monitoring.NewFunc(monitoring.GetNamespace("state").GetRegistry(), "module", moduleList.Report, monitoring.Report)
monitoring.NewFunc(monitoring.GetNamespace("state").GetRegistry(), namespace, moduleList.Report, monitoring.Report)
})
}

Expand Down
2 changes: 1 addition & 1 deletion x-pack/filebeat/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const Name = fbcmd.Name

// Filebeat build the beat root command for executing filebeat and it's subcommands.
func Filebeat() *cmd.BeatsRootCmd {
settings := fbcmd.FilebeatSettings()
settings := fbcmd.FilebeatSettings("")
globalProcs, err := processors.NewPluginConfigFromList(defaultProcessors())
if err != nil { // these are hard-coded, shouldn't fail
panic(fmt.Errorf("error creating global processors: %w", err))
Expand Down
2 changes: 1 addition & 1 deletion x-pack/metricbeat/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func init() {
if err != nil { // these are hard-coded, shouldn't fail
panic(fmt.Errorf("error creating global processors: %w", err))
}
settings := mbcmd.MetricbeatSettings()
settings := mbcmd.MetricbeatSettings("")
settings.ElasticLicensed = true
settings.Processing = processing.MakeDefaultSupport(true, globalProcs, withECSVersion, processing.WithHost, processing.WithAgentMeta())
RootCmd = cmd.GenRootCmdWithSettings(beater.DefaultCreator(), settings)
Expand Down

0 comments on commit 2f9345f

Please sign in to comment.