Skip to content

Commit 692f677

Browse files
douglascamatadragonlord93
authored andcommitted
[cmd/supervisor] Fix config merge and opamp server start order (open-telemetry#39949)
1 parent 74518cc commit 692f677

File tree

5 files changed

+94
-4
lines changed

5 files changed

+94
-4
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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: bug_fix
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: Fix bug in order of configuration composition and server start
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: [39949]
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+
First start the Supervisor's OpAMP server at a random port, then
20+
compose the configuration for the agent with that port.
21+
22+
# If your change doesn't affect end users or the exported elements of any package,
23+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
24+
# Optional: The change log or logs in which this entry should be included.
25+
# e.g. '[user]' or '[user, api]'
26+
# Include 'user' if the change is relevant to end users.
27+
# Include 'api' if there is a change to a library API.
28+
# Default: '[user]'
29+
change_logs: [user]

cmd/opampsupervisor/e2e_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ func defaultConnectingHandler(connectionCallbacks types.ConnectionCallbacks) fun
7878
}
7979
}
8080

81+
func getAgentLogs(t *testing.T, storageDir string) string {
82+
agentLogFile := filepath.Join(storageDir, "agent.log")
83+
agentLog, err := os.ReadFile(agentLogFile)
84+
require.NoError(t, err)
85+
return string(agentLog)
86+
}
87+
8188
// onConnectingFuncFactory is a function that will be given to types.ConnectionCallbacks as
8289
// OnConnectingFunc. This allows changing the ConnectionCallbacks both from the newOpAMPServer
8390
// caller and inside of newOpAMP Server, and for custom implementations of the value for `Accept`
@@ -274,6 +281,53 @@ func TestSupervisorStartsCollectorWithRemoteConfig(t *testing.T) {
274281
}, 10*time.Second, 500*time.Millisecond, "Log never appeared in output")
275282
}
276283

284+
func TestSupervisorStartsCollectorWithLocalConfigOnly(t *testing.T) {
285+
connected := atomic.Bool{}
286+
server := newOpAMPServer(t, defaultConnectingHandler, types.ConnectionCallbacks{
287+
OnConnected: func(_ context.Context, _ types.Connection) {
288+
connected.Store(true)
289+
},
290+
})
291+
292+
cfg, _, inputFile, outputFile := createSimplePipelineCollectorConf(t)
293+
294+
collectorConfigDir := t.TempDir()
295+
cfgFile, err := os.CreateTemp(collectorConfigDir, "config_*.yaml")
296+
t.Cleanup(func() { cfgFile.Close() })
297+
require.NoError(t, err)
298+
299+
_, err = cfgFile.Write(cfg.Bytes())
300+
require.NoError(t, err)
301+
302+
storageDir := t.TempDir()
303+
304+
s := newSupervisor(t, "basic", map[string]string{
305+
"url": server.addr,
306+
"storage_dir": storageDir,
307+
"local_config": cfgFile.Name(),
308+
})
309+
t.Cleanup(s.Shutdown)
310+
require.NoError(t, s.Start())
311+
312+
waitForSupervisorConnection(server.supervisorConnected, true)
313+
require.True(t, connected.Load(), "Supervisor failed to connect")
314+
315+
require.EventuallyWithTf(t, func(c *assert.CollectT) {
316+
require.Contains(c, getAgentLogs(t, storageDir), "Connected to the OpAMP server")
317+
}, 10*time.Second, 500*time.Millisecond, "Collector did not connected to the OpAMP server")
318+
319+
n, err := inputFile.WriteString("{\"body\":\"hello, world\"}\n")
320+
require.NotZero(t, n, "Could not write to input file")
321+
require.NoError(t, err)
322+
323+
require.Eventually(t, func() bool {
324+
logRecord := make([]byte, 1024)
325+
n, _ := outputFile.Read(logRecord)
326+
327+
return n != 0
328+
}, 10*time.Second, 500*time.Millisecond, "Log never appeared in output")
329+
}
330+
277331
func TestSupervisorStartsCollectorWithNoOpAMPServerWithNoLastRemoteConfig(t *testing.T) {
278332
storageDir := t.TempDir()
279333
t.Log("Storage dir:", storageDir)

cmd/opampsupervisor/supervisor/supervisor.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -348,15 +348,15 @@ func (s *Supervisor) Start() error {
348348
s.telemetrySettings.Logger.Info("Supervisor starting",
349349
zap.String("id", s.persistentState.InstanceID.String()))
350350

351+
if err = s.startOpAMP(); err != nil {
352+
return fmt.Errorf("cannot start OpAMP client: %w", err)
353+
}
354+
351355
err = s.loadAndWriteInitialMergedConfig()
352356
if err != nil {
353357
return fmt.Errorf("failed loading initial config: %w", err)
354358
}
355359

356-
if err = s.startOpAMP(); err != nil {
357-
return fmt.Errorf("cannot start OpAMP client: %w", err)
358-
}
359-
360360
s.commander, err = commander.NewCommander(
361361
s.telemetrySettings.Logger,
362362
s.config.Storage.Directory,

cmd/opampsupervisor/testdata/collector/simple_pipeline.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,6 @@ service:
1717
logs:
1818
receivers: [filelog]
1919
exporters: [file]
20+
telemetry:
21+
logs:
22+
level: debug

cmd/opampsupervisor/testdata/supervisor/supervisor_basic.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,7 @@ storage:
1616

1717
agent:
1818
executable: ../../bin/otelcontribcol_{{.goos}}_{{.goarch}}{{.extension}}
19+
{{- if .local_config }}
20+
config_files:
21+
- {{ .local_config }}
22+
{{- end }}

0 commit comments

Comments
 (0)