Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cmd/opampsupervisor] Support AcceptsRestartCommand Capability #30694

Merged
merged 13 commits into from
Apr 23, 2024
Merged
37 changes: 37 additions & 0 deletions cmd/opampsupervisor/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,3 +470,40 @@ func waitForSupervisorConnection(connection chan bool, connected bool) {
}
}
}

func TestSupervisorRestartCommand(t *testing.T) {

var connectedCount atomic.Int32
server := newOpAMPServer(
t,
defaultConnectingHandler,
server.ConnectionCallbacksStruct{
OnConnectedFunc: func(_ types.Connection) {
connectedCount.Add(1)
},
OnMessageFunc: func(_ types.Connection, message *protobufs.AgentToServer) *protobufs.ServerToAgent {
return &protobufs.ServerToAgent{}
},
})

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

waitForSupervisorConnection(server.supervisorConnected, true)

require.Eventually(t, func() bool {

return connectedCount.Load() == 1

evan-bradley marked this conversation as resolved.
Show resolved Hide resolved
}, 5*time.Second, 500*time.Millisecond, "Collector never connected")

server.sendToSupervisor(&protobufs.ServerToAgent{
Command: &protobufs.ServerToAgentCommand{
Type: protobufs.CommandType_CommandType_Restart,
},
})

require.Eventually(t, func() bool {
return s.LastRestartError() == nil
evan-bradley marked this conversation as resolved.
Show resolved Hide resolved
}, 10*time.Second, 250*time.Millisecond, "Collector didn't connect after restart")
}
1 change: 1 addition & 0 deletions cmd/opampsupervisor/supervisor/commander/commander.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func (c *Commander) Start(ctx context.Context) error {
}

func (c *Commander) Restart(ctx context.Context) error {
c.logger.Debug("Restarting agent", zap.String("agent", c.cfg.Executable))
if err := c.Stop(ctx); err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions cmd/opampsupervisor/supervisor/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Capabilities struct {
ReportsOwnMetrics *bool `mapstructure:"reports_own_metrics"`
ReportsHealth *bool `mapstructure:"reports_health"`
ReportsRemoteConfig *bool `mapstructure:"reports_remote_config"`
AcceptsRestartCommand *bool `mapstructure:"accepts_restart_command"`
}

type OpAMPServer struct {
Expand Down
22 changes: 20 additions & 2 deletions cmd/opampsupervisor/supervisor/supervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type Supervisor struct {
healthCheckTicker *backoff.Ticker
healthChecker *healthchecker.HTTPHealthChecker
lastHealthCheckErr error
lastRestartErr error

// Supervisor's own config.
config config.Supervisor
Expand Down Expand Up @@ -334,6 +335,10 @@ func (s *Supervisor) Capabilities() protobufs.AgentCapabilities {
if c.ReportsRemoteConfig != nil && *c.ReportsRemoteConfig {
supportedCapabilities |= protobufs.AgentCapabilities_AgentCapabilities_ReportsRemoteConfig
}

if c.AcceptsRestartCommand != nil && *c.AcceptsRestartCommand {
supportedCapabilities |= protobufs.AgentCapabilities_AgentCapabilities_AcceptsRestartCommand
}
}
return supportedCapabilities
}
Expand Down Expand Up @@ -373,8 +378,7 @@ func (s *Supervisor) startOpAMP() error {
OnCommandFunc: func(command *protobufs.ServerToAgentCommand) error {
cmdType := command.GetType()
if *cmdType.Enum() == protobufs.CommandType_CommandType_Restart {
// TODO: https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/21077
s.logger.Debug("Received restart command")
return s.handleRestartCommand()
}
return nil
},
Expand Down Expand Up @@ -604,6 +608,20 @@ func (s *Supervisor) recalcEffectiveConfig() (configChanged bool, err error) {
return configChanged, nil
}

func (s *Supervisor) handleRestartCommand() error {
err := s.commander.Restart(context.Background())
evan-bradley marked this conversation as resolved.
Show resolved Hide resolved
s.lastRestartErr = err
if err != nil {
s.logger.Error("Could not restart agent process", zap.Error(err))
}
return err
}

// Used in tests.
func (s *Supervisor) LastRestartError() error {
return s.lastRestartErr
}

func (s *Supervisor) startAgent() {
err := s.commander.Start(context.Background())
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ capabilities:
reports_health: true
accepts_remote_config: true
reports_remote_config: true
accepts_restart_command: true

agent:
executable: ../../bin/otelcontribcol_{{.goos}}_{{.goarch}}{{.extension}}
Loading