Skip to content

Commit

Permalink
Don't include full ES index template in errors (elastic#25743)
Browse files Browse the repository at this point in the history
* Don't include full ES index template in errors

Index templates in some beats can be very large (~1MB) and including the data in errors can use a lot of memory and also makes for very large log lines. If the error is recurring then this makes the effects worse. So this change removes the index template body from the error. Users that need to see the index template for debugging can use `<beatname> export template --es.version=1.2.3`.

Fixes elastic#25540

* Update expected log messages in tests
  • Loading branch information
andrewkroh authored and michalpristas committed Jun 17, 2021
1 parent 6f6302e commit 01f6530
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Fix handling of `file_selectors` in aws-s3 input. {pull}25792[25792]
- Fix ILM alias creation when write alias exists and initial index does not exist {pull}26143[26143]
- Include date separator in the filename prefix of `dateRotator` to make sure nothing gets purged accidentally {pull}26176[26176]
- Omit full index template from errors that occur while loading the template. {pull}25743[25743]
- In the script processor, the `decode_xml` and `decode_xml_wineventlog` processors are now available as `DecodeXML` and `DecodeXMLWineventlog` respectively.

*Auditbeat*
Expand Down
16 changes: 8 additions & 8 deletions libbeat/template/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ var (
}
)

//Loader interface for loading templates
// Loader interface for loading templates.
type Loader interface {
Load(config TemplateConfig, info beat.Info, fields []byte, migration bool) error
}
Expand Down Expand Up @@ -94,15 +94,15 @@ func newTemplateBuilder() *templateBuilder {
return &templateBuilder{log: logp.NewLogger("template")}
}

// Load checks if the index mapping template should be loaded
// Load checks if the index mapping template should be loaded.
// In case the template is not already loaded or overwriting is enabled, the
// template is built and written to index
// template is built and written to index.
func (l *ESLoader) Load(config TemplateConfig, info beat.Info, fields []byte, migration bool) error {
if l.client == nil {
return errors.New("can not load template without active Elasticsearch client")
}

//build template from config
// build template from config
tmpl, err := l.builder.template(config, info, l.client.GetVersion(), migration)
if err != nil || tmpl == nil {
return err
Expand All @@ -120,19 +120,19 @@ func (l *ESLoader) Load(config TemplateConfig, info beat.Info, fields []byte, mi
}

if exists && !config.Overwrite {
l.log.Infof("Template %s already exists and will not be overwritten.", templateName)
l.log.Infof("Template %q already exists and will not be overwritten.", templateName)
return nil
}

//loading template to ES
// loading template to ES
body, err := l.builder.buildBody(tmpl, config, fields)
if err != nil {
return err
}
if err := l.loadTemplate(templateName, config.Type, body); err != nil {
return fmt.Errorf("could not load template. Elasticsearch returned: %v. Template is: %s", err, body.StringToPrint())
return fmt.Errorf("failed to load template: %w", err)
}
l.log.Infof("template with name '%s' loaded.", templateName)
l.log.Infof("Template with name %q loaded.", templateName)
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions libbeat/tests/system/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def test_json_template(self):
proc = self.start_beat()
self.wait_until(lambda: self.log_contains("mockbeat start running."))
self.wait_until(lambda: self.log_contains("Loading json template from file"))
self.wait_until(lambda: self.log_contains("template with name 'bla' loaded"))
self.wait_until(lambda: self.log_contains('Template with name "bla" loaded.'))
proc.check_kill_and_wait()

result = es.transport.perform_request('GET', '/_template/' + template_name)
Expand Down Expand Up @@ -149,7 +149,7 @@ def test_template_default(self):
self.render_config()
proc = self.start_beat()
self.wait_until(lambda: self.log_contains("mockbeat start running."))
self.wait_until(lambda: self.log_contains("template with name 'mockbeat-9.9.9' loaded"))
self.wait_until(lambda: self.log_contains('Template with name "mockbeat-9.9.9" loaded.'))
self.wait_until(lambda: self.log_contains("PublishEvents: 1 events have been published"))
proc.check_kill_and_wait()

Expand Down

0 comments on commit 01f6530

Please sign in to comment.