Skip to content

Commit 63fb24c

Browse files
tpaschalisAkshayS198
authored andcommitted
supervisor: add support for including local configuration files (open-telemetry#38671)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description This PR enables the supervisor to read the local configuration files as per the spec, and merge those with other sources to be included in the final merged effective config. The local configuration files are read last, and as such, any values there take precedence over other sources. <!-- Issue number (e.g. open-telemetry#1234) or full URL to issue, if applicable. --> #### Link to tracking issue Fixes open-telemetry#37886 <!--Describe what testing was performed and which tests were added.--> #### Testing Amended the existing test for composing configuration to include two extra local files, and made sure the end-result is merged properly on effective_config.yaml <!--Please delete paragraphs that you did not use before submitting.--> --------- Signed-off-by: Paschalis T <[email protected]> Signed-off-by: Paschalis Tsilias <[email protected]>
1 parent 6581f90 commit 63fb24c

File tree

6 files changed

+79
-1
lines changed

6 files changed

+79
-1
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: opampsupervisor
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: add support for including local configuration files
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [37886]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: []

cmd/opampsupervisor/supervisor/supervisor.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
"go.opentelemetry.io/collector/config/configopaque"
3939
"go.opentelemetry.io/collector/config/configtelemetry"
4040
"go.opentelemetry.io/collector/config/configtls"
41+
"go.opentelemetry.io/collector/confmap"
4142
"go.opentelemetry.io/collector/pdata/pcommon"
4243
semconv "go.opentelemetry.io/collector/semconv/v1.21.0"
4344
"go.opentelemetry.io/contrib/bridges/otelzap"
@@ -1035,6 +1036,36 @@ func (s *Supervisor) composeOpAMPExtensionConfig() []byte {
10351036
return cfg.Bytes()
10361037
}
10371038

1039+
func (s *Supervisor) composeAgentConfigFiles() []byte {
1040+
conf := confmap.New()
1041+
1042+
for _, file := range s.config.Agent.ConfigFiles {
1043+
cfgBytes, err := os.ReadFile(file)
1044+
if err != nil {
1045+
s.telemetrySettings.Logger.Error("Could not read local config file", zap.Error(err))
1046+
continue
1047+
}
1048+
1049+
cfgMap, err := yaml.Parser().Unmarshal(cfgBytes)
1050+
if err != nil {
1051+
s.telemetrySettings.Logger.Error("Could not unmarshal local config file", zap.Error(err))
1052+
continue
1053+
}
1054+
err = conf.Merge(confmap.NewFromStringMap(cfgMap))
1055+
if err != nil {
1056+
s.telemetrySettings.Logger.Error("Could not merge local config file: "+file, zap.Error(err))
1057+
continue
1058+
}
1059+
}
1060+
1061+
b, err := yaml.Parser().Marshal(conf.ToStringMap())
1062+
if err != nil {
1063+
s.telemetrySettings.Logger.Error("Could not marshal merged local config files", zap.Error(err))
1064+
return []byte("")
1065+
}
1066+
return b
1067+
}
1068+
10381069
func (s *Supervisor) loadAndWriteInitialMergedConfig() error {
10391070
var lastRecvRemoteConfig, lastRecvOwnTelemetryConfig []byte
10401071
var err error
@@ -1228,6 +1259,10 @@ func (s *Supervisor) composeMergedConfig(config *protobufs.AgentRemoteConfig) (c
12281259
return false, err
12291260
}
12301261

1262+
if err = k.Load(rawbytes.Provider(s.composeAgentConfigFiles()), yaml.Parser(), koanf.WithMergeFunc(configMergeFunc)); err != nil {
1263+
return false, err
1264+
}
1265+
12311266
// The merged final result is our new merged config.
12321267
newMergedConfigBytes, err := k.Marshal(yaml.Parser())
12331268
if err != nil {

cmd/opampsupervisor/supervisor/supervisor_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ func Test_composeEffectiveConfig(t *testing.T) {
176176
s := Supervisor{
177177
telemetrySettings: newNopTelemetrySettings(),
178178
persistentState: &persistentState{},
179-
config: config.Supervisor{Capabilities: config.Capabilities{AcceptsRemoteConfig: acceptsRemoteConfig}},
179+
config: config.Supervisor{Capabilities: config.Capabilities{AcceptsRemoteConfig: acceptsRemoteConfig}, Agent: config.Agent{ConfigFiles: []string{"testdata/local_config1.yaml", "testdata/local_config2.yaml"}}},
180180
pidProvider: staticPIDProvider(1234),
181181
hasNewConfig: make(chan struct{}, 1),
182182
agentConfigOwnMetricsSection: &atomic.Value{},
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
receivers:
2+
journald:
3+
units:
4+
- ssh
5+
priority: info
6+
directory: /run/log/journal
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
exporters:
2+
file/2:
3+
path: ./foo

cmd/opampsupervisor/testdata/collector/effective_config.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
exporters:
22
file:
33
path: /test/logs/output.log
4+
file/2:
5+
path: ./foo
46
extensions:
57
health_check:
68
endpoint: localhost:8000
@@ -20,6 +22,11 @@ receivers:
2022
include:
2123
- /test/logs/input.log
2224
start_at: beginning
25+
journald:
26+
directory: /run/log/journal
27+
priority: info
28+
units:
29+
- ssh
2330
service:
2431
extensions:
2532
- health_check

0 commit comments

Comments
 (0)