Skip to content

Commit

Permalink
fix(loki/src/k8s_events): correctly update health state for the compo…
Browse files Browse the repository at this point in the history
…nent during operation

Signed-off-by: hainenber <[email protected]>
  • Loading branch information
hainenber committed Feb 17, 2024
1 parent e7b95cf commit 85b7a36
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
40 changes: 37 additions & 3 deletions component/loki/source/kubernetes_events/kubernetes_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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(),
}
}
}

0 comments on commit 85b7a36

Please sign in to comment.