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

Port fields.yml collector to Golang #6911

Merged
merged 25 commits into from
May 30, 2018

Conversation

kvch
Copy link
Contributor

@kvch kvch commented Apr 20, 2018

Existing Python and shell code is ported to Golang. The previous Beat specific field collection is is generalized and moved to the Makefile of libbeat. A new variable is added to makefiles: FIELDS_FILE_PATH. This specifies where fields.yml are.

Misc

  • Packetbeat's fields_base.yml is renamed to fields.common.yml to be the same as in other Beats

Breaking change for community Beats

If your Beat has partial fields.yml files which need to be collected by make update, please fill in the FIELDS_FILE_PATH variable in your Makefile with the root directory of your fields files.

If you need a custom collector function you can implement it module_fields_collector.go with the following type signature:

func CollectModuleFiles(root string) ([]*fields.YmlFile, error)

@kvch kvch added in progress Pull request is currently in progress. review :Generator Related to code generators for building custom Beats or modules. labels Apr 20, 2018
"github.com/elastic/beats/filebeat/generator"
)

func Generate(module, fileset, modulesPath, beatsPath string) error {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported function Generate should have comment or be unexported

"github.com/elastic/beats/filebeat/generator"
)

func Generate(module, modulesPath, beatsPath string) error {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported function Generate should have comment or be unexported

"path/filepath"
)

func CollectModuleFiles(root string) ([]*YmlFile, error) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported function CollectModuleFiles should have comment or be unexported

"path/filepath"
)

func CollectFiles(root string) ([]*YmlFile, error) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported function CollectFiles should have comment or be unexported

return bytes.Join([][]byte{newline, content}, empty)
}

func Generate(beatsPath, beatName string, files []*YmlFile) error {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported function Generate should have comment or be unexported

return nil
}

func Generate(moduleName, filesetName, beatsPath string, noDoc bool) error {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported function Generate should have comment or be unexported


err := fields.Generate(moduleName, filesetName, beatsPath, noDoc)
if err != nil {
return fmt.Errorf("cannot generate fields.yml for %s/%s: %v\n", moduleName, filesetName, err)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

error strings should not be capitalized or end with punctuation or a newline


err := fileset.Generate(moduleName, filesetName, modulesPath, beatsPath)
if err != nil {
return fmt.Errorf("cannot generate fileset: %v\n", err)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

error strings should not be capitalized or end with punctuation or a newline


modulePath := path.Join(modulesPath, "module", moduleName)
if !generator.DirExists(modulePath) {
fmt.Errorf("cannot generate fileset: module not exists, please create module first by create-module command\n")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

error strings should not be capitalized or end with punctuation or a newline


err := module.Generate(name, modulesPath, beatsPath)
if err != nil {
return fmt.Errorf("cannot generate module: %v\n", err)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

error strings should not be capitalized or end with punctuation or a newline

@ruflin
Copy link
Member

ruflin commented Apr 20, 2018

Could this PR be split up in 2 parts:

  • Moving the make fields collection step to Golang
  • Introducing the subcommands

This should make reviewing easier and will separate the two different concerns.

@kvch kvch force-pushed the feature/filebeat/global-fields-yml branch from 2530189 to 21922fd Compare April 20, 2018 14:01
@kvch kvch mentioned this pull request Apr 20, 2018
2 tasks
@kvch
Copy link
Contributor Author

kvch commented Apr 20, 2018

@ruflin Done. Other PR: #6912

@kvch kvch changed the title Expose generators as subcommands && port fields.yml collector to Golang Port fields.yml collector to Golang Apr 20, 2018
Copy link
Member

@ruflin ruflin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for splitting up in 2 PR's. Review is probably to early but left some notes.

This is really great as soon all the fields.yml collection is directly in golang.

@@ -132,6 +133,10 @@ func New(b *beat.Beat, rawConfig *common.Config) (beat.Beater, error) {
return nil, err
}

b.CollectFieldsYmlCallback = func() ([]*fields.YmlFile, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leftover from the other PR?

@@ -5,6 +5,8 @@ SYSTEM_TESTS?=true
TEST_ENVIRONMENT?=true
GOX_FLAGS=-arch="amd64 386 arm ppc64 ppc64le"
ES_BEATS?=..
FIELDS_FILE_PATH=module
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this only support one path? I could see especially in libbeat or filebeat that a module, processor and inputs have fields.yml.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but it can be changed easily to support lists.

@@ -49,6 +51,12 @@ func New(b *beat.Beat, cfg *common.Config) (beat.Beater, error) {
scheduler: sched,
manager: manager,
}

pathToFields := filepath.Join("monitors", "active")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leftover?

@@ -418,6 +419,38 @@ func (b *Beat) Setup(bt beat.Creator, template, dashboards, machineLearning, pip
}())
}

// Setup registers ES index template and kibana dashboards
func (b *Beat) GenerateGlobalFields(bt beat.Creator, beatsPath string) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably also part of the other PR?

@kvch kvch force-pushed the feature/filebeat/global-fields-yml branch from 21922fd to 05cb292 Compare April 23, 2018 17:15
@kvch
Copy link
Contributor Author

kvch commented Apr 23, 2018

I removed the leftovers from the other PR and added tests. However, I have a Winlogbeat issue I am still investigating.

Copy link
Member

@ruflin ruflin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this is a breaking change for all our community beats, this should be documented on the CHANGELOG-developer with a note on how to migrate.

},
}

//processors := collectProcessorsFields(beatsPath)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be uncommented or removed? Same next line.

content = bytes.Replace(content, newline, c, -1)
content = bytes.TrimRight(content, " ")

return bytes.Join([][]byte{newline, content}, empty)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also add a newline after to make sure it ends with a newline?

func Generate(beatsPath, beatName string, files []*YmlFile) error {
files = collectBeatFiles(beatsPath, beatName, files)

err := os.MkdirAll(filepath.Join(beatsPath, beatName, "_meta"), 0644)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the global fields.yml end up outside the _meta directory?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Makefiles of Beats the generated file ends up under _meta. See for example Metricbeat: https://github.com/elastic/beats/blob/master/metricbeat/Makefile#L28

return files, nil
}

func collect(module os.FileInfo, files []*YmlFile, modulesPath string) []*YmlFile {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we call this collectModuleas this is quite specific to modules?

const (
generatedFieldsYml = "_meta/fields.generated.yml"
commonFieldsYml = "libbeat/_meta/fields.common.yml"
libbeatFields = "libbeat/processors/*/_meta/fields.yml"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this used somewhere except for the tests? Should this be moved to the tests?

@kvch kvch force-pushed the feature/filebeat/global-fields-yml branch from 05cb292 to 92e2e85 Compare April 26, 2018 17:21
generatedFieldsYml = "_meta/fields.generated.yml"
)

type YmlFile struct {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported type YmlFile should have comment or be unexported

@ruflin
Copy link
Member

ruflin commented Apr 27, 2018

I tried the code locally by run make clean and then make update in the beats directory and got the following error:

Updating generated files for libbeat
cat: _meta/beat.yml: No such file or directory
cat: _meta/beat.yml: No such file or directory
Generated global fields.yml under _meta/fields.generated.yml
ERROR: Field <error.> is duplicated. Please update and try again.exit status 1
make[1]: *** [update] Error 1
make: *** [update] Error 1

@ruflin
Copy link
Member

ruflin commented Apr 27, 2018

An other thing I spotted when testing is that the indentation of key: cloud is different from before. Looks like a general issue with the processor fields collection.

@kvch
Copy link
Contributor Author

kvch commented Apr 27, 2018

Yes, I started to refactor the code, so things got messed up and a few things are not working properly. I will ping you when it's ready for a next round of review.

@kvch kvch removed the review label May 3, 2018
@kvch kvch force-pushed the feature/filebeat/global-fields-yml branch 3 times, most recently from 11f5363 to f59fd4b Compare May 4, 2018 09:09
@kvch kvch added review and removed in progress Pull request is currently in progress. labels May 4, 2018
@kvch
Copy link
Contributor Author

kvch commented May 4, 2018

@ruflin I fixed the problems in the code. It should be ready to be reviewed.

@ruflin
Copy link
Member

ruflin commented May 4, 2018

@kvch Could you have a look at the generator tests? The change seems to break it.

@kvch kvch force-pushed the feature/filebeat/global-fields-yml branch 4 times, most recently from 2c5415d to 133509e Compare May 8, 2018 15:23
@kvch kvch force-pushed the feature/filebeat/global-fields-yml branch from 91794a4 to e982a79 Compare May 30, 2018 17:02
@jsoriano jsoriano merged commit 2b0df2f into elastic:master May 30, 2018
graphaelli added a commit to graphaelli/apm-server that referenced this pull request Jun 4, 2018
per elastic/beats#6911 set FIELDS_FILE_PATH accordingly.
graphaelli added a commit to elastic/apm-server that referenced this pull request Jun 6, 2018
* use beats provided fields target

per elastic/beats#6911 set FIELDS_FILE_PATH accordingly.

* Update beats framework to 245b3e1

* removes fields target from top-level Makefile
* vendors github.com/elastic/beats/libbeat/generator/fields
* cleans up github.com/ericchiang/k8s/...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:Generator Related to code generators for building custom Beats or modules. review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants