forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Elastic Agent] Add params
prefer_v2_templates=true
on the ES output
To make sure we use the v2 templates we must send on bulk request the params `prefer_v2_templates=true` if not it will default to v1 and will not try to use the v2 template at all. Reference: elastic#17809
- Loading branch information
Showing
6 changed files
with
260 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
package application | ||
|
||
import ( | ||
"github.com/elastic/beats/v7/libbeat/common" | ||
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/program" | ||
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/transpiler" | ||
) | ||
|
||
func injectPreferV2Template( | ||
_ string, | ||
_ *transpiler.AST, | ||
programsToRun []program.Program, | ||
) ([]program.Program, error) { | ||
|
||
const ( | ||
outputKey = "output" | ||
elasticsearchKey = "elasticsearch" | ||
paramsKey = "parameters" | ||
elasticsearchOutputKey = outputKey + "." + elasticsearchKey | ||
) | ||
|
||
params := common.MapStr{ | ||
"output": common.MapStr{ | ||
"elasticsearch": common.MapStr{ | ||
"parameters": common.MapStr{ | ||
"prefer_v2_templates": true, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
programList := make([]program.Program, 0, len(programsToRun)) | ||
|
||
for _, program := range programsToRun { | ||
if _, found := transpiler.Lookup(program.Config, elasticsearchOutputKey); !found { | ||
programList = append(programList, program) | ||
continue | ||
} | ||
|
||
m, err := program.Config.Map() | ||
if err != nil { | ||
return programsToRun, err | ||
} | ||
|
||
// Add prefer_v2_templates to every bulk request on the elasticsearch output. | ||
// without this Elasticsearch will fallback to v1 templates even if the index matches existing v2. | ||
mStr := common.MapStr(m) | ||
mStr.DeepUpdate(params) | ||
|
||
a, err := transpiler.NewAST(mStr) | ||
if err != nil { | ||
return programsToRun, err | ||
} | ||
|
||
program.Config = a | ||
|
||
programList = append(programList, program) | ||
} | ||
|
||
return programList, nil | ||
} |
183 changes: 183 additions & 0 deletions
183
x-pack/elastic-agent/pkg/agent/application/decorators_test.go
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,183 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
package application | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/program" | ||
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/transpiler" | ||
) | ||
|
||
func TestInjectV2Templates(t *testing.T) { | ||
const outputGroup = "default" | ||
t.Run("inject parameters on elasticsearch output", func(t *testing.T) { | ||
|
||
config := map[string]interface{}{ | ||
"outputs": map[string]interface{}{ | ||
"default": map[string]interface{}{ | ||
"type": "elasticsearch", | ||
"username": "foo", | ||
"password": "secret", | ||
}, | ||
}, | ||
"datasources": []map[string]interface{}{ | ||
map[string]interface{}{ | ||
"inputs": []map[string]interface{}{ | ||
map[string]interface{}{ | ||
"type": "log", | ||
"streams": []map[string]interface{}{ | ||
map[string]interface{}{"paths": "/xxxx"}, | ||
}, | ||
"processors": []interface{}{ | ||
map[string]interface{}{ | ||
"dissect": map[string]interface{}{ | ||
"tokenizer": "---", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
map[string]interface{}{ | ||
"inputs": []map[string]interface{}{ | ||
map[string]interface{}{ | ||
"type": "system/metrics", | ||
"streams": []map[string]interface{}{ | ||
map[string]interface{}{ | ||
"id": "system/metrics-system.core", | ||
"enabled": true, | ||
"dataset": "system.core", | ||
"period": "10s", | ||
"metrics": []string{"percentages"}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"use_output": "default", | ||
}, | ||
}, | ||
} | ||
|
||
ast, err := transpiler.NewAST(config) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
programsToRun, err := program.Programs(ast) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
programsWithOutput, err := injectPreferV2Template(outputGroup, ast, programsToRun[outputGroup]) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, len(programsToRun[outputGroup]), len(programsWithOutput)) | ||
|
||
for _, program := range programsWithOutput { | ||
m, err := program.Config.Map() | ||
require.NoError(t, err) | ||
|
||
want := map[string]interface{}{ | ||
"elasticsearch": map[string]interface{}{ | ||
"username": "foo", | ||
"password": "secret", | ||
"parameters": map[string]interface{}{ | ||
"prefer_v2_templates": true, | ||
}, | ||
}, | ||
} | ||
|
||
got, _ := m["output"] | ||
if diff := cmp.Diff(want, got); diff != "" { | ||
t.Errorf("output mismatch (-want +got):\n%s", diff) | ||
} | ||
} | ||
}) | ||
|
||
t.Run("dont do anything on logstash output", func(t *testing.T) { | ||
config := map[string]interface{}{ | ||
"outputs": map[string]interface{}{ | ||
"default": map[string]interface{}{ | ||
"type": "logstash", | ||
"username": "foo", | ||
"password": "secret", | ||
}, | ||
}, | ||
"datasources": []map[string]interface{}{ | ||
map[string]interface{}{ | ||
"inputs": []map[string]interface{}{ | ||
map[string]interface{}{ | ||
"type": "log", | ||
"streams": []map[string]interface{}{ | ||
map[string]interface{}{"paths": "/xxxx"}, | ||
}, | ||
"processors": []interface{}{ | ||
map[string]interface{}{ | ||
"dissect": map[string]interface{}{ | ||
"tokenizer": "---", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
map[string]interface{}{ | ||
"inputs": []map[string]interface{}{ | ||
map[string]interface{}{ | ||
"type": "system/metrics", | ||
"streams": []map[string]interface{}{ | ||
map[string]interface{}{ | ||
"id": "system/metrics-system.core", | ||
"enabled": true, | ||
"dataset": "system.core", | ||
"period": "10s", | ||
"metrics": []string{"percentages"}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"use_output": "default", | ||
}, | ||
}, | ||
} | ||
|
||
ast, err := transpiler.NewAST(config) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
programsToRun, err := program.Programs(ast) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
programsWithOutput, err := injectPreferV2Template(outputGroup, ast, programsToRun[outputGroup]) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, len(programsToRun[outputGroup]), len(programsWithOutput)) | ||
|
||
for _, program := range programsWithOutput { | ||
m, err := program.Config.Map() | ||
require.NoError(t, err) | ||
|
||
want := map[string]interface{}{ | ||
"logstash": map[string]interface{}{ | ||
"username": "foo", | ||
"password": "secret", | ||
}, | ||
} | ||
|
||
got, _ := m["output"] | ||
if diff := cmp.Diff(want, got); diff != "" { | ||
t.Errorf("output mismatch (-want +got):\n%s", diff) | ||
} | ||
} | ||
}) | ||
} |
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