diff --git a/CHANGELOG.md b/CHANGELOG.md index 96146d696c4b..1b0e65443280 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -85,6 +85,9 @@ Main (unreleased) - Fix bug where custom headers were not actually being set in loki client. (@captncraig) +- Fix bug where `loki.source.kubernetes_events` unable to register as unhealthy + when there are failures for underlying informers. (@hainenber) + ### Other changes - Removed support for Windows 2012 in line with Microsoft end of life. (@mattdurham) diff --git a/component/loki/source/kubernetes_events/kubernetes_events.go b/component/loki/source/kubernetes_events/kubernetes_events.go index e409f6aebcdc..86a825c08970 100644 --- a/component/loki/source/kubernetes_events/kubernetes_events.go +++ b/component/loki/source/kubernetes_events/kubernetes_events.go @@ -93,11 +93,15 @@ type Component struct { receiversMut sync.RWMutex receivers []loki.LogsReceiver + + healthMut sync.RWMutex + health component.Health } var ( - _ component.Component = (*Component)(nil) - _ component.DebugComponent = (*Component)(nil) + _ component.Component = (*Component)(nil) + _ component.DebugComponent = (*Component)(nil) + _ component.HealthComponent = (*Component)(nil) ) // New creates a new loki.source.kubernetes_events component. @@ -152,6 +156,7 @@ func (c *Component) Run(ctx context.Context) error { c.tasksMut.RUnlock() if err := c.runner.ApplyTasks(ctx, tasks); err != nil { + c.setHealth(err) level.Error(c.log).Log("msg", "failed to apply event watchers", "err", err) } } @@ -180,7 +185,10 @@ func (c *Component) Run(ctx context.Context) error { cancel() }) - return rg.Run() + err := rg.Run() + c.setHealth(err) + + return err } // Update implements component.Component. @@ -255,3 +263,29 @@ func (c *Component) DebugInfo() interface{} { } return info } + +// CurrentHealth implements component.HealthComponent +func (c *Component) CurrentHealth() component.Health { + c.healthMut.RLock() + defer c.healthMut.RUnlock() + return c.health +} + +func (c *Component) setHealth(err error) { + c.healthMut.Lock() + defer c.healthMut.Unlock() + + if err == nil { + c.health = component.Health{ + Health: component.HealthTypeHealthy, + Message: "component is ready", + UpdateTime: time.Now(), + } + } else { + c.health = component.Health{ + Health: component.HealthTypeUnhealthy, + Message: fmt.Sprintf("component encounters error: %s", err), + UpdateTime: time.Now(), + } + } +}