From 13921497a86459e97a83a979716b53976ed2e2b7 Mon Sep 17 00:00:00 2001 From: Jonathan Davies Date: Mon, 13 Nov 2023 22:58:54 +0000 Subject: [PATCH] systemd.go: Added collector health/duration metrics Fixes: #112 Signed-off-by: Jonathan Davies --- systemd/systemd.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/systemd/systemd.go b/systemd/systemd.go index 85a9df7..3cbb5a6 100644 --- a/systemd/systemd.go +++ b/systemd/systemd.go @@ -77,6 +77,8 @@ type Collector struct { ipEgressBytes *prometheus.Desc ipIngressPackets *prometheus.Desc ipEgressPackets *prometheus.Desc + scrapeDurationDesc *prometheus.Desc + scrapeSuccessDesc *prometheus.Desc unitIncludePattern *regexp.Regexp unitExcludePattern *regexp.Regexp @@ -186,6 +188,16 @@ func NewCollector(logger log.Logger) (*Collector, error) { "Service unit egress IP accounting in packets.", []string{"name"}, nil, ) + scrapeDurationDesc := prometheus.NewDesc( + prometheus.BuildFQName(namespace, "exporter", "collector_duration_seconds"), + "systemd_exporter: Duration of a systemd collector scrape.", + []string{"collector"}, nil, + ) + scrapeSuccessDesc := prometheus.NewDesc( + prometheus.BuildFQName(namespace, "exporter", "collector_success"), + "systemd_exporter: Whether the systemd collector succeeded.", + []string{"collector"}, nil, + ) unitIncludePattern := regexp.MustCompile(fmt.Sprintf("^(?:%s)$", *unitInclude)) unitExcludePattern := regexp.MustCompile(fmt.Sprintf("^(?:%s)$", *unitExclude)) @@ -213,6 +225,8 @@ func NewCollector(logger log.Logger) (*Collector, error) { ipEgressBytes: ipEgressBytes, ipIngressPackets: ipIngressPackets, ipEgressPackets: ipEgressPackets, + scrapeSuccessDesc: scrapeSuccessDesc, + scrapeDurationDesc: scrapeDurationDesc, unitIncludePattern: unitIncludePattern, unitExcludePattern: unitExcludePattern, }, nil @@ -220,10 +234,19 @@ func NewCollector(logger log.Logger) (*Collector, error) { // Collect gathers metrics from systemd. func (c *Collector) Collect(ch chan<- prometheus.Metric) { + begin := time.Now() + err := c.collect(ch) if err != nil { level.Error(c.logger).Log("msg", "error collecting metrics", "err", err) + ch <- prometheus.MustNewConstMetric( + c.scrapeSuccessDesc, prometheus.GaugeValue, 0, namespace) + } else { + ch <- prometheus.MustNewConstMetric( + c.scrapeSuccessDesc, prometheus.GaugeValue, 1, namespace) } + + ch <- prometheus.MustNewConstMetric(c.scrapeDurationDesc, prometheus.GaugeValue, time.Since(begin).Seconds(), namespace) } // Describe gathers descriptions of Metrics @@ -243,6 +266,8 @@ func (c *Collector) Describe(desc chan<- *prometheus.Desc) { desc <- c.ipEgressBytes desc <- c.ipIngressPackets desc <- c.ipEgressPackets + desc <- c.scrapeDurationDesc + desc <- c.scrapeSuccessDesc }