Skip to content

Commit 85b7a36

Browse files
committed
fix(loki/src/k8s_events): correctly update health state for the component during operation
Signed-off-by: hainenber <dotronghai96@gmail.com>
1 parent e7b95cf commit 85b7a36

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ Main (unreleased)
8585

8686
- Fix bug where custom headers were not actually being set in loki client. (@captncraig)
8787

88+
- Fix bug where `loki.source.kubernetes_events` unable to register as unhealthy
89+
when there are failures for underlying informers. (@hainenber)
90+
8891
### Other changes
8992

9093
- Removed support for Windows 2012 in line with Microsoft end of life. (@mattdurham)

component/loki/source/kubernetes_events/kubernetes_events.go

+37-3
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,15 @@ type Component struct {
9393

9494
receiversMut sync.RWMutex
9595
receivers []loki.LogsReceiver
96+
97+
healthMut sync.RWMutex
98+
health component.Health
9699
}
97100

98101
var (
99-
_ component.Component = (*Component)(nil)
100-
_ component.DebugComponent = (*Component)(nil)
102+
_ component.Component = (*Component)(nil)
103+
_ component.DebugComponent = (*Component)(nil)
104+
_ component.HealthComponent = (*Component)(nil)
101105
)
102106

103107
// New creates a new loki.source.kubernetes_events component.
@@ -152,6 +156,7 @@ func (c *Component) Run(ctx context.Context) error {
152156
c.tasksMut.RUnlock()
153157

154158
if err := c.runner.ApplyTasks(ctx, tasks); err != nil {
159+
c.setHealth(err)
155160
level.Error(c.log).Log("msg", "failed to apply event watchers", "err", err)
156161
}
157162
}
@@ -180,7 +185,10 @@ func (c *Component) Run(ctx context.Context) error {
180185
cancel()
181186
})
182187

183-
return rg.Run()
188+
err := rg.Run()
189+
c.setHealth(err)
190+
191+
return err
184192
}
185193

186194
// Update implements component.Component.
@@ -255,3 +263,29 @@ func (c *Component) DebugInfo() interface{} {
255263
}
256264
return info
257265
}
266+
267+
// CurrentHealth implements component.HealthComponent
268+
func (c *Component) CurrentHealth() component.Health {
269+
c.healthMut.RLock()
270+
defer c.healthMut.RUnlock()
271+
return c.health
272+
}
273+
274+
func (c *Component) setHealth(err error) {
275+
c.healthMut.Lock()
276+
defer c.healthMut.Unlock()
277+
278+
if err == nil {
279+
c.health = component.Health{
280+
Health: component.HealthTypeHealthy,
281+
Message: "component is ready",
282+
UpdateTime: time.Now(),
283+
}
284+
} else {
285+
c.health = component.Health{
286+
Health: component.HealthTypeUnhealthy,
287+
Message: fmt.Sprintf("component encounters error: %s", err),
288+
UpdateTime: time.Now(),
289+
}
290+
}
291+
}

0 commit comments

Comments
 (0)