github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/engine/client/container_stats.go (about) 1 package client // import "github.com/docker/docker/client" 2 3 import ( 4 "context" 5 "net/url" 6 7 "github.com/docker/docker/api/types" 8 ) 9 10 // ContainerStats returns near realtime stats for a given container. 11 // It's up to the caller to close the io.ReadCloser returned. 12 func (cli *Client) ContainerStats(ctx context.Context, containerID string, stream bool) (types.ContainerStats, error) { 13 query := url.Values{} 14 query.Set("stream", "0") 15 if stream { 16 query.Set("stream", "1") 17 } 18 19 resp, err := cli.get(ctx, "/containers/"+containerID+"/stats", query, nil) 20 if err != nil { 21 return types.ContainerStats{}, err 22 } 23 24 osType := getDockerOS(resp.header.Get("Server")) 25 return types.ContainerStats{Body: resp.body, OSType: osType}, err 26 } 27 28 // ContainerStatsOneShot gets a single stat entry from a container. 29 // It differs from `ContainerStats` in that the API should not wait to prime the stats 30 func (cli *Client) ContainerStatsOneShot(ctx context.Context, containerID string) (types.ContainerStats, error) { 31 query := url.Values{} 32 query.Set("stream", "0") 33 query.Set("one-shot", "1") 34 35 resp, err := cli.get(ctx, "/containers/"+containerID+"/stats", query, nil) 36 if err != nil { 37 return types.ContainerStats{}, err 38 } 39 40 osType := getDockerOS(resp.header.Get("Server")) 41 return types.ContainerStats{Body: resp.body, OSType: osType}, err 42 }