container_stats.go 1.3 KB

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