Skip to content

Commit 1392149

Browse files
committed
systemd.go: Added collector health/duration metrics
Fixes: #112 Signed-off-by: Jonathan Davies <[email protected]>
1 parent 25c6295 commit 1392149

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

Diff for: systemd/systemd.go

+25
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ type Collector struct {
7777
ipEgressBytes *prometheus.Desc
7878
ipIngressPackets *prometheus.Desc
7979
ipEgressPackets *prometheus.Desc
80+
scrapeDurationDesc *prometheus.Desc
81+
scrapeSuccessDesc *prometheus.Desc
8082

8183
unitIncludePattern *regexp.Regexp
8284
unitExcludePattern *regexp.Regexp
@@ -186,6 +188,16 @@ func NewCollector(logger log.Logger) (*Collector, error) {
186188
"Service unit egress IP accounting in packets.",
187189
[]string{"name"}, nil,
188190
)
191+
scrapeDurationDesc := prometheus.NewDesc(
192+
prometheus.BuildFQName(namespace, "exporter", "collector_duration_seconds"),
193+
"systemd_exporter: Duration of a systemd collector scrape.",
194+
[]string{"collector"}, nil,
195+
)
196+
scrapeSuccessDesc := prometheus.NewDesc(
197+
prometheus.BuildFQName(namespace, "exporter", "collector_success"),
198+
"systemd_exporter: Whether the systemd collector succeeded.",
199+
[]string{"collector"}, nil,
200+
)
189201
unitIncludePattern := regexp.MustCompile(fmt.Sprintf("^(?:%s)$", *unitInclude))
190202
unitExcludePattern := regexp.MustCompile(fmt.Sprintf("^(?:%s)$", *unitExclude))
191203

@@ -213,17 +225,28 @@ func NewCollector(logger log.Logger) (*Collector, error) {
213225
ipEgressBytes: ipEgressBytes,
214226
ipIngressPackets: ipIngressPackets,
215227
ipEgressPackets: ipEgressPackets,
228+
scrapeSuccessDesc: scrapeSuccessDesc,
229+
scrapeDurationDesc: scrapeDurationDesc,
216230
unitIncludePattern: unitIncludePattern,
217231
unitExcludePattern: unitExcludePattern,
218232
}, nil
219233
}
220234

221235
// Collect gathers metrics from systemd.
222236
func (c *Collector) Collect(ch chan<- prometheus.Metric) {
237+
begin := time.Now()
238+
223239
err := c.collect(ch)
224240
if err != nil {
225241
level.Error(c.logger).Log("msg", "error collecting metrics", "err", err)
242+
ch <- prometheus.MustNewConstMetric(
243+
c.scrapeSuccessDesc, prometheus.GaugeValue, 0, namespace)
244+
} else {
245+
ch <- prometheus.MustNewConstMetric(
246+
c.scrapeSuccessDesc, prometheus.GaugeValue, 1, namespace)
226247
}
248+
249+
ch <- prometheus.MustNewConstMetric(c.scrapeDurationDesc, prometheus.GaugeValue, time.Since(begin).Seconds(), namespace)
227250
}
228251

229252
// Describe gathers descriptions of Metrics
@@ -243,6 +266,8 @@ func (c *Collector) Describe(desc chan<- *prometheus.Desc) {
243266
desc <- c.ipEgressBytes
244267
desc <- c.ipIngressPackets
245268
desc <- c.ipEgressPackets
269+
desc <- c.scrapeDurationDesc
270+
desc <- c.scrapeSuccessDesc
246271

247272
}
248273

0 commit comments

Comments
 (0)