Skip to content

Commit bee420c

Browse files
Add default option to allow merging multiple agent config keys (#11619) (#11634)
(cherry picked from commit 5ad9f9b) Co-authored-by: Michel Laterman <[email protected]>
1 parent 17ff7db commit bee420c

File tree

5 files changed

+104
-0
lines changed

5 files changed

+104
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Kind can be one of:
2+
# - breaking-change: a change to previously-documented behavior
3+
# - deprecation: functionality that is being removed in a later release
4+
# - bug-fix: fixes a problem in a previous version
5+
# - enhancement: extends functionality but does not break or fix existing behavior
6+
# - feature: new functionality
7+
# - known-issue: problems that we are aware of in a given version
8+
# - security: impacts on the security of a product or a user’s deployment.
9+
# - upgrade: important information for someone upgrading from a prior version
10+
# - other: does not fit into any of the other categories
11+
kind: bug-fix
12+
13+
# Change summary; a 80ish characters long description of the change.
14+
summary: Merge multiple agent keys when loading config
15+
16+
# Long description; in case the summary is not enough to describe the change
17+
# this field accommodate a description without length limits.
18+
# NOTE: This field will be rendered only for breaking-change and known-issue kinds at the moment.
19+
#description:
20+
21+
# Affected component; usually one of "elastic-agent", "fleet-server", "filebeat", "metricbeat", "auditbeat", "all", etc.
22+
component: elastic-agent
23+
24+
# PR URL; optional; the PR number that added the changeset.
25+
# If not present is automatically filled by the tooling finding the PR where this changelog fragment has been added.
26+
# NOTE: the tooling supports backports, so it's able to fill the original PR number instead of the backport PR number.
27+
# Please provide it if you are adding a fragment for a different PR.
28+
#pr: https://github.com/owner/repo/1234
29+
30+
# Issue URL; optional; the GitHub issue related to this changeset (either closes or is part of).
31+
# If not present is automatically filled by the tooling with the issue linked to the PR number.
32+
issue: https://github.com/elastic/elastic-agent/issues/3717

internal/pkg/agent/cmd/run_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,29 @@
55
package cmd
66

77
import (
8+
_ "embed"
89
"fmt"
10+
"os"
11+
"path/filepath"
912
"testing"
1013

1114
"github.com/stretchr/testify/assert"
15+
"github.com/stretchr/testify/require"
1216

17+
"github.com/elastic/elastic-agent-libs/logp"
18+
"github.com/elastic/elastic-agent/internal/pkg/agent/application/paths"
19+
"github.com/elastic/elastic-agent/internal/pkg/agent/configuration"
1320
monitoringCfg "github.com/elastic/elastic-agent/internal/pkg/core/monitoring/config"
1421
)
1522

23+
var (
24+
//go:embed testdata/run/singlelogging.yaml
25+
singleLoggingConfig []byte
26+
27+
//go:embed testdata/run/splitlogging.yaml
28+
splitLoggingConfig []byte
29+
)
30+
1631
func Test_initTracer(t *testing.T) {
1732
tenPercentSamplingRate := float32(0.1)
1833

@@ -93,3 +108,49 @@ func Test_initTracer(t *testing.T) {
93108
})
94109
}
95110
}
111+
112+
func TestRunLoadConfig(t *testing.T) {
113+
tests := []struct {
114+
name string
115+
file []byte
116+
expect func() *configuration.Configuration
117+
}{{
118+
name: "single logging entry",
119+
file: singleLoggingConfig,
120+
expect: func() *configuration.Configuration {
121+
cfg := configuration.DefaultConfiguration()
122+
cfg.Settings.LoggingConfig.Level = logp.DebugLevel
123+
cfg.Settings.LoggingConfig.ToFiles = true
124+
cfg.Settings.LoggingConfig.ToStderr = false
125+
126+
return cfg
127+
},
128+
}, {
129+
name: "split logging entries",
130+
file: splitLoggingConfig,
131+
expect: func() *configuration.Configuration {
132+
cfg := configuration.DefaultConfiguration()
133+
cfg.Settings.LoggingConfig.Level = logp.DebugLevel
134+
cfg.Settings.LoggingConfig.ToFiles = true
135+
cfg.Settings.LoggingConfig.ToStderr = false
136+
137+
return cfg
138+
},
139+
}}
140+
141+
origCfgDir := paths.Config()
142+
t.Cleanup(func() { paths.SetConfig(origCfgDir) })
143+
144+
for _, tt := range tests {
145+
t.Run(tt.name, func(t *testing.T) {
146+
dir := t.TempDir()
147+
paths.SetConfig(dir)
148+
err := os.WriteFile(filepath.Join(dir, paths.DefaultConfigName), tt.file, 0o644)
149+
require.NoError(t, err)
150+
151+
cfg, err := loadConfig(t.Context(), nil)
152+
require.NoError(t, err)
153+
require.Equal(t, tt.expect(), cfg)
154+
})
155+
}
156+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
agent:
2+
logging:
3+
level: debug
4+
to_files: true
5+
to_stderr: false
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
agent.logging.level: debug
2+
agent.logging.to_files: true
3+
agent:
4+
logging:
5+
to_stderr: false

internal/pkg/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ var DefaultOptions = []interface{}{
5656
ucfg.IgnoreCommas,
5757
ucfg.ResolveEnv,
5858
ucfg.VarExp,
59+
ucfg.FieldMergeValues("agent"),
5960
VarSkipKeys("inputs", "outputs"),
6061
OTelKeys("connectors", "receivers", "processors", "exporters", "extensions", "service"),
6162
}

0 commit comments

Comments
 (0)