Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

systemd.go: Added systemd health metric #113

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't make sense to me, as it's not actually exposing metrics per collector that I can tell. namespace is always systemd, so the label would always be collector="systmed".

This means it's no different than up and scrape_duration_seconds recorded by Prometheus.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We didn't have collectors up until this point - will fix that in a bit.

}

// 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