Skip to content

Commit

Permalink
chore: add updated_at in notification trace log
Browse files Browse the repository at this point in the history
  • Loading branch information
yashmehrotra committed Feb 13, 2025
1 parent 95a4ab2 commit 7d55432
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
23 changes: 13 additions & 10 deletions notification/cel.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,28 +53,31 @@ func (t *celVariables) SetSilenceURL(frontendURL string) {
}
}

func (t *celVariables) GetResourceHealth(ctx context.Context) (models.Health, bool, error) {
type ResourceHealthRow struct {
Health models.Health
DeletedAt *time.Time
UpdatedAt *time.Time
}

func (t *celVariables) GetResourceHealth(ctx context.Context) (ResourceHealthRow, error) {
var err error
var row struct {
Health models.Health
DeletedAt *time.Time
}
var row ResourceHealthRow
switch {
case t.ConfigItem != nil:
err = ctx.DB().Model(&models.ConfigItem{}).Select("health, deleted_at").Where("id = ?", t.ConfigItem.ID).Scan(&row).Error
err = ctx.DB().Model(&models.ConfigItem{}).Select("health, deleted_at", "updated_at").Where("id = ?", t.ConfigItem.ID).Scan(&row).Error
case t.Component != nil:
err = ctx.DB().Model(&models.Component{}).Select("health, deleted_at").Where("id = ?", t.Component.ID).Scan(&row).Error
err = ctx.DB().Model(&models.Component{}).Select("health, deleted_at", "updated_at").Where("id = ?", t.Component.ID).Scan(&row).Error
case t.Check != nil:
err = ctx.DB().Model(&models.Check{}).Select("status, deleted_at").Where("id = ?", t.Check.ID).Scan(&row).Error
err = ctx.DB().Model(&models.Check{}).Select("status, deleted_at", "updated_at").Where("id = ?", t.Check.ID).Scan(&row).Error
default:
return models.HealthUnknown, false, errors.New("no resource")
return ResourceHealthRow{}, errors.New("no resource")
}

if row.Health == "" {
row.Health = models.HealthUnknown
}

return row.Health, row.DeletedAt != nil, err
return row, err
}

func (t *celVariables) AsMap(ctx context.Context) map[string]any {
Expand Down
7 changes: 5 additions & 2 deletions notification/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,12 +347,15 @@ func shouldSkipNotificationDueToHealth(ctx context.Context, notif NotificationWi
// previousHealth is the health that triggered the notification event
previousHealth := api.EventToHealth(payload.EventName)

currentHealth, deleted, err := celEnv.GetResourceHealth(ctx)
resourceHealth, err := celEnv.GetResourceHealth(ctx)
if err != nil {
return false, fmt.Errorf("failed to get resource health from cel env: %w", err)
}
currentHealth := resourceHealth.Health
deleted := resourceHealth.DeletedAt != nil
relativeUpdatedAt := time.Since(lo.FromPtr(resourceHealth.UpdatedAt))

traceLog("NotificationID=%s HistoryID=%s Resource=[%s/%s] PreviousHealth=%s CurrentHealth=%s Checking if reportable", notif.ID, currentHistory.ID, payload.EventName, payload.ID, previousHealth, currentHealth)
traceLog("NotificationID=%s HistoryID=%s Resource=[%s/%s] PreviousHealth=%s CurrentHealth=%s UpdatedAt=%s RelativeUpdatedAtAgo=%s Checking if reportable", notif.ID, currentHistory.ID, payload.EventName, payload.ID, previousHealth, currentHealth, lo.FromPtr(resourceHealth.UpdatedAt), relativeUpdatedAt)
if !isHealthReportable(notif.Events, previousHealth, currentHealth) || deleted {
ctx.Logger.V(6).Infof("skipping notification[%s] as health change is not reportable", notif.ID)
traceLog("NotificationID=%s HistoryID=%s Resource=[%s/%s] PreviousHealth=%s CurrentHealth=%s ResourceDeleted=%v Skipping", notif.ID, currentHistory.ID, payload.EventName, payload.ID, previousHealth, currentHealth, deleted)
Expand Down

0 comments on commit 7d55432

Please sign in to comment.