Skip to content

Commit

Permalink
add support for an instance failing to be created
Browse files Browse the repository at this point in the history
  • Loading branch information
rfratto committed Jul 31, 2020
1 parent 509c644 commit 67daf47
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 19 deletions.
4 changes: 2 additions & 2 deletions pkg/integrations/manager_test.go
Expand Up @@ -137,8 +137,8 @@ func (i *mockIntegration) Run(ctx context.Context) error {
}
}

func mockInstanceFactory(_ instance.Config) prom.Instance {
return instance.NoOpInstance{}
func mockInstanceFactory(_ instance.Config) (prom.Instance, error) {
return instance.NoOpInstance{}, nil
}

func mockManagerConfig() Config {
Expand Down
9 changes: 2 additions & 7 deletions pkg/prom/agent.go
Expand Up @@ -143,13 +143,8 @@ func newAgent(cfg Config, logger log.Logger, fact instanceFactory) (*Agent, erro
}

// newInstance creates a new Instance given a config.
func (a *Agent) newInstance(c instance.Config) Instance {
inst, err := a.instanceFactory(a.cfg.Global, c, a.cfg.WALDir, a.logger)
if err != nil {
level.Error(a.logger).Log("msg", "failed to create instance", "err", err)
return nil
}
return inst
func (a *Agent) newInstance(c instance.Config) (Instance, error) {
return a.instanceFactory(a.cfg.Global, c, a.cfg.WALDir, a.logger)
}

func (a *Agent) validateInstance(c *instance.Config) error {
Expand Down
18 changes: 14 additions & 4 deletions pkg/prom/instance_manager.go
Expand Up @@ -89,7 +89,7 @@ func NewInstanceManager(cfg InstanceManagerConfig, logger log.Logger, launch Ins
}

// An InstanceFactory should return an unstarted instance given some config.
type InstanceFactory func(c instance.Config) Instance
type InstanceFactory func(c instance.Config) (Instance, error)

// A ConfigValidator should validate an instance.Config and return an error if
// a problem was found.
Expand Down Expand Up @@ -149,20 +149,28 @@ func (im *InstanceManager) ApplyConfig(c instance.Config) error {
}

// Spawn a new process for the new config.
im.spawnProcess(c)
err := im.spawnProcess(c)
if err != nil {
return err
}
currentActiveConfigs.Inc()
return nil
}

func (im *InstanceManager) spawnProcess(c instance.Config) {
func (im *InstanceManager) spawnProcess(c instance.Config) error {
ctx, cancel := context.WithCancel(context.Background())
done := make(chan bool)

inst, err := im.launch(c)
if err != nil {
return err
}

proc := &instanceManagerProcess{
cancel: cancel,
done: done,
cfg: c,
inst: im.launch(c),
inst: inst,
}

im.processes[c.Name] = proc
Expand All @@ -185,6 +193,8 @@ func (im *InstanceManager) spawnProcess(c instance.Config) {

currentActiveConfigs.Dec()
}()

return nil
}

// runProcess runs an instance and keeps it alive until the context is canceled.
Expand Down
8 changes: 2 additions & 6 deletions pkg/prom/instance_manager_test.go
Expand Up @@ -33,11 +33,7 @@ func TestInstanceManager_ApplyConfig(t *testing.T) {
}

func mockInstanceSpawner(fact *mockInstanceFactory) InstanceFactory {
return func(c instance.Config) Instance {
inst, err := fact.factory(config.DefaultGlobalConfig, c, "", nil)
if err != nil {
return nil
}
return inst
return func(c instance.Config) (Instance, error) {
return fact.factory(config.DefaultGlobalConfig, c, "", nil)
}
}

0 comments on commit 67daf47

Please sign in to comment.