Skip to content

Commit

Permalink
[cmd/opampsupervisor]: Write agent config and logs to storage directo…
Browse files Browse the repository at this point in the history
…ry (#34348)

**Description:*
* Writes the agent's `effective.yaml` and `agent.log` to the storage
directory.

**Link to tracking Issue:** Closes #34341

**Testing:**
* Added an e2e test testing that the supervisor writes the files to the
correct location
  • Loading branch information
BinaryFissionGames authored Aug 6, 2024
1 parent a790c77 commit 35f218e
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 13 deletions.
13 changes: 13 additions & 0 deletions .chloggen/fix_supervisor-write-files-to-storage.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: bug_fix

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: cmd/opampsupervisor

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Write the generated effective config and agent log files to the user-defined storage directory.

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [34341]
28 changes: 28 additions & 0 deletions cmd/opampsupervisor/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1191,6 +1191,34 @@ func TestSupervisorPersistsNewInstanceID(t *testing.T) {
require.Equal(t, newID, uuid.UUID(newRecievedAgentID))
}

func TestSupervisorWritesAgentFilesToStorageDir(t *testing.T) {
// Tests that the agent logs and effective.yaml are written under the storage directory.
storageDir := t.TempDir()

server := newOpAMPServer(
t,
defaultConnectingHandler,
server.ConnectionCallbacksStruct{},
)

s := newSupervisor(t, "basic", map[string]string{
"url": server.addr,
"storage_dir": storageDir,
})

waitForSupervisorConnection(server.supervisorConnected, true)

t.Logf("Supervisor connected")

s.Shutdown()

t.Logf("Supervisor shutdown")

// Check config and log files are written in storage dir
require.FileExists(t, filepath.Join(storageDir, "agent.log"))
require.FileExists(t, filepath.Join(storageDir, "effective.yaml"))
}

func findRandomPort() (int, error) {
l, err := net.Listen("tcp", "localhost:0")

Expand Down
7 changes: 5 additions & 2 deletions cmd/opampsupervisor/supervisor/commander/commander.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"os"
"os/exec"
"path/filepath"
"sync/atomic"
"syscall"
"time"
Expand All @@ -23,16 +24,18 @@ import (
type Commander struct {
logger *zap.Logger
cfg config.Agent
logsDir string
args []string
cmd *exec.Cmd
doneCh chan struct{}
exitCh chan struct{}
running *atomic.Int64
}

func NewCommander(logger *zap.Logger, cfg config.Agent, args ...string) (*Commander, error) {
func NewCommander(logger *zap.Logger, logsDir string, cfg config.Agent, args ...string) (*Commander, error) {
return &Commander{
logger: logger,
logsDir: logsDir,
cfg: cfg,
args: args,
running: &atomic.Int64{},
Expand Down Expand Up @@ -69,7 +72,7 @@ func (c *Commander) Start(ctx context.Context) error {

c.logger.Debug("Starting agent", zap.String("agent", c.cfg.Executable))

logFilePath := "agent.log"
logFilePath := filepath.Join(c.logsDir, "agent.log")
logFile, err := os.Create(logFilePath)
if err != nil {
return fmt.Errorf("cannot create %s: %w", logFilePath, err)
Expand Down
28 changes: 17 additions & 11 deletions cmd/opampsupervisor/supervisor/supervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ var (
)

const (
persistentStateFilePath = "persistent_state.yaml"
agentConfigFilePath = "effective.yaml"
persistentStateFileName = "persistent_state.yaml"
agentConfigFileName = "effective.yaml"
)

const maxBufferedCustomMessages = 10
Expand Down Expand Up @@ -166,7 +166,7 @@ func NewSupervisor(logger *zap.Logger, configFile string) (*Supervisor, error) {
}

var err error
s.persistentState, err = loadOrCreatePersistentState(s.persistentStateFile())
s.persistentState, err = loadOrCreatePersistentState(s.persistentStateFilePath())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -197,8 +197,9 @@ func NewSupervisor(logger *zap.Logger, configFile string) (*Supervisor, error) {

s.commander, err = commander.NewCommander(
s.logger,
s.config.Storage.Directory,
s.config.Agent,
"--config", agentConfigFilePath,
"--config", s.agentConfigFilePath(),
)
if err != nil {
return nil, err
Expand Down Expand Up @@ -278,7 +279,7 @@ func (s *Supervisor) getBootstrapInfo() (err error) {
return err
}

err = os.WriteFile(agentConfigFilePath, bootstrapConfig, 0600)
err = os.WriteFile(s.agentConfigFilePath(), bootstrapConfig, 0600)
if err != nil {
return fmt.Errorf("failed to write agent config: %w", err)
}
Expand Down Expand Up @@ -338,8 +339,9 @@ func (s *Supervisor) getBootstrapInfo() (err error) {

cmd, err := commander.NewCommander(
s.logger,
s.config.Storage.Directory,
s.config.Agent,
"--config", agentConfigFilePath,
"--config", s.agentConfigFilePath(),
)
if err != nil {
return err
Expand Down Expand Up @@ -801,7 +803,7 @@ func (s *Supervisor) loadAndWriteInitialMergedConfig() error {

// write the initial merged config to disk
cfg := s.mergedConfig.Load().(string)
if err := os.WriteFile(agentConfigFilePath, []byte(cfg), 0600); err != nil {
if err := os.WriteFile(s.agentConfigFilePath(), []byte(cfg), 0600); err != nil {
s.logger.Error("Failed to write agent config.", zap.Error(err))
}

Expand Down Expand Up @@ -1046,7 +1048,7 @@ func (s *Supervisor) healthCheck() {
}

func (s *Supervisor) runAgentProcess() {
if _, err := os.Stat(agentConfigFilePath); err == nil {
if _, err := os.Stat(s.agentConfigFilePath()); err == nil {
// We have an effective config file saved previously. Use it to start the agent.
s.logger.Debug("Effective config found, starting agent initial time")
s.startAgent()
Expand Down Expand Up @@ -1118,7 +1120,7 @@ func (s *Supervisor) stopAgentApplyConfig() {
s.logger.Error("Could not stop agent process", zap.Error(err))
}

if err := os.WriteFile(agentConfigFilePath, []byte(cfg), 0600); err != nil {
if err := os.WriteFile(s.agentConfigFilePath(), []byte(cfg), 0600); err != nil {
s.logger.Error("Failed to write agent config.", zap.Error(err))
}
}
Expand Down Expand Up @@ -1309,8 +1311,12 @@ func (s *Supervisor) processAgentIdentificationMessage(msg *protobufs.AgentIdent
return true
}

func (s *Supervisor) persistentStateFile() string {
return filepath.Join(s.config.Storage.Directory, persistentStateFilePath)
func (s *Supervisor) persistentStateFilePath() string {
return filepath.Join(s.config.Storage.Directory, persistentStateFileName)
}

func (s *Supervisor) agentConfigFilePath() string {
return filepath.Join(s.config.Storage.Directory, agentConfigFileName)
}

func (s *Supervisor) findRandomPort() (int, error) {
Expand Down

0 comments on commit 35f218e

Please sign in to comment.