From 7c4bec01774e6b3fd8f1ee17fa953ba85b07e08d Mon Sep 17 00:00:00 2001 From: Paul Morton Date: Sat, 28 Mar 2015 07:18:56 -0700 Subject: [PATCH] Add a blocking get stats interface --- dockerclient.go | 27 +++++++++++++++++++++++++++ interface.go | 1 + types.go | 10 ++++++++-- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/dockerclient.go b/dockerclient.go index d427a6f..235f9d6 100644 --- a/dockerclient.go +++ b/dockerclient.go @@ -306,6 +306,33 @@ func (client *DockerClient) getStats(id string, cb StatCallback, ec chan error, } } +func (client *DockerClient) GetStats(id string, statsChan chan Stats, errorChan chan ContainerError, exitChan chan bool) { + uri := fmt.Sprintf("%s/%s/containers/%s/stats", client.URL.String(), APIVersion, id) + resp, err := client.HTTPClient.Get(uri) + if err != nil { + errorChan <- ContainerError{Error: err, ContainerId: id} + return + } + defer resp.Body.Close() + + dec := json.NewDecoder(resp.Body) + for { + select { + case <-exitChan: + return + default: + var stats *Stats + if err := dec.Decode(&stats); err != nil { + errorChan <- ContainerError{Error: err, ContainerId: id} + return + } + stats.ContainerId = id + statsChan <- *stats + } + + } +} + func (client *DockerClient) StopAllMonitorStats() { atomic.StoreInt32(&client.monitorStats, 0) } diff --git a/interface.go b/interface.go index 87b69f5..51eccc7 100644 --- a/interface.go +++ b/interface.go @@ -31,4 +31,5 @@ type Client interface { RemoveImage(name string) ([]*ImageDelete, error) PauseContainer(name string) error UnpauseContainer(name string) error + GetStats(id string, statsChan chan Stats, errorChan chan ContainerError, exitChan chan bool) } diff --git a/types.go b/types.go index 6bf197e..aee5c74 100644 --- a/types.go +++ b/types.go @@ -237,8 +237,9 @@ type BlkioStats struct { } type Stats struct { - Read time.Time `json:"read"` - Network struct { + ContainerId string + Read time.Time `json:"read"` + Network struct { RxBytes uint64 `json:"rx_bytes"` RxPackets uint64 `json:"rx_packets"` RxErrors uint64 `json:"rx_errors"` @@ -253,3 +254,8 @@ type Stats struct { MemoryStats MemoryStats `json:"memory_stats,omitempty"` BlkioStats BlkioStats `json:"blkio_stats,omitempty"` } + +type ContainerError struct { + Error error + ContainerId string +}