From cc52c988bf7a59b88088c2ffc4863841fefb5a28 Mon Sep 17 00:00:00 2001 From: Filip Barl Date: Mon, 30 Oct 2017 17:10:24 +0100 Subject: [PATCH] Show uptime durations in a more human format. --- client/app/scripts/utils/string-utils.js | 9 ++++++++- probe/docker/container.go | 6 ++++-- probe/docker/container_test.go | 5 +++-- probe/docker/reporter.go | 2 +- probe/host/reporter.go | 6 ++++-- probe/host/reporter_test.go | 4 ++-- 6 files changed, 22 insertions(+), 10 deletions(-) diff --git a/client/app/scripts/utils/string-utils.js b/client/app/scripts/utils/string-utils.js index e22218996f..bd69709367 100644 --- a/client/app/scripts/utils/string-utils.js +++ b/client/app/scripts/utils/string-utils.js @@ -99,7 +99,14 @@ export function formatDataType(field, referenceTimestampStr = null) { value: timestamp.from(referenceTimestamp), title: timestamp.utc().toISOString() }; - } + }, + duration(durationString) { + const humanizedDuration = moment.duration(parseInt(durationString, 10)).humanize(); + return { + value: humanizedDuration, + title: humanizedDuration, + }; + }, }; const format = formatters[field.dataType]; return format diff --git a/probe/docker/container.go b/probe/docker/container.go index bc10f6bfd4..949a09bbdd 100644 --- a/probe/docker/container.go +++ b/probe/docker/container.go @@ -425,12 +425,14 @@ func (c *container) GetNode() report.Node { controls := c.controlsMap() if !c.container.State.Paused && c.container.State.Running { - uptime := (mtime.Now().Sub(c.container.State.StartedAt) / time.Second) * time.Second + // TODO: Use mtime.Now() only as a fallback from Time Travel + // timestamp here to get accurate durations when time travelling. + uptimeMs := mtime.Now().Sub(c.container.State.StartedAt) / time.Millisecond networkMode := "" if c.container.HostConfig != nil { networkMode = c.container.HostConfig.NetworkMode } - latest[ContainerUptime] = uptime.String() + latest[ContainerUptime] = strconv.Itoa(int(uptimeMs)) latest[ContainerRestartCount] = strconv.Itoa(c.container.RestartCount) latest[ContainerNetworkMode] = networkMode } diff --git a/probe/docker/container_test.go b/probe/docker/container_test.go index 95b502c904..3e2298218e 100644 --- a/probe/docker/container_test.go +++ b/probe/docker/container_test.go @@ -2,6 +2,7 @@ package docker_test import ( "net" + "strconv" "strings" "testing" "time" @@ -58,7 +59,7 @@ func TestContainer(t *testing.T) { // Now see if we go them { - uptime := (now.Sub(startTime) / time.Second) * time.Second + uptimeMs := int(now.Sub(startTime) / time.Millisecond) controls := map[string]report.NodeControlData{ docker.UnpauseContainer: {Dead: true}, docker.RestartContainer: {Dead: false}, @@ -79,7 +80,7 @@ func TestContainer(t *testing.T) { "docker_label_foo2": "bar2", "docker_container_state": "running", "docker_container_state_human": c.Container().State.String(), - "docker_container_uptime": uptime.String(), + "docker_container_uptime": strconv.Itoa(uptimeMs), "docker_env_FOO": "secret-bar", }).WithLatestControls( controls, diff --git a/probe/docker/reporter.go b/probe/docker/reporter.go index a20aa7381f..02068f9c74 100644 --- a/probe/docker/reporter.go +++ b/probe/docker/reporter.go @@ -32,7 +32,7 @@ var ( ImageName: {ID: ImageName, Label: "Image", From: report.FromLatest, Priority: 1}, ContainerCommand: {ID: ContainerCommand, Label: "Command", From: report.FromLatest, Priority: 2}, ContainerStateHuman: {ID: ContainerStateHuman, Label: "State", From: report.FromLatest, Priority: 3}, - ContainerUptime: {ID: ContainerUptime, Label: "Uptime", From: report.FromLatest, Priority: 4}, + ContainerUptime: {ID: ContainerUptime, Label: "Uptime", From: report.FromLatest, Priority: 4, Datatype: "duration"}, ContainerRestartCount: {ID: ContainerRestartCount, Label: "Restart #", From: report.FromLatest, Priority: 5}, ContainerNetworks: {ID: ContainerNetworks, Label: "Networks", From: report.FromSets, Priority: 6}, ContainerIPs: {ID: ContainerIPs, Label: "IPs", From: report.FromSets, Priority: 7}, diff --git a/probe/host/reporter.go b/probe/host/reporter.go index 7c4d44de1a..a07a12e765 100644 --- a/probe/host/reporter.go +++ b/probe/host/reporter.go @@ -3,6 +3,7 @@ package host import ( "fmt" "runtime" + "strconv" "sync" "time" @@ -37,7 +38,7 @@ const ( var ( MetadataTemplates = report.MetadataTemplates{ KernelVersion: {ID: KernelVersion, Label: "Kernel Version", From: report.FromLatest, Priority: 1}, - Uptime: {ID: Uptime, Label: "Uptime", From: report.FromLatest, Priority: 2}, + Uptime: {ID: Uptime, Label: "Uptime", From: report.FromLatest, Priority: 2, Datatype: "duration"}, HostName: {ID: HostName, Label: "Hostname", From: report.FromLatest, Priority: 11}, OS: {ID: OS, Label: "OS", From: report.FromLatest, Priority: 12}, LocalNetworks: {ID: LocalNetworks, Label: "Local Networks", From: report.FromSets, Priority: 13}, @@ -102,6 +103,7 @@ func (r *Reporter) Report() (report.Report, error) { localCIDRs = append(localCIDRs, localNet.String()) } + // TODO: Make sure uptime is accurate also when time travelling. uptime, err := GetUptime() if err != nil { return rep, err @@ -130,7 +132,7 @@ func (r *Reporter) Report() (report.Report, error) { HostName: r.hostName, OS: runtime.GOOS, KernelVersion: kernel, - Uptime: uptime.String(), + Uptime: strconv.Itoa(int(uptime / time.Millisecond)), ScopeVersion: r.version, }). WithSets(report.MakeSets(). diff --git a/probe/host/reporter_test.go b/probe/host/reporter_test.go index 791ded5817..568ed19244 100644 --- a/probe/host/reporter_test.go +++ b/probe/host/reporter_test.go @@ -25,7 +25,7 @@ func TestReporter(t *testing.T) { host.CPUUsage: report.MakeSingletonMetric(timestamp, 30.0).WithMax(100.0), host.MemoryUsage: report.MakeSingletonMetric(timestamp, 60.0).WithMax(100.0), } - uptime = "278h55m43s" + uptime = "3600000" // one hour kernel = "release version" _, ipnet, _ = net.ParseCIDR(network) ) @@ -51,7 +51,7 @@ func TestReporter(t *testing.T) { }() host.GetKernelReleaseAndVersion = func() (string, string, error) { return release, version, nil } host.GetLoad = func(time.Time) report.Metrics { return metrics } - host.GetUptime = func() (time.Duration, error) { return time.ParseDuration(uptime) } + host.GetUptime = func() (time.Duration, error) { return time.Hour, nil } host.GetCPUUsagePercent = func() (float64, float64) { return 30.0, 100.0 } host.GetMemoryUsageBytes = func() (float64, float64) { return 60.0, 100.0 } host.GetLocalNetworks = func() ([]*net.IPNet, error) { return []*net.IPNet{ipnet}, nil }