Skip to content

Commit

Permalink
WIP/not merge: Introduce extra interface for introspection...
Browse files Browse the repository at this point in the history
In elastic#4378 we briefly discussed how to test the parsing of config
parameters. The way ucfg works is that the yaml/json is unpacked
into the struct and  with the current approach at least two issues
can occur:

1.) One can have entries in the config file that are not consumed
2.) One can have members in the struct that are not filled

To allow testing proper parsing of a valid configuration file and
help users to show what has been passed I introduce a new config
option.

Instead of forcing this new method for the existing Beater interface,
add a new introspection interface...

TODO: Is this a sound approach? Shall a list of interfaces be returned?
Print them to a file? What about the b.Config? How to add this as a
system test?

TODO: Don't use go-yaml/yaml but the one used by ucfg?
  • Loading branch information
zecke committed May 31, 2017
1 parent 0f2d9a6 commit 73e275d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
17 changes: 17 additions & 0 deletions libbeat/beat/beat.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import (
svc "github.com/elastic/beats/libbeat/service"
"github.com/elastic/beats/libbeat/template"
"github.com/elastic/beats/libbeat/version"
"github.com/go-yaml/yaml"

// Register default processors.
_ "github.com/elastic/beats/libbeat/processors/actions"
Expand Down Expand Up @@ -92,6 +93,13 @@ type Beater interface {
Stop()
}

// Additional interface to provide introspection. E.g. for system tests
// or other forms of runtime introspection
type BeaterIntrospection interface {
// Provide the internal Config structure
Config() interface{}
}

// Creator initializes and configures a new Beater instance used to execute
// the beat its run-loop.
type Creator func(*Beat, *common.Config) (Beater, error)
Expand Down Expand Up @@ -246,6 +254,15 @@ func (b *Beat) launch(bt Creator) error {
defer reporter.Stop()
}

// Provide yaml presentation of what was parsed
if cfgfile.DumpConfig() {
if val, ok := beater.(BeaterIntrospection); ok {
dat, _ := yaml.Marshal(val.Config())
fmt.Print(string(dat))
}
return GracefulExit
}

// If -configtest was specified, exit now prior to run.
if cfgfile.IsTestConfig() {
fmt.Println("Config OK")
Expand Down
6 changes: 6 additions & 0 deletions libbeat/cfgfile/cfgfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var (
configfiles = flagArgList("c", "beat.yml", "Configuration file, relative to path.config")
overwrites = common.NewFlagConfig(nil, nil, "E", "Configuration overwrite")
testConfig = flag.Bool("configtest", false, "Test configuration and exit.")
dumpConfig = flag.Bool("dumpconfig", false, "Dump parsed configuration.")

// Additional default settings, that must be available for variable expansion
defaults = mustNewConfigFrom(map[string]interface{}{
Expand Down Expand Up @@ -171,3 +172,8 @@ func GetPathConfig() string {
func IsTestConfig() bool {
return *testConfig
}

// DumpConfig returns where or not to print the parsed configuration
func DumpConfig() bool {
return *dumpConfig
}
4 changes: 4 additions & 0 deletions packetbeat/beater/packetbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,3 +282,7 @@ func (pb *packetbeat) icmpConfig() (*common.Config, error) {

return icmp, nil
}

func (pb *packetbeat) Config() interface{} {
return pb.config
}

0 comments on commit 73e275d

Please sign in to comment.