mirror of
https://github.com/moby/moby.git
synced 2026-01-11 18:51:37 +00:00
This utility was added in83b5729f64to replace httputils.ParseServerHeader, which was added to print a warning on Windows in126529c6d0. At the time, the only available option to detect the daemon's OS was to parse the `Server` header, which contained the version of Docker as well as the OS. However,7199522ea2introduced an `OSType` ("Ostype") header that's included on all responses, and a later commite9dac5ef5echanged that to also be included when producing an error for unsupported API versions. Note that the casing in the midddleware was changed from `OSType` to `Ostype` (normalized form) in76a5ca1d4d, but headers are case-insensitive, and `header.Get()` should handle either case in the response. In short; every API response contains an "Ostype" header, which already contains the OS ("windows" or "linux") that doesn't require any parsing, so let's put that header to use. Signed-off-by: Sebastiaan van Stijn <github@gone.nl> (cherry picked from commitfcf3ff1b2f) Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
57 lines
1.5 KiB
Go
57 lines
1.5 KiB
Go
package client
|
|
|
|
import (
|
|
"context"
|
|
"net/url"
|
|
|
|
"github.com/docker/docker/api/types/container"
|
|
)
|
|
|
|
// ContainerStats returns near realtime stats for a given container.
|
|
// It's up to the caller to close the io.ReadCloser returned.
|
|
func (cli *Client) ContainerStats(ctx context.Context, containerID string, stream bool) (container.StatsResponseReader, error) {
|
|
containerID, err := trimID("container", containerID)
|
|
if err != nil {
|
|
return container.StatsResponseReader{}, err
|
|
}
|
|
|
|
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 {
|
|
return container.StatsResponseReader{}, err
|
|
}
|
|
|
|
return container.StatsResponseReader{
|
|
Body: resp.Body,
|
|
OSType: resp.Header.Get("Ostype"),
|
|
}, nil
|
|
}
|
|
|
|
// 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) (container.StatsResponseReader, error) {
|
|
containerID, err := trimID("container", containerID)
|
|
if err != nil {
|
|
return container.StatsResponseReader{}, err
|
|
}
|
|
|
|
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 container.StatsResponseReader{}, err
|
|
}
|
|
|
|
return container.StatsResponseReader{
|
|
Body: resp.Body,
|
|
OSType: resp.Header.Get("Ostype"),
|
|
}, nil
|
|
}
|