Skip to content

Commit

Permalink
systemd.go: Added collector health/duration metrics
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 17, 2023
1 parent 25c6295 commit 1392149
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions systemd/systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -213,17 +225,28 @@ func NewCollector(logger log.Logger) (*Collector, error) {
ipEgressBytes: ipEgressBytes,
ipIngressPackets: ipIngressPackets,
ipEgressPackets: ipEgressPackets,
scrapeSuccessDesc: scrapeSuccessDesc,
scrapeDurationDesc: scrapeDurationDesc,
unitIncludePattern: unitIncludePattern,
unitExcludePattern: unitExcludePattern,
}, nil
}

// 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
Expand All @@ -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

}

Expand Down

0 comments on commit 1392149

Please sign in to comment.