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

Fix libbeat argument parsing to allow beats to be imported in other code bases. #41153

Closed
leehinman opened this issue Oct 7, 2024 · 1 comment · Fixed by #41277
Closed

Fix libbeat argument parsing to allow beats to be imported in other code bases. #41153

leehinman opened this issue Oct 7, 2024 · 1 comment · Fixed by #41277
Assignees
Labels
bug Team:Elastic-Agent-Data-Plane Label for the Agent Data Plane team

Comments

@leehinman
Copy link
Contributor

leehinman commented Oct 7, 2024

had to be reverted because of APM issues:

Also when trying to add beats to elastic-agent we discovered:

We need to rework Beats argument parsing and init calls to make libbeat both safe to import in other code bases but also provide guidance for APM to use the modified code correctly.

Locations that need updating:

libbeat

  • flag.Var(plugins, "plugin", "Load additional plugins")
  • func init() {
    // add '-path.x' options overwriting paths in 'overwrites' config
    makePathFlag := func(name, usage string) *string {
    return config.ConfigOverwriteFlag(nil, overwrites, name, name, "", usage)
    }
    homePath = makePathFlag("path.home", "Home path")
    configPath = makePathFlag("path.config", "Configuration path")
    makePathFlag("path.data", "Data path")
    makePathFlag("path.logs", "Logs path")
    }
  • func Enabled() bool {
    type management struct {
    Enabled bool `config:"management.enabled"`
    }
    var managementSettings management
    cfgFlag := flag.Lookup("E")
    if cfgFlag == nil {
    return false
    }
    cfgObject, _ := cfgFlag.Value.(*config.SettingsFlag)
    cliCfg := cfgObject.Config()
    err := cliCfg.Unpack(&managementSettings)
    if err != nil {
    return false
    }
    return managementSettings.Enabled
    }
  • func init() {
    flag.StringVar(&defaultTempDir, "dir", "", "Temporary directory for use by the test")
    flag.BoolVar(&keepTmpDir, "keep", false, "Keep temporary test directories")
    }
  • func init() {
    // backwards compatibility workaround, convert -flags to --flags:
    for i, arg := range os.Args[1:] {
    if strings.HasPrefix(arg, "-") && !strings.HasPrefix(arg, "--") && len(arg) > 2 {
    os.Args[1+i] = "-" + arg
    }
    }
    }
  • func init() {
    flag.BoolVar(&publishDisabled, "N", false, "Disable actual publishing for testing")
    }
  • beats/libbeat/cmd/root.go

    Lines 98 to 115 in 2ff38df

    rootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("E"))
    rootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("c"))
    rootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("d"))
    rootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("v"))
    rootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("e"))
    rootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("environment"))
    rootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("path.config"))
    rootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("path.data"))
    rootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("path.logs"))
    rootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("path.home"))
    rootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("strict.perms"))
    if f := flag.CommandLine.Lookup("plugin"); f != nil {
    rootCmd.PersistentFlags().AddGoFlag(f)
    }
    // Inherit root flags from run command
    // TODO deprecate when root command no longer executes run (7.0)
    rootCmd.Flags().AddFlagSet(rootCmd.RunCmd.Flags())
  • func (b *Beat) handleFlags() error {
    flag.Parse()
    return cfgfile.HandleFlags()
    }
  • beats/libbeat/cmd/run.go

    Lines 44 to 47 in 2ff38df

    runCmd.Flags().AddGoFlag(flag.CommandLine.Lookup("N"))
    runCmd.Flags().AddGoFlag(flag.CommandLine.Lookup("httpprof"))
    runCmd.Flags().AddGoFlag(flag.CommandLine.Lookup("cpuprofile"))
    runCmd.Flags().AddGoFlag(flag.CommandLine.Lookup("memprofile"))
  • var flagStrictPerms = flag.Bool("strict.perms", true, "Strict permission checking on config files")

filebeat

  • // Modules related command line flags.
    var (
    modulesFlag = flag.String("modules", "", "List of enabled modules (comma separated)")
    moduleOverrides = config.SettingFlag(nil, "M", "Module configuration overwrite")
    )
  • runFlags := pflag.NewFlagSet(Name, pflag.ExitOnError)
    runFlags.AddGoFlag(flag.CommandLine.Lookup("once"))
    runFlags.AddGoFlag(flag.CommandLine.Lookup("modules"))
  • func Filebeat(inputs beater.PluginFactory, settings instance.Settings) *cmd.BeatsRootCmd {
    command := cmd.GenRootCmdWithSettings(beater.New(inputs), settings)
    command.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("M"))
    command.TestCmd.Flags().AddGoFlag(flag.CommandLine.Lookup("modules"))
    command.SetupCmd.Flags().AddGoFlag(flag.CommandLine.Lookup("modules"))
    command.AddCommand(cmd.GenModulesCmd(Name, "", buildModulesManager))
    command.AddCommand(genGenerateCmd())
    return command
    }
  • var once = flag.Bool("once", false, "Run filebeat only once until all harvesters reach EOF")

metricbeat

packetbeat

  • func PacketbeatSettings(globals processors.PluginConfig) instance.Settings {
    runFlags := pflag.NewFlagSet(Name, pflag.ExitOnError)
    runFlags.AddGoFlag(flag.CommandLine.Lookup("I"))
    runFlags.AddGoFlag(flag.CommandLine.Lookup("t"))
    runFlags.AddGoFlag(flag.CommandLine.Lookup("O"))
    runFlags.AddGoFlag(flag.CommandLine.Lookup("l"))
    runFlags.AddGoFlag(flag.CommandLine.Lookup("dump"))
  • var cmdLineArgs = flags{
    file: flag.String("I", "", "Read packet data from specified file"),
    loop: flag.Int("l", 1, "Loop file. 0 - loop forever"),
    oneAtAtime: flag.Bool("O", false, "Read packets one at a time (press Enter)"),
    topSpeed: flag.Bool("t", false, "Read packets as fast as possible, without sleeping"),
    dumpfile: flag.String("dump", "", "Write all captured packets to libpcap files with this prefix - a timestamp and pcap extension are added"),
    }

x-pack

@leehinman leehinman added the Team:Elastic-Agent-Data-Plane Label for the Agent Data Plane team label Oct 7, 2024
@elasticmachine
Copy link
Collaborator

Pinging @elastic/elastic-agent-data-plane (Team:Elastic-Agent-Data-Plane)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Team:Elastic-Agent-Data-Plane Label for the Agent Data Plane team
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants