From b97f4cf48c27cc085218b999a663fc8127276ff5 Mon Sep 17 00:00:00 2001 From: Brian Brazil Date: Thu, 30 Nov 2017 14:36:34 +0000 Subject: [PATCH] Add metrics for rule group interval and last duration. --- cmd/prometheus/main.go | 1 + rules/manager.go | 41 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/cmd/prometheus/main.go b/cmd/prometheus/main.go index ab3a862e2..e278f5a4b 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 16fb48104..c9d907032 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)) + } +}