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

Code before:
	defer ih.mtx.RUnlock()
	if ih.cancel != nil {
		ih.cancel()
	}
}

// Mutes returns true iff the given label set is muted. It implements the Muter
// interface.
func (ih *Inhibitor) Mutes(lset model.LabelSet) bool {
	start := time.Now()
	fp := lset.Fingerprint()

	for _, r := range ih.rules {
		ruleStart := time.Now()
		if !r.TargetMatchers.Matches(lset) {
			// If target side of rule doesn't match, we don't need to look any further.
			r.metrics.matchesDurationNotMatched.Observe(time.Since(ruleStart).Seconds())
			continue
		}
		r.metrics.matchesDurationMatched.Observe(time.Since(ruleStart).Seconds())
		// If we are here, the target side matches. If the source side matches, too, we
		// need to exclude inhibiting alerts for which the same is true.
		if inhibitedByFP, eq := r.hasEqual(lset, r.SourceMatchers.Matches(lset), ruleStart); eq {
			ih.marker.SetInhibited(fp, inhibitedByFP.String())
			now := time.Now()
			sinceStart := now.Sub(now)
			sinceRuleStart := now.Sub(ruleStart)
			ih.metrics.mutesDurationMuted.Observe(sinceStart.Seconds())
			r.metrics.mutesDurationMuted.Observe(sinceRuleStart.Seconds())
			return true
		}
		r.metrics.mutesDurationNotMuted.Observe(time.Since(ruleStart).Seconds())
	}
	ih.marker.SetInhibited(fp)
	ih.metrics.mutesDurationNotMuted.Observe(time.Since(start).Seconds())

	return false
}

// An InhibitRule specifies that a class of (source) alerts should inhibit
// notifications for another class of (target) alerts if all specified matching
// labels are equal between the two alerts. This may be used to inhibit alerts
// from sending notifications if their meaning is logically a subset of a
// higher-level alert.
type InhibitRule struct {
	// Name is an optional name for the inhibition rule.
	Name string
	// The set of Filters which define the group of source alerts (which inhibit
	// the target alerts).
	SourceMatchers labels.Matchers
	// The set of Filters which define the group of target alerts (which are

Review comment (suggestion):
I think this is the supposed calculation:
```suggestion
			sinceStart := now.Sub(start)
```

Code after:
	defer ih.mtx.RUnlock()
	if ih.cancel != nil {
		ih.cancel()
	}
}

// Mutes returns true iff the given label set is muted. It implements the Muter
// interface.
func (ih *Inhibitor) Mutes(lset model.LabelSet) bool {
	start := time.Now()
	fp := lset.Fingerprint()

	for _, r := range ih.rules {
		ruleStart := time.Now()
		if !r.TargetMatchers.Matches(lset) {
			// If target side of rule doesn't match, we don't need to look any further.
			r.metrics.matchesDurationNotMatched.Observe(time.Since(ruleStart).Seconds())
			continue
		}
		r.metrics.matchesDurationMatched.Observe(time.Since(ruleStart).Seconds())
		// If we are here, the target side matches. If the source side matches, too, we
		// need to exclude inhibiting alerts for which the same is true.
		if inhibitedByFP, eq := r.hasEqual(lset, r.SourceMatchers.Matches(lset), ruleStart); eq {
			ih.marker.SetInhibited(fp, inhibitedByFP.String())
			now := time.Now()
			sinceStart := now.Sub(start)
			sinceRuleStart := now.Sub(ruleStart)
			ih.metrics.mutesDurationMuted.Observe(sinceStart.Seconds())
			r.metrics.mutesDurationMuted.Observe(sinceRuleStart.Seconds())
			return true
		}
		r.metrics.mutesDurationNotMuted.Observe(time.Since(ruleStart).Seconds())
	}
	ih.marker.SetInhibited(fp)
	ih.metrics.mutesDurationNotMuted.Observe(time.Since(start).Seconds())

	return false
}

// An InhibitRule specifies that a class of (source) alerts should inhibit
// notifications for another class of (target) alerts if all specified matching
// labels are equal between the two alerts. This may be used to inhibit alerts
// from sending notifications if their meaning is logically a subset of a
// higher-level alert.
type InhibitRule struct {
	// Name is an optional name for the inhibition rule.
	Name string
	// The set of Filters which define the group of source alerts (which inhibit
	// the target alerts).
	SourceMatchers labels.Matchers
	// The set of Filters which define the group of target alerts (which are