container_stats.go 1.3 KB

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