Skip to content

Commit

Permalink
Merge pull request #853 from weaveworks/844-container-uptime
Browse files Browse the repository at this point in the history
Add container uptime and restart count to details panel.
  • Loading branch information
paulbellamy committed Feb 1, 2016
2 parents 394bc88 + 638c567 commit 7601a34
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 6 deletions.
11 changes: 9 additions & 2 deletions probe/docker/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/http"
"net/http/httputil"
"net/url"
"strconv"
"strings"
"sync"
"time"
Expand All @@ -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"
Expand Down Expand Up @@ -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.
Expand All @@ -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,
)
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 @@ -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",
Expand All @@ -79,15 +80,15 @@ 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")).
Add("docker_container_ips_with_scopes", report.MakeStringSet("scope;1.2.3.4")),
).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),
Expand Down
7 changes: 6 additions & 1 deletion probe/docker/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
2 changes: 2 additions & 0 deletions render/detailed/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
2 changes: 1 addition & 1 deletion report/latest_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 7601a34

Please sign in to comment.