From 638c5676cb5b60245d2bca0fe508d0dbeaafe874 Mon Sep 17 00:00:00 2001 From: Tom Wilkie Date: Sat, 23 Jan 2016 16:43:17 -0800 Subject: [PATCH] Add container uptime and restart count to details panel. --- probe/docker/container.go | 11 +++++++++-- probe/docker/container_test.go | 5 +++-- probe/docker/registry_test.go | 7 ++++++- render/detailed/metadata.go | 2 ++ report/latest_map.go | 2 +- 5 files changed, 21 insertions(+), 6 deletions(-) diff --git a/probe/docker/container.go b/probe/docker/container.go index 8e8707f46..050fbb9a0 100644 --- a/probe/docker/container.go +++ b/probe/docker/container.go @@ -10,6 +10,7 @@ import ( "net/http" "net/http/httputil" "net/url" + "strconv" "strings" "sync" "time" @@ -30,6 +31,8 @@ const ( ContainerHostname = "docker_container_hostname" ContainerIPsWithScopes = "docker_container_ips_with_scopes" ContainerState = "docker_container_state" + ContainerUptime = "docker_container_uptime" + ContainerRestartCount = "docker_container_restart_count" NetworkRxDropped = "network_rx_dropped" NetworkRxBytes = "network_rx_bytes" @@ -331,12 +334,11 @@ func (c *container) GetNode(hostID string, localAddrs []net.IP) report.Node { ContainerCommand: c.container.Path + " " + strings.Join(c.container.Args, " "), ImageID: c.container.Image, ContainerHostname: c.Hostname(), + ContainerState: state, }).WithSets(report.EmptySets. Add(ContainerPorts, c.ports(localAddrs)). Add(ContainerIPs, report.MakeStringSet(ips...)). Add(ContainerIPsWithScopes, report.MakeStringSet(ipsWithScopes...)), - ).WithLatest( - ContainerState, mtime.Now(), state, ).WithMetrics( c.metrics(), ).WithParents(report.EmptySets. @@ -346,6 +348,11 @@ func (c *container) GetNode(hostID string, localAddrs []net.IP) report.Node { if c.container.State.Paused { result = result.WithControls(UnpauseContainer) } else if c.container.State.Running { + uptime := (mtime.Now().Sub(c.container.State.StartedAt) / time.Second) * time.Second + result = result.WithLatests(map[string]string{ + ContainerUptime: uptime.String(), + ContainerRestartCount: strconv.Itoa(c.container.RestartCount), + }) result = result.WithControls( RestartContainer, StopContainer, PauseContainer, AttachContainer, ExecContainer, ) diff --git a/probe/docker/container_test.go b/probe/docker/container_test.go index 610456968..d4e79d37f 100644 --- a/probe/docker/container_test.go +++ b/probe/docker/container_test.go @@ -71,6 +71,7 @@ func TestContainer(t *testing.T) { } // Now see if we go them + uptime := (now.Sub(startTime) / time.Second) * time.Second want := report.MakeNode().WithLatests(map[string]string{ "docker_container_command": " ", "docker_container_created": "01 Jan 01 00:00 UTC", @@ -79,6 +80,8 @@ func TestContainer(t *testing.T) { "docker_image_id": "baz", "docker_label_foo1": "bar1", "docker_label_foo2": "bar2", + "docker_container_state": "running", + "docker_container_uptime": uptime.String(), }).WithSets(report.EmptySets. Add("docker_container_ports", report.MakeStringSet("1.2.3.4:80->80/tcp", "81/tcp")). Add("docker_container_ips", report.MakeStringSet("1.2.3.4")). @@ -86,8 +89,6 @@ func TestContainer(t *testing.T) { ).WithControls( docker.RestartContainer, docker.StopContainer, docker.PauseContainer, docker.AttachContainer, docker.ExecContainer, - ).WithLatest( - "docker_container_state", now, "running", ).WithMetrics(report.Metrics{ "docker_cpu_total_usage": report.MakeMetric(), "docker_memory_usage": report.MakeMetric().Add(now, 12345), diff --git a/probe/docker/registry_test.go b/probe/docker/registry_test.go index cc9c40f51..5dcdc462b 100644 --- a/probe/docker/registry_test.go +++ b/probe/docker/registry_test.go @@ -158,11 +158,16 @@ func (m *mockDockerClient) send(event *client.APIEvents) { } var ( + startTime = time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC) container1 = &client.Container{ ID: "ping", Name: "pong", Image: "baz", - State: client.State{Pid: 2, Running: true}, + State: client.State{ + Pid: 2, + Running: true, + StartedAt: startTime, + }, NetworkSettings: &client.NetworkSettings{ IPAddress: "1.2.3.4", Ports: map[client.Port][]client.PortBinding{ diff --git a/render/detailed/metadata.go b/render/detailed/metadata.go index c67b1b618..b225f9e74 100644 --- a/render/detailed/metadata.go +++ b/render/detailed/metadata.go @@ -24,6 +24,8 @@ var ( ltst(docker.ContainerID, "ID"), ltst(docker.ImageID, "Image ID"), ltst(docker.ContainerState, "State"), + ltst(docker.ContainerUptime, "Uptime"), + ltst(docker.ContainerRestartCount, "Restart #"), sets(docker.ContainerIPs, "IPs"), sets(docker.ContainerPorts, "Ports"), ltst(docker.ContainerCreated, "Created"), diff --git a/report/latest_map.go b/report/latest_map.go index 9f5619c28..453d4ac9e 100644 --- a/report/latest_map.go +++ b/report/latest_map.go @@ -135,7 +135,7 @@ func (m LatestMap) String() string { buf := bytes.NewBufferString("{") for _, key := range keys { val, _ := m.Map.Lookup(key) - fmt.Fprintf(buf, "%s: %s, ", key, val) + fmt.Fprintf(buf, "%s: %s,\n", key, val) } fmt.Fprintf(buf, "}\n") return buf.String()