Reading values from lsbeat.yml

I have written a custom beat lsbeat.yml (similar to the tutorial) and it works fine. I have a question on reading values from yml file.

I added a new property in the lsbeat.yml called
lastRunTimestamp: "2017-07-27 00:00:00"

Now how do I read the "lastRunTimestamp" property value from lsbeat.yml in the lsbeat.go file?

I tried to look for the code in libbeat directory for the code which reads output.elasticsearch property etc. from lsbeat.yml, but couldn't find anything solid except cfgfile_test.go file.

Note: I know to use "gopkg.in/yaml.v2" and get the properties. But I want to use something from beat framework itself. Hence the question.

Your beat has access to the config data through the *common.Config object passed to the New() function.

func New(b *beat.Beat, cfg *common.Config) (beat.Beater, error) {
	config := config.DefaultConfig
	if err := cfg.Unpack(&config); err != nil {
		return nil, fmt.Errorf("Error unpacking config: %v", err)
	}

You can add a new field to your config struct and it will be populated with the value from the config file when calling Unpack.

type Config struct {
    LastRunTimestamp string `config:"lastRunTimestamp"`
}

Or you can pass a map[string]interface{} object to Unpack() and the map will be populated with all the config data from the config file.

Thanks Andrew for the response. I added the lastRunTimestamp in the config.go file like below

package config

import "time"

type Config struct {
	Period time.Duration `config:"period"`
	LastRunTimestamp string `config: "lastRunTimestamp"`
}

var DefaultConfig = Config{
	Period: 60 * time.Second,
	LastRunTimestamp: "2017-07-27 00:00:25",
}

But I only get the LastRunTimestamp value configured in the DefaultConfig (i.e., "2017-07-27 00:00:25")and not the value that I enter in the lsbeat.yml ("2017-07-27 00:00:00").

Any clue.

Try removing that space after the colon in config: "last.

1 Like

That worked. Thanks for the help.

This topic was automatically closed after 21 days. New replies are no longer allowed.