Repository: prometheus/alertmanager
File: notify/notify.go (Go)

Code before:

func (r NotifyReason) String() string {
	switch r {
	case ReasonDoNotNotify:
		return "none"
	case ReasonFirstNotification:
		return "first notification"
	case ReasonNewAlertsInGroup:
		return "new alerts added"
	case ReasonNewResolvedAlerts:
		return "some alerts resolved"
	case ReasonAllAlertsResolved:
		return "all alerts resolved"
	case ReasonRepeatIntervalElapsed:
		return "repeat interval elapsed"
	default:
		return "unknown"
	}
}

func (n *DedupStage) needsUpdate(entry *nflogpb.Entry, firing, resolved map[uint64]struct{}, repeat time.Duration) NotifyReason {
	// If we haven't notified about the alert group before, notify right away
	// unless we only have resolved alerts.
	if entry == nil {
		newAlerts := len(firing) > 0
		if newAlerts {
			return ReasonFirstNotification
		}
		return ReasonDoNotNotify
	}

	// new alerts in the group
	if !entry.IsFiringSubset(firing) {
		// If the previous entry has no firing alerts, it was a reason and we
		// should treat thisas the first notification for the group.
		if len(entry.FiringAlerts) == 0 {
			return ReasonFirstNotification
		}
		return ReasonNewAlertsInGroup
	}

	// Notify about all alerts being resolved.
	// This is done irrespective of the send_resolved flag to make sure that
	// the firing alerts are cleared from the notification log.
	if len(firing) == 0 {
		// If the current alert group and last notification contain no firing
		// alert, it means that some alerts have been fired and resolved during the
		// last interval. In this case, there is no need to notify the receiver
		// since it doesn't know about them.
		if len(entry.FiringAlerts) > 0 {
			return ReasonAllAlertsResolved

Review comment (suggestion):
Looks to me like `newAlerts` is used exactly once so why not:

```suggestion
		if len(firing) > 0 {
```

Or `newAlerts := len(firing)` and then use `newAlerts` in comparison conditions on line 768 and 787


Code after:

func (r NotifyReason) String() string {
	switch r {
	case ReasonDoNotNotify:
		return "none"
	case ReasonFirstNotification:
		return "first notification"
	case ReasonNewAlertsInGroup:
		return "new alerts added"
	case ReasonNewResolvedAlerts:
		return "some alerts resolved"
	case ReasonAllAlertsResolved:
		return "all alerts resolved"
	case ReasonRepeatIntervalElapsed:
		return "repeat interval elapsed"
	default:
		return "unknown"
	}
}

func (n *DedupStage) needsUpdate(entry *nflogpb.Entry, firing, resolved map[uint64]struct{}, repeat time.Duration) NotifyReason {
	// If we haven't notified about the alert group before, notify right away
	// unless we only have resolved alerts.
	if entry == nil {
		if len(firing) > 0 {
			return ReasonFirstNotification
		}
		return ReasonDoNotNotify
	}

	// new alerts in the group
	if !entry.IsFiringSubset(firing) {
		// If the previous entry has no firing alerts, it was a resolution and we
		// should treat this as the first notification for the group.
		if len(entry.FiringAlerts) == 0 {
			return ReasonFirstNotification
		}
		return ReasonNewAlertsInGroup
	}

	// Notify about all alerts being resolved.
	// This is done irrespective of the send_resolved flag to make sure that
	// the firing alerts are cleared from the notification log.
	if len(firing) == 0 {
		// If the current alert group and last notification contain no firing
		// alert, it means that some alerts have been fired and resolved during the
		// last interval. In this case, there is no need to notify the receiver
		// since it doesn't know about them.
		if len(entry.FiringAlerts) > 0 {
			return ReasonAllAlertsResolved
		}