diff --git a/cmd/prometheus/main.go b/cmd/prometheus/main.go index ab3a862e2d..e278f5a4bf 100644 --- a/cmd/prometheus/main.go +++ b/cmd/prometheus/main.go @@ -241,6 +241,7 @@ func main() { NotifyFunc: sendAlerts(notifier, cfg.web.ExternalURL.String()), Context: ctx, ExternalURL: cfg.web.ExternalURL, + Registerer: prometheus.DefaultRegisterer, Logger: log.With(logger, "component", "rule manager"), }) diff --git a/rules/manager.go b/rules/manager.go index 16fb48104d..c9d9070325 100644 --- a/rules/manager.go +++ b/rules/manager.go @@ -81,6 +81,18 @@ var ( Name: "rule_group_iterations_total", Help: "The total number of scheduled rule group evaluations, whether executed or missed.", }) + lastDuration = prometheus.NewDesc( + prometheus.BuildFQName(namespace, "", "rule_group_last_duration_seconds"), + "The duration of the last rule group evaulation.", + []string{"rule_group"}, + nil, + ) + groupInterval = prometheus.NewDesc( + prometheus.BuildFQName(namespace, "", "rule_group_interval_seconds"), + "The interval of a rule group.", + []string{"rule_group"}, + nil, + ) ) func init() { @@ -444,17 +456,22 @@ type ManagerOptions struct { Context context.Context Appendable Appendable Logger log.Logger + Registerer prometheus.Registerer } // NewManager returns an implementation of Manager, ready to be started // by calling the Run method. func NewManager(o *ManagerOptions) *Manager { - return &Manager{ + m := &Manager{ groups: map[string]*Group{}, opts: o, block: make(chan struct{}), logger: o.Logger, } + if o.Registerer != nil { + o.Registerer.MustRegister(m) + } + return m } // Run starts processing of the rule manager. @@ -627,3 +644,25 @@ func (m *Manager) AlertingRules() []*AlertingRule { } return alerts } + +// Implements prometheus.Collector. +func (m *Manager) Describe(ch chan<- *prometheus.Desc) { + ch <- lastDuration + ch <- groupInterval +} + +// Implements prometheus.Collector. +func (m *Manager) Collect(ch chan<- prometheus.Metric) { + for _, g := range m.RuleGroups() { + ch <- prometheus.MustNewConstMetric(lastDuration, + prometheus.GaugeValue, + g.GetEvaluationTime().Seconds(), + groupKey(g.file, g.name)) + } + for _, g := range m.RuleGroups() { + ch <- prometheus.MustNewConstMetric(groupInterval, + prometheus.GaugeValue, + g.interval.Seconds(), + groupKey(g.file, g.name)) + } +}