Skip to content

Commit 7569991

Browse files
committed
expose systemd's MemoryCurrent value in metrics
Signed-off-by: Evgeni Golov <[email protected]>
1 parent e362160 commit 7569991

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

Diff for: systemd/systemd.go

+34
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ type Collector struct {
6969
unitActiveExitTimeDesc *prometheus.Desc
7070
unitInactiveEnterTimeDesc *prometheus.Desc
7171
unitInactiveExitTimeDesc *prometheus.Desc
72+
unitMemoryCurrentDesc *prometheus.Desc
7273
nRestartsDesc *prometheus.Desc
7374
timerLastTriggerDesc *prometheus.Desc
7475
socketAcceptedConnectionsDesc *prometheus.Desc
@@ -142,6 +143,11 @@ func NewCollector(logger log.Logger) (*Collector, error) {
142143
"Last time the unit transitioned out of the inactive state",
143144
[]string{"name", "type"}, nil,
144145
)
146+
unitMemoryCurrentDesc := prometheus.NewDesc(
147+
prometheus.BuildFQName(namespace, "", "unit_memory_current"),
148+
"Current memory per Systemd unit",
149+
[]string{"name"}, nil,
150+
)
145151
nRestartsDesc := prometheus.NewDesc(
146152
prometheus.BuildFQName(namespace, "", "service_restart_total"),
147153
"Service unit count of Restart triggers", []string{"name"}, nil)
@@ -205,6 +211,7 @@ func NewCollector(logger log.Logger) (*Collector, error) {
205211
unitActiveExitTimeDesc: unitActiveExitTimeDesc,
206212
unitInactiveEnterTimeDesc: unitInactiveEnterTimeDesc,
207213
unitInactiveExitTimeDesc: unitInactiveExitTimeDesc,
214+
unitMemoryCurrentDesc: unitMemoryCurrentDesc,
208215
nRestartsDesc: nRestartsDesc,
209216
timerLastTriggerDesc: timerLastTriggerDesc,
210217
socketAcceptedConnectionsDesc: socketAcceptedConnectionsDesc,
@@ -235,6 +242,7 @@ func (c *Collector) Describe(desc chan<- *prometheus.Desc) {
235242
desc <- c.unitStartTimeDesc
236243
desc <- c.unitTasksCurrentDesc
237244
desc <- c.unitTasksMaxDesc
245+
desc <- c.unitMemoryCurrentDesc
238246
desc <- c.nRestartsDesc
239247
desc <- c.timerLastTriggerDesc
240248
desc <- c.socketAcceptedConnectionsDesc
@@ -325,6 +333,11 @@ func (c *Collector) collectUnit(conn *dbus.Conn, ch chan<- prometheus.Metric, un
325333
level.Warn(logger).Log("msg", errUnitMetricsMsg, "err", err)
326334
}
327335

336+
err = c.collectServiceMemoryMetrics(conn, ch, unit)
337+
if err != nil {
338+
level.Warn(logger).Log("msg", errUnitMetricsMsg, "err", err)
339+
}
340+
328341
err = c.collectUnitCPUUsageMetrics("Service", conn, ch, unit)
329342
if err != nil {
330343
level.Warn(logger).Log("msg", errUnitMetricsMsg, "err", err)
@@ -686,6 +699,27 @@ func (c *Collector) collectServiceTasksMetrics(conn *dbus.Conn, ch chan<- promet
686699
return nil
687700
}
688701

702+
func (c *Collector) collectServiceMemoryMetrics(conn *dbus.Conn, ch chan<- prometheus.Metric, unit dbus.UnitStatus) error {
703+
memoryCurrentCount, err := conn.GetUnitTypePropertyContext(c.ctx, unit.Name, "Service", "MemoryCurrent")
704+
if err != nil {
705+
return errors.Wrapf(err, errGetPropertyMsg, "MemoryCurrent")
706+
}
707+
708+
currentCount, ok := memoryCurrentCount.Value.Value().(uint64)
709+
if !ok {
710+
return errors.Errorf(errConvertUint64PropertyMsg, "MemoryCurrent", memoryCurrentCount.Value.Value())
711+
}
712+
713+
// Don't set if memoryCurrent if dbus reports MaxUint64.
714+
if currentCount != math.MaxUint64 {
715+
ch <- prometheus.MustNewConstMetric(
716+
c.unitMemoryCurrentDesc, prometheus.GaugeValue,
717+
float64(currentCount), unit.Name)
718+
}
719+
720+
return nil
721+
}
722+
689723
func (c *Collector) collectTimerTriggerTime(conn *dbus.Conn, ch chan<- prometheus.Metric, unit dbus.UnitStatus) error {
690724
lastTriggerValue, err := conn.GetUnitTypePropertyContext(c.ctx, unit.Name, "Timer", "LastTriggerUSec")
691725
if err != nil {

0 commit comments

Comments
 (0)