Skip to content

Commit

Permalink
Improve crash on failed agent initialization (#326)
Browse files Browse the repository at this point in the history
* Move fact gathering up and block on failed listen

* Add test to check for error on invalid amqp url
  • Loading branch information
rtorrero authored Mar 6, 2024
1 parent 6c52f80 commit 1564aea
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 18 deletions.
35 changes: 17 additions & 18 deletions internal/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,25 @@ func GetAgentID(fileSystem afero.Fs) (string, error) {

// Start the Agent. This will start the discovery ticker and the heartbeat ticker
func (a *Agent) Start(ctx context.Context) error {
gathererRegistry := gatherers.NewRegistry(gatherers.StandardGatherers())

c := factsengine.NewFactsEngine(a.config.AgentID, a.config.FactsServiceURL, *gathererRegistry)
log.Info("Starting fact gathering service...")
if err := c.Subscribe(); err != nil {
return err
}

g, groupCtx := errgroup.WithContext(ctx)

g.Go(func() error {
if err := c.Listen(groupCtx); err != nil {
return err
}

log.Info("fact gathering stopped.")
return nil
})

for _, d := range a.discoveries {
dLoop := d
g.Go(func() error {
Expand All @@ -93,8 +110,6 @@ func (a *Agent) Start(ctx context.Context) error {
return nil
})

gathererRegistry := gatherers.NewRegistry(gatherers.StandardGatherers())

log.Info("loading plugins")

pluginLoaders := gatherers.PluginLoaders{
Expand All @@ -111,22 +126,6 @@ func (a *Agent) Start(ctx context.Context) error {

gathererRegistry.AddGatherers(gatherersFromPlugins)

c := factsengine.NewFactsEngine(a.config.AgentID, a.config.FactsServiceURL, *gathererRegistry)

g.Go(func() error {
log.Info("Starting fact gathering service...")
if err := c.Subscribe(); err != nil {
return err
}

if err := c.Listen(groupCtx); err != nil {
return err
}

log.Info("fact gathering stopped.")
return nil
})

return g.Wait()
}

Expand Down
22 changes: 22 additions & 0 deletions internal/agent/agent_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package agent_test

import (
"context"
"testing"

"github.com/stretchr/testify/suite"
"github.com/trento-project/agent/internal/agent"
"github.com/trento-project/agent/internal/discovery"
"github.com/trento-project/agent/internal/discovery/collector"
"github.com/trento-project/agent/test/helpers"
)

Expand All @@ -23,3 +26,22 @@ func (suite *AgentTestSuite) TestAgentGetAgentID() {
suite.NoError(err)
suite.Equal(helpers.DummyAgentID, agentID)
}

func (suite *AgentTestSuite) TestAgentFailsWithInvalidFactsServiceURL() {
config := &agent.Config{
AgentID: helpers.DummyAgentID,
InstanceName: "test",
DiscoveriesConfig: &discovery.DiscoveriesConfig{
DiscoveriesPeriodsConfig: &discovery.DiscoveriesPeriodConfig{},
CollectorConfig: &collector.Config{},
},
FactsServiceURL: "amqp://trento:trento@localhost:12345/somevhost",
PluginsFolder: "/usr/etc/trento/plugins/",
}

agent, _ := agent.NewAgent(config)
ctx := context.Background()
err := agent.Start(ctx)

suite.ErrorContains(err, "connect: connection refused")
}

0 comments on commit 1564aea

Please sign in to comment.