-
Notifications
You must be signed in to change notification settings - Fork 25
Replace container-stats with golang client #2188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
0518786
6d7e570
87c5799
2108ff7
724f0d8
046ab03
c5279ca
b966ef2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ package executor | |
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"strings" | ||
|
@@ -91,6 +92,34 @@ func (d *dockerAPIExecutor) GetContainerHealthCheck(containerID string) (string, | |
return strings.Join(inspectResp.Config.Healthcheck.Test, " "), nil | ||
} | ||
|
||
func (d *dockerAPIExecutor) GetContainerStats(ctx context.Context, containerID string) ( | ||
*ContainerStat, error) { | ||
|
||
// ContainerStatsOneShot add "one-shot" parameter to the API query, which | ||
// instructs the server to get a single stat, rather than waiting for more. | ||
statsResp, err := d.client.ContainerStatsOneShot(ctx, containerID) | ||
if err != nil { | ||
return nil, fmt.Errorf("error getting container stats: %w", err) | ||
} | ||
|
||
var stats container.StatsResponse | ||
decoder := json.NewDecoder(statsResp.Body) | ||
|
||
if err := decoder.Decode(&stats); err == io.EOF { | ||
return nil, nil | ||
Comment on lines
+108
to
+109
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this situation that the response was empty when we queried for the stats? Do we know what could cause this? Should we return some sort of error? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It will bubble up an warning "no stats" later. A reason could be e.g. an empty response body, if the stats api was called before Collector container was started. |
||
} else if err != nil { | ||
return nil, fmt.Errorf("error decoding container stats: %w", err) | ||
} | ||
|
||
return &ContainerStat{ | ||
Timestamp: stats.Read, | ||
Id: stats.ID, | ||
Name: stats.Name, | ||
Mem: stats.MemoryStats.Usage, | ||
Cpu: stats.CPUStats.CPUUsage.TotalUsage, | ||
}, nil | ||
} | ||
|
||
func (d *dockerAPIExecutor) StartContainer(startConfig config.ContainerStartConfig) (string, error) { | ||
ctx := context.Background() | ||
var binds []string | ||
|
@@ -159,7 +188,7 @@ func (d *dockerAPIExecutor) StartContainer(startConfig config.ContainerStartConf | |
|
||
func (d *dockerAPIExecutor) ExecContainer(opts *ExecOptions) (string, error) { | ||
ctx := context.Background() | ||
execConfig := types.ExecConfig{ | ||
execConfig := container.ExecOptions{ | ||
AttachStdout: true, | ||
AttachStderr: true, | ||
Cmd: opts.Command, | ||
|
@@ -170,7 +199,7 @@ func (d *dockerAPIExecutor) ExecContainer(opts *ExecOptions) (string, error) { | |
return "", fmt.Errorf("error creating Exec: %w", err) | ||
} | ||
|
||
execStartCheck := types.ExecStartCheck{Detach: false, Tty: false} | ||
execStartCheck := container.ExecAttachOptions{Detach: false, Tty: false} | ||
attachResp, err := d.client.ContainerExecAttach(ctx, resp.ID, execStartCheck) | ||
if err != nil { | ||
return "", fmt.Errorf("error attaching to Exec: %w", err) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this
ContainerStatsOneShot
return a snapshot of the usage at the time of the call or does it do some sort of aggregation over time? Is the behavior consistent with the CRI counterpart?I tried to figure it out from reading the docs but I couldn't find an answer, I can dig a little more later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The documentation is sparse, but I guess it's the same one-shot as in the API, which just eliminates waiting and returns empty per_cpu:
https://docs.docker.com/reference/api/engine/version/v1.45/#tag/Container/operation/ContainerStats
No aggregation is happening on the client either:
https://github.com/moby/moby/blob/master/client/container_stats.go