Skip to content

Commit c3cac72

Browse files
committed
ctr: fix potential panic in metric
Signed-off-by: Ace-Tang <[email protected]>
1 parent a4b6522 commit c3cac72

1 file changed

Lines changed: 13 additions & 7 deletions

File tree

cmd/ctr/commands/tasks/metrics.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,19 @@ var metricsCommand = cli.Command{
8989
fmt.Fprintf(w, "%s\t%s\t\n\n", metric.ID, metric.Timestamp)
9090

9191
fmt.Fprintf(w, "METRIC\tVALUE\t\n")
92-
fmt.Fprintf(w, "memory.usage_in_bytes\t%d\t\n", data.Memory.Usage.Usage)
93-
fmt.Fprintf(w, "memory.limit_in_bytes\t%d\t\n", data.Memory.Usage.Limit)
94-
fmt.Fprintf(w, "memory.stat.cache\t%d\t\n", data.Memory.TotalCache)
95-
fmt.Fprintf(w, "cpuacct.usage\t%d\t\n", data.CPU.Usage.Total)
96-
fmt.Fprintf(w, "cpuacct.usage_percpu\t%v\t\n", data.CPU.Usage.PerCPU)
97-
fmt.Fprintf(w, "pids.current\t%v\t\n", data.Pids.Current)
98-
fmt.Fprintf(w, "pids.limit\t%v\t\n", data.Pids.Limit)
92+
if data.Memory != nil {
93+
fmt.Fprintf(w, "memory.usage_in_bytes\t%d\t\n", data.Memory.Usage.Usage)
94+
fmt.Fprintf(w, "memory.limit_in_bytes\t%d\t\n", data.Memory.Usage.Limit)
95+
fmt.Fprintf(w, "memory.stat.cache\t%d\t\n", data.Memory.TotalCache)
96+
}
97+
if data.CPU != nil {
98+
fmt.Fprintf(w, "cpuacct.usage\t%d\t\n", data.CPU.Usage.Total)
99+
fmt.Fprintf(w, "cpuacct.usage_percpu\t%v\t\n", data.CPU.Usage.PerCPU)
100+
}
101+
if data.Pids != nil {
102+
fmt.Fprintf(w, "pids.current\t%v\t\n", data.Pids.Current)
103+
fmt.Fprintf(w, "pids.limit\t%v\t\n", data.Pids.Limit)
104+
}
99105
return w.Flush()
100106
case formatJSON:
101107
marshaledJSON, err := json.MarshalIndent(data, "", " ")

0 commit comments

Comments
 (0)