2018-02-05 21:05:59 +00:00
|
|
|
package client // import "github.com/docker/docker/client"
|
2016-09-06 18:46:37 +00:00
|
|
|
|
|
|
|
import (
|
2018-04-19 22:30:59 +00:00
|
|
|
"context"
|
2016-09-06 18:46:37 +00:00
|
|
|
"net/url"
|
|
|
|
|
2016-09-07 23:08:51 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
2016-09-06 18:46:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ContainerStats returns near realtime stats for a given container.
|
|
|
|
// It's up to the caller to close the io.ReadCloser returned.
|
2016-09-07 23:08:51 +00:00
|
|
|
func (cli *Client) ContainerStats(ctx context.Context, containerID string, stream bool) (types.ContainerStats, error) {
|
2016-09-06 18:46:37 +00:00
|
|
|
query := url.Values{}
|
|
|
|
query.Set("stream", "0")
|
|
|
|
if stream {
|
|
|
|
query.Set("stream", "1")
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := cli.get(ctx, "/containers/"+containerID+"/stats", query, nil)
|
|
|
|
if err != nil {
|
2016-09-07 23:08:51 +00:00
|
|
|
return types.ContainerStats{}, err
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
2016-09-07 23:08:51 +00:00
|
|
|
|
2016-11-09 22:46:53 +00:00
|
|
|
osType := getDockerOS(resp.header.Get("Server"))
|
2016-09-07 23:08:51 +00:00
|
|
|
return types.ContainerStats{Body: resp.body, OSType: osType}, err
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
2020-02-07 23:55:06 +00:00
|
|
|
|
|
|
|
// ContainerStatsOneShot gets a single stat entry from a container.
|
|
|
|
// It differs from `ContainerStats` in that the API should not wait to prime the stats
|
|
|
|
func (cli *Client) ContainerStatsOneShot(ctx context.Context, containerID string) (types.ContainerStats, error) {
|
|
|
|
query := url.Values{}
|
|
|
|
query.Set("stream", "0")
|
|
|
|
query.Set("one-shot", "1")
|
|
|
|
|
|
|
|
resp, err := cli.get(ctx, "/containers/"+containerID+"/stats", query, nil)
|
|
|
|
if err != nil {
|
|
|
|
return types.ContainerStats{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
osType := getDockerOS(resp.header.Get("Server"))
|
|
|
|
return types.ContainerStats{Body: resp.body, OSType: osType}, err
|
|
|
|
}
|