Skip to content

Commit

Permalink
Log errors from v2 client errors channel.
Browse files Browse the repository at this point in the history
  • Loading branch information
blakerouse committed Jan 25, 2023
1 parent 50dad3d commit ba30459
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions x-pack/libbeat/management/managerV2.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ type BeatV2Manager struct {

logger *logp.Logger

// handles client errors
errCanceller context.CancelFunc

// track individual units given to us by the V2 API
mx sync.Mutex
units map[unitKey]*client.Unit
Expand Down Expand Up @@ -175,11 +178,18 @@ func (cm *BeatV2Manager) Start() error {
if !cm.Enabled() {
return fmt.Errorf("V2 Manager is disabled")
}
err := cm.client.Start(context.Background())
if cm.errCanceller != nil {
cm.errCanceller()
cm.errCanceller = nil
}
ctx := context.Background()
err := cm.client.Start(ctx)
if err != nil {
return fmt.Errorf("error starting connection to client")
}

ctx, canceller := context.WithCancel(ctx)
cm.errCanceller = canceller
go cm.watchErrChan(ctx)
cm.client.RegisterDiagnosticHook("beat-rendered-config", "the rendered config used by the beat", "beat-rendered-config.yml", "application/yaml", cm.handleDebugYaml)
go cm.unitListen()
cm.isRunning = true
Expand Down Expand Up @@ -325,6 +335,17 @@ func (cm *BeatV2Manager) deleteUnit(unit *client.Unit) {
// Private V2 implementation
// ================================

func (cm *BeatV2Manager) watchErrChan(ctx context.Context) {
for {
select {
case <-ctx.Done():
return
case err := <-cm.client.Errors():
cm.logger.Errorf("elastic-agent-client error: %s", err)
}
}
}

func (cm *BeatV2Manager) unitListen() {
const changeDebounce = 100 * time.Millisecond

Expand Down Expand Up @@ -405,6 +426,10 @@ func (cm *BeatV2Manager) stopBeat() {
}
cm.client.Stop()
cm.UpdateStatus(lbmanagement.Stopped, "Stopped")
if cm.errCanceller != nil {
cm.errCanceller()
cm.errCanceller = nil
}
}

func (cm *BeatV2Manager) reload(units map[unitKey]*client.Unit) {
Expand Down

0 comments on commit ba30459

Please sign in to comment.