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

Code before:
	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
	// inhibited by the source alerts).
	TargetMatchers labels.Matchers
	// A set of label names whose label values need to be identical in source and
	// target alerts in order for the inhibition to take effect.
	Equal map[model.LabelName]struct{}

	// Cache of alerts matching source labels.
	scache *store.Alerts


Review comment (performance):
There is also a Prometheus Timer which could be used with `defer` for example:
```golang
defer prometheus.NewTimer(ih.metrics.mutesDurationNotMuted).ObserveDuration()
```
Not sure if it would be more or less efficient than plain observe.

Code after:
	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
	// inhibited by the source alerts).
	TargetMatchers labels.Matchers
	// A set of label names whose label values need to be identical in source and
	// target alerts in order for the inhibition to take effect.
	Equal map[model.LabelName]struct{}

	// Cache of alerts matching source labels.
	scache *store.Alerts