Skip to content
This repository has been archived by the owner on Mar 18, 2024. It is now read-only.

Add a blocking get stats interface #84

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions dockerclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with the errorChan when the stats stream is open, but should we use an error code return for opening the stream (and check this http req)?

I am not sure about this one (what's the easiest for the dev), it's an honest question. Also, tests?

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)
}
Expand Down
1 change: 1 addition & 0 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
10 changes: 8 additions & 2 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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
}