Skip to content

Commit 7e1bdff

Browse files
authored
Merge pull request #75 from thin-edge/fix-healthcheck-exec-die-events
fix(monitor): ignore all exec event types not directly related to the main process
2 parents beeb035 + 9585ac8 commit 7e1bdff

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

pkg/app/app.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,19 @@ func (a *App) Monitor(ctx context.Context, filterOptions container.FilterOptions
282282
case evt := <-evtCh:
283283
switch evt.Type {
284284
case events.ContainerEventType:
285+
// Note: health checks will run command inside the containers periodically, and this results
286+
// in periodic exec_die events, however these events are interesting (as we're already listening to the health status transitions)
287+
// Check if the exec dying was related to the container's main exec or not.
288+
// Log event on debug level so it does not spam the logs
289+
if _, isRelatedToExec := evt.Actor.Attributes["execID"]; isRelatedToExec {
290+
// Just check for the prefix, as some events will have additional context appended to the status
291+
if strings.HasPrefix(string(evt.Action), "exec_") {
292+
slog.Debug("Ignoring event.", "value", evt)
293+
continue
294+
}
295+
}
296+
285297
payload := make(map[string]any)
286-
skipPublish := false
287298
if action, ok := ContainerEventText[evt.Action]; ok {
288299
props := getEventAttributes(evt.Actor.Attributes, "name", "image", "com.docker.compose.project")
289300
name := props[0]
@@ -303,13 +314,7 @@ func (a *App) Monitor(ctx context.Context, filterOptions container.FilterOptions
303314
}
304315

305316
switch evt.Action {
306-
case events.ActionExecDie:
307-
// Check if the exec dying was related to the container's main exec or not
308-
if _, hasExecID := evt.Actor.Attributes["execID"]; !hasExecID {
309-
skipPublish = true
310-
}
311-
fallthrough
312-
case events.ActionCreate, events.ActionStart, events.ActionStop, events.ActionPause, events.ActionUnPause, events.ActionHealthStatusHealthy, events.ActionHealthStatusUnhealthy:
317+
case events.ActionExecDie, events.ActionCreate, events.ActionStart, events.ActionStop, events.ActionPause, events.ActionUnPause, events.ActionHealthStatusHealthy, events.ActionHealthStatusUnhealthy:
313318
go func() {
314319
// Delay before trigger update to allow the service status to be updated
315320
time.Sleep(500 * time.Millisecond)
@@ -333,7 +338,7 @@ func (a *App) Monitor(ctx context.Context, filterOptions container.FilterOptions
333338
}
334339

335340
if a.config.EnableEngineEvents {
336-
if len(payload) > 0 && !skipPublish {
341+
if len(payload) > 0 {
337342
if err := a.client.Publish(tedge.GetTopic(a.client.Target, "e", string(evt.Action)), 1, false, mustMarshalJSON(payload)); err != nil {
338343
slog.Warn("Failed to publish container event.", "err", err)
339344
}

0 commit comments

Comments
 (0)