diff --git a/changelog/fragments/1764873750-Merge-multiple-agent-keys-when-loading-config.yaml b/changelog/fragments/1764873750-Merge-multiple-agent-keys-when-loading-config.yaml new file mode 100644 index 00000000000..68b5335f7f1 --- /dev/null +++ b/changelog/fragments/1764873750-Merge-multiple-agent-keys-when-loading-config.yaml @@ -0,0 +1,32 @@ +# Kind can be one of: +# - breaking-change: a change to previously-documented behavior +# - deprecation: functionality that is being removed in a later release +# - bug-fix: fixes a problem in a previous version +# - enhancement: extends functionality but does not break or fix existing behavior +# - feature: new functionality +# - known-issue: problems that we are aware of in a given version +# - security: impacts on the security of a product or a user’s deployment. +# - upgrade: important information for someone upgrading from a prior version +# - other: does not fit into any of the other categories +kind: bug-fix + +# Change summary; a 80ish characters long description of the change. +summary: Merge multiple agent keys when loading config + +# Long description; in case the summary is not enough to describe the change +# this field accommodate a description without length limits. +# NOTE: This field will be rendered only for breaking-change and known-issue kinds at the moment. +#description: + +# Affected component; usually one of "elastic-agent", "fleet-server", "filebeat", "metricbeat", "auditbeat", "all", etc. +component: elastic-agent + +# PR URL; optional; the PR number that added the changeset. +# If not present is automatically filled by the tooling finding the PR where this changelog fragment has been added. +# NOTE: the tooling supports backports, so it's able to fill the original PR number instead of the backport PR number. +# Please provide it if you are adding a fragment for a different PR. +#pr: https://github.com/owner/repo/1234 + +# Issue URL; optional; the GitHub issue related to this changeset (either closes or is part of). +# If not present is automatically filled by the tooling with the issue linked to the PR number. +issue: https://github.com/elastic/elastic-agent/issues/3717 diff --git a/internal/pkg/agent/cmd/run_test.go b/internal/pkg/agent/cmd/run_test.go index f2782866cd0..a1f3bc296e2 100644 --- a/internal/pkg/agent/cmd/run_test.go +++ b/internal/pkg/agent/cmd/run_test.go @@ -5,14 +5,29 @@ package cmd import ( + _ "embed" "fmt" + "os" + "path/filepath" "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/elastic/elastic-agent-libs/logp" + "github.com/elastic/elastic-agent/internal/pkg/agent/application/paths" + "github.com/elastic/elastic-agent/internal/pkg/agent/configuration" monitoringCfg "github.com/elastic/elastic-agent/internal/pkg/core/monitoring/config" ) +var ( + //go:embed testdata/run/singlelogging.yaml + singleLoggingConfig []byte + + //go:embed testdata/run/splitlogging.yaml + splitLoggingConfig []byte +) + func Test_initTracer(t *testing.T) { tenPercentSamplingRate := float32(0.1) @@ -93,3 +108,49 @@ func Test_initTracer(t *testing.T) { }) } } + +func TestRunLoadConfig(t *testing.T) { + tests := []struct { + name string + file []byte + expect func() *configuration.Configuration + }{{ + name: "single logging entry", + file: singleLoggingConfig, + expect: func() *configuration.Configuration { + cfg := configuration.DefaultConfiguration() + cfg.Settings.LoggingConfig.Level = logp.DebugLevel + cfg.Settings.LoggingConfig.ToFiles = true + cfg.Settings.LoggingConfig.ToStderr = false + + return cfg + }, + }, { + name: "split logging entries", + file: splitLoggingConfig, + expect: func() *configuration.Configuration { + cfg := configuration.DefaultConfiguration() + cfg.Settings.LoggingConfig.Level = logp.DebugLevel + cfg.Settings.LoggingConfig.ToFiles = true + cfg.Settings.LoggingConfig.ToStderr = false + + return cfg + }, + }} + + origCfgDir := paths.Config() + t.Cleanup(func() { paths.SetConfig(origCfgDir) }) + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + dir := t.TempDir() + paths.SetConfig(dir) + err := os.WriteFile(filepath.Join(dir, paths.DefaultConfigName), tt.file, 0o644) + require.NoError(t, err) + + cfg, err := loadConfig(t.Context(), nil) + require.NoError(t, err) + require.Equal(t, tt.expect(), cfg) + }) + } +} diff --git a/internal/pkg/agent/cmd/testdata/run/singlelogging.yaml b/internal/pkg/agent/cmd/testdata/run/singlelogging.yaml new file mode 100644 index 00000000000..1165f8b5553 --- /dev/null +++ b/internal/pkg/agent/cmd/testdata/run/singlelogging.yaml @@ -0,0 +1,5 @@ +agent: + logging: + level: debug + to_files: true + to_stderr: false diff --git a/internal/pkg/agent/cmd/testdata/run/splitlogging.yaml b/internal/pkg/agent/cmd/testdata/run/splitlogging.yaml new file mode 100644 index 00000000000..8cf89a26780 --- /dev/null +++ b/internal/pkg/agent/cmd/testdata/run/splitlogging.yaml @@ -0,0 +1,5 @@ +agent.logging.level: debug +agent.logging.to_files: true +agent: + logging: + to_stderr: false diff --git a/internal/pkg/config/config.go b/internal/pkg/config/config.go index 457d66bebaa..a416f5819e7 100644 --- a/internal/pkg/config/config.go +++ b/internal/pkg/config/config.go @@ -56,6 +56,7 @@ var DefaultOptions = []interface{}{ ucfg.IgnoreCommas, ucfg.ResolveEnv, ucfg.VarExp, + ucfg.FieldMergeValues("agent"), VarSkipKeys("inputs", "outputs"), OTelKeys("connectors", "receivers", "processors", "exporters", "extensions", "service"), }