diff --git a/zfs/zfscmd/zfscmd.go b/zfs/zfscmd/zfscmd.go index b244bb9..8bce629 100644 --- a/zfs/zfscmd/zfscmd.go +++ b/zfs/zfscmd/zfscmd.go @@ -134,6 +134,10 @@ func (c *Cmd) waitPre() { waitPreLogging(c, now) } +type usage struct { + total_secs, system_secs, user_secs float64 +} + func (c *Cmd) waitPost(err error) { now := time.Now() @@ -146,9 +150,34 @@ func (c *Cmd) waitPost(err error) { c.waitReturnedAt = now c.mtx.Unlock() - waitPostReport(c, now) - waitPostLogging(c, err, now) - waitPostPrometheus(c, err, now) + // build usage + var u usage + { + var s *os.ProcessState + if err == nil { + s = c.cmd.ProcessState + } else if ee, ok := err.(*exec.ExitError); ok { + s = ee.ProcessState + } + + if s == nil { + u = usage{ + total_secs: c.Runtime().Seconds(), + system_secs: -1, + user_secs: -1, + } + } else { + u = usage{ + total_secs: c.Runtime().Seconds(), + system_secs: s.SystemTime().Seconds(), + user_secs: s.UserTime().Seconds(), + } + } + } + + waitPostReport(c, u, now) + waitPostLogging(c, u, err, now) + waitPostPrometheus(c, u, err, now) } // returns 0 if the command did not yet finish diff --git a/zfs/zfscmd/zfscmd_logging.go b/zfs/zfscmd/zfscmd_logging.go index ac637e4..0c65a05 100644 --- a/zfs/zfscmd/zfscmd_logging.go +++ b/zfs/zfscmd/zfscmd_logging.go @@ -1,7 +1,6 @@ package zfscmd import ( - "os/exec" "time" ) @@ -28,23 +27,12 @@ func waitPreLogging(c *Cmd, now time.Time) { c.log().Debug("start waiting") } -func waitPostLogging(c *Cmd, err error, now time.Time) { - - var total, system, user float64 - - total = c.Runtime().Seconds() - if ee, ok := err.(*exec.ExitError); ok { - system = ee.ProcessState.SystemTime().Seconds() - user = ee.ProcessState.UserTime().Seconds() - } else { - system = -1 - user = -1 - } +func waitPostLogging(c *Cmd, u usage, err error, now time.Time) { log := c.log(). - WithField("total_time_s", total). - WithField("systemtime_s", system). - WithField("usertime_s", user) + WithField("total_time_s", u.total_secs). + WithField("systemtime_s", u.system_secs). + WithField("usertime_s", u.user_secs) if err == nil { log.Info("command exited without error") diff --git a/zfs/zfscmd/zfscmd_prometheus.go b/zfs/zfscmd/zfscmd_prometheus.go index f68e998..5513b2e 100644 --- a/zfs/zfscmd/zfscmd_prometheus.go +++ b/zfs/zfscmd/zfscmd_prometheus.go @@ -46,7 +46,7 @@ func RegisterMetrics(r prometheus.Registerer) { r.MustRegister(metrics.usertime) } -func waitPostPrometheus(c *Cmd, err error, now time.Time) { +func waitPostPrometheus(c *Cmd, u usage, err error, now time.Time) { if len(c.cmd.Args) < 2 { getLogger(c.ctx).WithField("args", c.cmd.Args). @@ -64,10 +64,10 @@ func waitPostPrometheus(c *Cmd, err error, now time.Time) { metrics.totaltime. WithLabelValues(labelValues...). - Observe(c.Runtime().Seconds()) + Observe(u.total_secs) metrics.systemtime.WithLabelValues(labelValues...). - Observe(c.cmd.ProcessState.SystemTime().Seconds()) + Observe(u.system_secs) metrics.usertime.WithLabelValues(labelValues...). - Observe(c.cmd.ProcessState.UserTime().Seconds()) + Observe(u.user_secs) } diff --git a/zfs/zfscmd/zfscmd_report.go b/zfs/zfscmd/zfscmd_report.go index abd7b4f..1b4a301 100644 --- a/zfs/zfscmd/zfscmd_report.go +++ b/zfs/zfscmd/zfscmd_report.go @@ -57,7 +57,7 @@ func startPostReport(c *Cmd, err error, now time.Time) { active.mtx.Unlock() } -func waitPostReport(c *Cmd, now time.Time) { +func waitPostReport(c *Cmd, _ usage, now time.Time) { active.mtx.Lock() defer active.mtx.Unlock() prev := active.cmds[c]