Skip to content

Commit 2c87d12

Browse files
committed
ctr: add new metrics subcommand
Signed-off-by: Samuel Karp <[email protected]>
1 parent c8017d0 commit 2c87d12

1 file changed

Lines changed: 83 additions & 0 deletions

File tree

cmd/ctr/commands/tasks/metrics.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// +build linux
2+
3+
/*
4+
Copyright The containerd Authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package tasks
20+
21+
import (
22+
"errors"
23+
"fmt"
24+
25+
"os"
26+
"text/tabwriter"
27+
28+
"github.com/containerd/cgroups"
29+
"github.com/containerd/containerd/cmd/ctr/commands"
30+
"github.com/containerd/typeurl"
31+
"github.com/urfave/cli"
32+
)
33+
34+
func init() {
35+
// metricsCommand is only added on Linux as github.com/containerd/cgroups
36+
// does not compile on darwin or windows
37+
Command.Subcommands = append(Command.Subcommands, metricsCommand)
38+
}
39+
40+
var metricsCommand = cli.Command{
41+
Name: "metrics",
42+
Usage: "get a single data point of metrics for a task with the built-in Linux runtime",
43+
ArgsUsage: "CONTAINER",
44+
Aliases: []string{"metric"},
45+
Action: func(context *cli.Context) error {
46+
client, ctx, cancel, err := commands.NewClient(context)
47+
if err != nil {
48+
return err
49+
}
50+
defer cancel()
51+
container, err := client.LoadContainer(ctx, context.Args().First())
52+
if err != nil {
53+
return err
54+
}
55+
task, err := container.Task(ctx, nil)
56+
if err != nil {
57+
return err
58+
}
59+
metric, err := task.Metrics(ctx)
60+
if err != nil {
61+
return nil
62+
}
63+
w := tabwriter.NewWriter(os.Stdout, 1, 8, 4, ' ', 0)
64+
fmt.Fprintf(w, "ID\tTIMESTAMP\t\n")
65+
fmt.Fprintf(w, "%s\t%s\t\n\n", metric.ID, metric.Timestamp)
66+
67+
anydata, err := typeurl.UnmarshalAny(metric.Data)
68+
if err != nil {
69+
return err
70+
}
71+
data, ok := anydata.(*cgroups.Metrics)
72+
if !ok {
73+
return errors.New("cannot convert metric data to cgroups.Metrics")
74+
}
75+
fmt.Fprintf(w, "METRIC\tVALUE\t\n")
76+
fmt.Fprintf(w, "memory.usage_in_bytes\t%d\t\n", data.Memory.Usage.Usage)
77+
fmt.Fprintf(w, "memory.stat.cache\t%d\t\n", data.Memory.TotalCache)
78+
fmt.Fprintf(w, "cpuacct.usage\t%d\t\n", data.CPU.Usage.Total)
79+
fmt.Fprintf(w, "cpuacct.usage_percpu\t%v\t\n", data.CPU.Usage.PerCPU)
80+
81+
return w.Flush()
82+
},
83+
}

0 commit comments

Comments
 (0)