Skip to content

Commit

Permalink
expose systemd's MemoryCurrent value in metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeni committed May 30, 2023
1 parent e362160 commit 08670da
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions systemd/systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ type Collector struct {
unitActiveExitTimeDesc *prometheus.Desc
unitInactiveEnterTimeDesc *prometheus.Desc
unitInactiveExitTimeDesc *prometheus.Desc
unitMemoryCurrentDesc *prometheus.Desc
nRestartsDesc *prometheus.Desc
timerLastTriggerDesc *prometheus.Desc
socketAcceptedConnectionsDesc *prometheus.Desc
Expand Down Expand Up @@ -142,6 +143,11 @@ func NewCollector(logger log.Logger) (*Collector, error) {
"Last time the unit transitioned out of the inactive state",
[]string{"name", "type"}, nil,
)
unitMemoryCurrentDesc := prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "unit_memory_current"),
"Current memory per Systemd unit",
[]string{"name"}, nil,
)
nRestartsDesc := prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "service_restart_total"),
"Service unit count of Restart triggers", []string{"name"}, nil)
Expand Down Expand Up @@ -205,6 +211,7 @@ func NewCollector(logger log.Logger) (*Collector, error) {
unitActiveExitTimeDesc: unitActiveExitTimeDesc,
unitInactiveEnterTimeDesc: unitInactiveEnterTimeDesc,
unitInactiveExitTimeDesc: unitInactiveExitTimeDesc,
unitMemoryCurrentDesc: unitMemoryCurrentDesc,
nRestartsDesc: nRestartsDesc,
timerLastTriggerDesc: timerLastTriggerDesc,
socketAcceptedConnectionsDesc: socketAcceptedConnectionsDesc,
Expand Down Expand Up @@ -235,6 +242,7 @@ func (c *Collector) Describe(desc chan<- *prometheus.Desc) {
desc <- c.unitStartTimeDesc
desc <- c.unitTasksCurrentDesc
desc <- c.unitTasksMaxDesc
desc <- c.unitMemoryCurrentDesc
desc <- c.nRestartsDesc
desc <- c.timerLastTriggerDesc
desc <- c.socketAcceptedConnectionsDesc
Expand Down Expand Up @@ -325,6 +333,11 @@ func (c *Collector) collectUnit(conn *dbus.Conn, ch chan<- prometheus.Metric, un
level.Warn(logger).Log("msg", errUnitMetricsMsg, "err", err)
}

err = c.collectServiceMemoryMetrics(conn, ch, unit)
if err != nil {
level.Warn(logger).Log("msg", errUnitMetricsMsg, "err", err)
}

err = c.collectUnitCPUUsageMetrics("Service", conn, ch, unit)
if err != nil {
level.Warn(logger).Log("msg", errUnitMetricsMsg, "err", err)
Expand Down Expand Up @@ -686,6 +699,27 @@ func (c *Collector) collectServiceTasksMetrics(conn *dbus.Conn, ch chan<- promet
return nil
}

func (c *Collector) collectServiceMemoryMetrics(conn *dbus.Conn, ch chan<- prometheus.Metric, unit dbus.UnitStatus) error {
memoryCurrentCount, err := conn.GetUnitTypePropertyContext(c.ctx, unit.Name, "Service", "MemoryCurrent")
if err != nil {
return errors.Wrapf(err, errGetPropertyMsg, "MemoryCurrent")
}

currentCount, ok := memoryCurrentCount.Value.Value().(uint64)
if !ok {
return errors.Errorf(errConvertUint64PropertyMsg, "MemoryCurrent", memoryCurrentCount.Value.Value())
}

// Don't set if memoryCurrent if dbus reports MaxUint64.
if currentCount != math.MaxUint64 {
ch <- prometheus.MustNewConstMetric(
c.unitMemoryCurrentDesc, prometheus.GaugeValue,
float64(currentCount), unit.Name)
}

return nil
}

func (c *Collector) collectTimerTriggerTime(conn *dbus.Conn, ch chan<- prometheus.Metric, unit dbus.UnitStatus) error {
lastTriggerValue, err := conn.GetUnitTypePropertyContext(c.ctx, unit.Name, "Timer", "LastTriggerUSec")
if err != nil {
Expand Down

0 comments on commit 08670da

Please sign in to comment.