Skip to content

Commit

Permalink
Show uptime durations in a more human format.
Browse files Browse the repository at this point in the history
  • Loading branch information
fbarl committed Nov 1, 2017
1 parent 99fad2f commit cc52c98
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 10 deletions.
9 changes: 8 additions & 1 deletion client/app/scripts/utils/string-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions probe/docker/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
5 changes: 3 additions & 2 deletions probe/docker/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package docker_test

import (
"net"
"strconv"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -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},
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion probe/docker/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
6 changes: 4 additions & 2 deletions probe/host/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package host
import (
"fmt"
"runtime"
"strconv"
"sync"
"time"

Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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().
Expand Down
4 changes: 2 additions & 2 deletions probe/host/reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
Expand All @@ -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 }
Expand Down

0 comments on commit cc52c98

Please sign in to comment.