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
27 changes: 27 additions & 0 deletions .chloggen/supervisor_restart_command_capability.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

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

# 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: Support AcceptsRestartCommand Capability.

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [21077]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
37 changes: 37 additions & 0 deletions cmd/opampsupervisor/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,3 +473,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