-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the option to generate the template into a file (#4323)
Part of #3654. This adds two settings: `setup.template.output_to_file.path` and `setup.template.output_to_file.version`. The version refers to the ES version and is optional, we'll use the Beats version if not specified. I put it under `output_to_file` to make it clear that it only applies when outputting to a file: To generate a config, one can do: ``` ./metricbeat -e -E setup.template.output_to_file.path=template.json ``` In the current version, the Beat automatically stops after generating the template, but the output might be slightly confusing: ``` 2017/05/16 09:55:02.043671 load.go:116: INFO Template for Elasticsearch 6.0.0-alpha2 written to: template.json 2017/05/16 09:55:02.043717 beat.go:538: CRIT Exiting: Stopping after successfully writing the template to the file. Exiting: Stopping after successfully writing the template to the file. ``` IMO this is better than the alternative of leaving it running. To generate the template for the 2.x version, one can do: ``` ./metricbeat -e -E setup.template.output_to_file.path=template.json -E setup.template.output_to_file.version=2.4.0 ``` Remaining TODOs: * [x] System test * [x] Docs * [x] Changelog
- Loading branch information
Showing
9 changed files
with
264 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// +build !integration | ||
|
||
package template | ||
|
||
import ( | ||
"encoding/json" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/elastic/beats/libbeat/common" | ||
"github.com/elastic/beats/libbeat/version" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGenerateTemplate(t *testing.T) { | ||
|
||
// Load template | ||
absPath, err := filepath.Abs("../") | ||
assert.NotNil(t, absPath) | ||
assert.Nil(t, err) | ||
|
||
beatInfo := common.BeatInfo{ | ||
Beat: "testbeat", | ||
Version: version.GetDefaultVersion(), | ||
} | ||
|
||
dir, err := ioutil.TempDir("", "test-template") | ||
assert.NoError(t, err) | ||
defer os.RemoveAll(dir) | ||
path := filepath.Join(dir, "template.json") | ||
|
||
config := newConfigFrom(t, TemplateConfig{ | ||
Enabled: true, | ||
Fields: absPath + "/fields.yml", | ||
OutputToFile: OutputToFile{ | ||
Path: path, | ||
}, | ||
}) | ||
|
||
loader, err := NewLoader(config, nil, beatInfo) | ||
assert.NoError(t, err) | ||
|
||
err = loader.Generate() | ||
assert.NoError(t, err) | ||
|
||
// Read it back to check it | ||
fp, err := os.Open(path) | ||
assert.NoError(t, err) | ||
jsonParser := json.NewDecoder(fp) | ||
var parsed common.MapStr | ||
err = jsonParser.Decode(&parsed) | ||
assert.NoError(t, err) | ||
|
||
val, err := parsed.GetValue("mappings._default_._meta.version") | ||
assert.NoError(t, err) | ||
assert.Equal(t, val.(string), version.GetDefaultVersion()) | ||
|
||
} | ||
|
||
func TestGenerateTemplateWithVersion(t *testing.T) { | ||
|
||
// Load template | ||
absPath, err := filepath.Abs("../") | ||
assert.NotNil(t, absPath) | ||
assert.Nil(t, err) | ||
|
||
beatInfo := common.BeatInfo{ | ||
Beat: "testbeat", | ||
Version: version.GetDefaultVersion(), | ||
} | ||
|
||
dir, err := ioutil.TempDir("", "test-template") | ||
assert.NoError(t, err) | ||
defer os.RemoveAll(dir) | ||
path := filepath.Join(dir, "template.json") | ||
|
||
config := newConfigFrom(t, TemplateConfig{ | ||
Enabled: true, | ||
Fields: absPath + "/fields.yml", | ||
OutputToFile: OutputToFile{ | ||
Path: path, | ||
Version: "2.4.0", | ||
}, | ||
}) | ||
|
||
loader, err := NewLoader(config, nil, beatInfo) | ||
assert.NoError(t, err) | ||
|
||
err = loader.Generate() | ||
assert.NoError(t, err) | ||
|
||
// Read it back to check it | ||
fp, err := os.Open(path) | ||
assert.NoError(t, err) | ||
jsonParser := json.NewDecoder(fp) | ||
var parsed common.MapStr | ||
err = jsonParser.Decode(&parsed) | ||
assert.NoError(t, err) | ||
|
||
// check a setting specific to that version | ||
val, err := parsed.GetValue("mappings._default_._all.norms.enabled") | ||
assert.NoError(t, err) | ||
assert.Equal(t, val.(bool), false) | ||
} | ||
|
||
func newConfigFrom(t *testing.T, from interface{}) *common.Config { | ||
cfg, err := common.NewConfigFrom(from) | ||
assert.NoError(t, err) | ||
return cfg | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from base import BaseTest | ||
|
||
import os | ||
import json | ||
|
||
|
||
class Test(BaseTest): | ||
|
||
def test_generate_templates(self): | ||
""" | ||
Generates templates from other Beats. | ||
""" | ||
self.render_config_template() | ||
|
||
output_json = os.path.join(self.working_dir, "template.json") | ||
fields_yml = "../../../../fields.yml" | ||
|
||
exit_code = self.run_beat(extra_args=[ | ||
"-E", "setup.template.output_to_file.path={}".format(output_json), | ||
"-E", "setup.template.fields={}".format(fields_yml)]) | ||
assert exit_code == 1 | ||
|
||
# check json file | ||
with open(output_json) as f: | ||
tmpl = json.load(f) | ||
assert "mappings" in tmpl | ||
|
||
def test_generate_templates_v5(self): | ||
""" | ||
Generates templates from other Beats. | ||
""" | ||
self.render_config_template() | ||
|
||
output_json = os.path.join(self.working_dir, "template-5x.json") | ||
fields_yml = "../../../../fields.yml" | ||
|
||
exit_code = self.run_beat(extra_args=[ | ||
"-E", "setup.template.output_to_file.path={}".format(output_json), | ||
"-E", "setup.template.output_to_file.version=5.0.0".format(output_json), | ||
"-E", "setup.template.fields={}".format(fields_yml)]) | ||
assert exit_code == 1 | ||
|
||
# check json file | ||
with open(output_json) as f: | ||
tmpl = json.load(f) | ||
assert "mappings" in tmpl | ||
assert tmpl["mappings"]["_default_"]["_all"]["norms"]["enabled"] is False |