github.com/rish1988/moby@v25.0.2+incompatible/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  	return types.ContainerStats{
    25  		Body:   resp.body,
    26  		OSType: getDockerOS(resp.header.Get("Server")),
    27  	}, nil
    28  }
    29  
    30  // ContainerStatsOneShot gets a single stat entry from a container.
    31  // It differs from `ContainerStats` in that the API should not wait to prime the stats
    32  func (cli *Client) ContainerStatsOneShot(ctx context.Context, containerID string) (types.ContainerStats, error) {
    33  	query := url.Values{}
    34  	query.Set("stream", "0")
    35  	query.Set("one-shot", "1")
    36  
    37  	resp, err := cli.get(ctx, "/containers/"+containerID+"/stats", query, nil)
    38  	if err != nil {
    39  		return types.ContainerStats{}, err
    40  	}
    41  
    42  	return types.ContainerStats{
    43  		Body:   resp.body,
    44  		OSType: getDockerOS(resp.header.Get("Server")),
    45  	}, nil
    46  }