Skip to content

Commit

Permalink
systemd.go: Added systemd health metric
Browse files Browse the repository at this point in the history
Fixes: prometheus-community#112

Signed-off-by: Jonathan Davies <[email protected]>
  • Loading branch information
jpds committed Nov 13, 2023
1 parent 25c6295 commit ca952f4
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions systemd/systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit ca952f4

Please sign in to comment.