diff --git a/x-pack/elastic-agent/pkg/agent/application/decorators.go b/x-pack/elastic-agent/pkg/agent/application/decorators.go deleted file mode 100644 index 4212759fea3..00000000000 --- a/x-pack/elastic-agent/pkg/agent/application/decorators.go +++ /dev/null @@ -1,65 +0,0 @@ -// 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 -} diff --git a/x-pack/elastic-agent/pkg/agent/application/decorators_test.go b/x-pack/elastic-agent/pkg/agent/application/decorators_test.go deleted file mode 100644 index 079a34a78f9..00000000000 --- a/x-pack/elastic-agent/pkg/agent/application/decorators_test.go +++ /dev/null @@ -1,183 +0,0 @@ -// 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) - } - } - }) -} diff --git a/x-pack/elastic-agent/pkg/agent/application/introspect_output_cmd.go b/x-pack/elastic-agent/pkg/agent/application/introspect_output_cmd.go index cc04b6ccd40..26eb424fe97 100644 --- a/x-pack/elastic-agent/pkg/agent/application/introspect_output_cmd.go +++ b/x-pack/elastic-agent/pkg/agent/application/introspect_output_cmd.go @@ -181,7 +181,7 @@ func getProgramsFromConfig(log *logger.Logger, cfg *config.Config) (map[string][ log, router, &configModifiers{ - Decorators: []decoratorFunc{injectMonitoring, injectPreferV2Template}, + Decorators: []decoratorFunc{injectMonitoring}, Filters: []filterFunc{filters.ConstraintFilter}, }, monitor, diff --git a/x-pack/elastic-agent/pkg/agent/application/local_mode.go b/x-pack/elastic-agent/pkg/agent/application/local_mode.go index 1475b447fa1..c0fe94b87f1 100644 --- a/x-pack/elastic-agent/pkg/agent/application/local_mode.go +++ b/x-pack/elastic-agent/pkg/agent/application/local_mode.go @@ -96,7 +96,7 @@ func newLocal( log, router, &configModifiers{ - Decorators: []decoratorFunc{injectMonitoring, injectPreferV2Template}, + Decorators: []decoratorFunc{injectMonitoring}, Filters: []filterFunc{filters.ConstraintFilter}, }, monitor, diff --git a/x-pack/elastic-agent/pkg/agent/application/managed_mode.go b/x-pack/elastic-agent/pkg/agent/application/managed_mode.go index 9942c34538b..84835e3d27d 100644 --- a/x-pack/elastic-agent/pkg/agent/application/managed_mode.go +++ b/x-pack/elastic-agent/pkg/agent/application/managed_mode.go @@ -136,7 +136,7 @@ func newManaged( log, router, &configModifiers{ - Decorators: []decoratorFunc{injectMonitoring, injectPreferV2Template}, + Decorators: []decoratorFunc{injectMonitoring}, Filters: []filterFunc{filters.ConstraintFilter}, }, monitor,