From ca952f490dbd34d1275d2e102ae4614fd8cd63ef Mon Sep 17 00:00:00 2001 From: Jonathan Davies Date: Mon, 13 Nov 2023 22:58:54 +0000 Subject: [PATCH] systemd.go: Added systemd health metric Fixes: #112 Signed-off-by: Jonathan Davies --- systemd/systemd.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/systemd/systemd.go b/systemd/systemd.go index 85a9df7..426bb97 100644 --- a/systemd/systemd.go +++ b/systemd/systemd.go @@ -58,6 +58,7 @@ var ( type Collector struct { ctx context.Context logger log.Logger + healthy *prometheus.Desc unitCPUTotal *prometheus.Desc unitState *prometheus.Desc unitInfo *prometheus.Desc @@ -84,6 +85,10 @@ type Collector struct { // NewCollector returns a new Collector exposing systemd statistics. func NewCollector(logger log.Logger) (*Collector, error) { + healthy := prometheus.NewDesc( + prometheus.BuildFQName(namespace, "", "healthy"), + "Systemd exporter DBus connection health", nil, nil, + ) // Type is labeled twice e.g. name="foo.service" and type="service" to maintain compatibility // with users before we started exporting type label unitState := prometheus.NewDesc( @@ -194,6 +199,7 @@ func NewCollector(logger log.Logger) (*Collector, error) { return &Collector{ ctx: ctx, logger: logger, + healthy: healthy, unitCPUTotal: unitCPUTotal, unitState: unitState, unitInfo: unitInfo, @@ -223,11 +229,14 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) { err := c.collect(ch) if err != nil { level.Error(c.logger).Log("msg", "error collecting metrics", "err", err) + ch <- prometheus.MustNewConstMetric( + c.healthy, prometheus.GaugeValue, 0) } } // Describe gathers descriptions of Metrics func (c *Collector) Describe(desc chan<- *prometheus.Desc) { + desc <- c.healthy desc <- c.unitCPUTotal desc <- c.unitState desc <- c.unitInfo @@ -282,6 +291,10 @@ func (c *Collector) collect(ch chan<- prometheus.Metric) error { } wg.Wait() + + ch <- prometheus.MustNewConstMetric( + c.healthy, prometheus.GaugeValue, 1) + return nil }