Skip to content

Commit ed45952

Browse files
committed
Use cgroups proto for prom metrics
Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
1 parent 0973a08 commit ed45952

File tree

9 files changed

+92
-82
lines changed

9 files changed

+92
-82
lines changed

linux/task.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type Task struct {
1919
pid int
2020
shim *client.Client
2121
namespace string
22-
cg *cgroups.Cgroup
22+
cg cgroups.Cgroup
2323
}
2424

2525
func newTask(id, namespace string, pid int, shim *client.Client) (*Task, error) {
@@ -210,5 +210,13 @@ func (t *Task) Process(ctx context.Context, id string) (runtime.Process, error)
210210
}
211211

212212
func (t *Task) Metrics(ctx context.Context) (interface{}, error) {
213-
return nil, nil
213+
stats, err := t.cg.Stat(cgroups.IgnoreNotExist)
214+
if err != nil {
215+
return nil, err
216+
}
217+
return stats, nil
218+
}
219+
220+
func (t *Task) Cgroup() cgroups.Cgroup {
221+
return t.cg
214222
}

metrics/cgroups/blkio.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var blkioMetrics = []*metric{
1515
unit: metrics.Total,
1616
vt: prometheus.GaugeValue,
1717
labels: []string{"op", "device", "major", "minor"},
18-
getValues: func(stats *cgroups.Stats) []value {
18+
getValues: func(stats *cgroups.Metrics) []value {
1919
if stats.Blkio == nil {
2020
return nil
2121
}
@@ -28,7 +28,7 @@ var blkioMetrics = []*metric{
2828
unit: metrics.Total,
2929
vt: prometheus.GaugeValue,
3030
labels: []string{"op", "device", "major", "minor"},
31-
getValues: func(stats *cgroups.Stats) []value {
31+
getValues: func(stats *cgroups.Metrics) []value {
3232
if stats.Blkio == nil {
3333
return nil
3434
}
@@ -41,7 +41,7 @@ var blkioMetrics = []*metric{
4141
unit: metrics.Bytes,
4242
vt: prometheus.GaugeValue,
4343
labels: []string{"op", "device", "major", "minor"},
44-
getValues: func(stats *cgroups.Stats) []value {
44+
getValues: func(stats *cgroups.Metrics) []value {
4545
if stats.Blkio == nil {
4646
return nil
4747
}
@@ -54,7 +54,7 @@ var blkioMetrics = []*metric{
5454
unit: metrics.Total,
5555
vt: prometheus.GaugeValue,
5656
labels: []string{"op", "device", "major", "minor"},
57-
getValues: func(stats *cgroups.Stats) []value {
57+
getValues: func(stats *cgroups.Metrics) []value {
5858
if stats.Blkio == nil {
5959
return nil
6060
}
@@ -67,7 +67,7 @@ var blkioMetrics = []*metric{
6767
unit: metrics.Total,
6868
vt: prometheus.GaugeValue,
6969
labels: []string{"op", "device", "major", "minor"},
70-
getValues: func(stats *cgroups.Stats) []value {
70+
getValues: func(stats *cgroups.Metrics) []value {
7171
if stats.Blkio == nil {
7272
return nil
7373
}
@@ -80,7 +80,7 @@ var blkioMetrics = []*metric{
8080
unit: metrics.Total,
8181
vt: prometheus.GaugeValue,
8282
labels: []string{"op", "device", "major", "minor"},
83-
getValues: func(stats *cgroups.Stats) []value {
83+
getValues: func(stats *cgroups.Metrics) []value {
8484
if stats.Blkio == nil {
8585
return nil
8686
}
@@ -93,7 +93,7 @@ var blkioMetrics = []*metric{
9393
unit: metrics.Total,
9494
vt: prometheus.GaugeValue,
9595
labels: []string{"op", "device", "major", "minor"},
96-
getValues: func(stats *cgroups.Stats) []value {
96+
getValues: func(stats *cgroups.Metrics) []value {
9797
if stats.Blkio == nil {
9898
return nil
9999
}

metrics/cgroups/cgroups.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/containerd/cgroups"
77
eventsapi "github.com/containerd/containerd/api/services/events/v1"
88
"github.com/containerd/containerd/events"
9+
"github.com/containerd/containerd/linux"
910
"github.com/containerd/containerd/log"
1011
"github.com/containerd/containerd/namespaces"
1112
"github.com/containerd/containerd/plugin"
@@ -49,10 +50,11 @@ type cgroupsMonitor struct {
4950

5051
func (m *cgroupsMonitor) Monitor(c runtime.Task) error {
5152
info := c.Info()
52-
if err := m.collector.Add(info.ID, info.Namespace, c); err != nil {
53+
t := c.(*linux.Task)
54+
if err := m.collector.Add(info.ID, info.Namespace, t.Cgroup()); err != nil {
5355
return err
5456
}
55-
return m.oom.Add(info.ID, info.Namespace, cg, m.trigger)
57+
return m.oom.Add(info.ID, info.Namespace, t.Cgroup(), m.trigger)
5658
}
5759

5860
func (m *cgroupsMonitor) Stop(c runtime.Task) error {

metrics/cgroups/cpu.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var cpuMetrics = []*metric{
1616
help: "The total cpu time",
1717
unit: metrics.Nanoseconds,
1818
vt: prometheus.GaugeValue,
19-
getValues: func(stats *cgroups.Stats) []value {
19+
getValues: func(stats *cgroups.Metrics) []value {
2020
if stats.Cpu == nil {
2121
return nil
2222
}
@@ -32,7 +32,7 @@ var cpuMetrics = []*metric{
3232
help: "The total kernel cpu time",
3333
unit: metrics.Nanoseconds,
3434
vt: prometheus.GaugeValue,
35-
getValues: func(stats *cgroups.Stats) []value {
35+
getValues: func(stats *cgroups.Metrics) []value {
3636
if stats.Cpu == nil {
3737
return nil
3838
}
@@ -48,7 +48,7 @@ var cpuMetrics = []*metric{
4848
help: "The total user cpu time",
4949
unit: metrics.Nanoseconds,
5050
vt: prometheus.GaugeValue,
51-
getValues: func(stats *cgroups.Stats) []value {
51+
getValues: func(stats *cgroups.Metrics) []value {
5252
if stats.Cpu == nil {
5353
return nil
5454
}
@@ -65,7 +65,7 @@ var cpuMetrics = []*metric{
6565
unit: metrics.Nanoseconds,
6666
vt: prometheus.GaugeValue,
6767
labels: []string{"cpu"},
68-
getValues: func(stats *cgroups.Stats) []value {
68+
getValues: func(stats *cgroups.Metrics) []value {
6969
if stats.Cpu == nil {
7070
return nil
7171
}
@@ -84,7 +84,7 @@ var cpuMetrics = []*metric{
8484
help: "The total cpu throttle periods",
8585
unit: metrics.Total,
8686
vt: prometheus.GaugeValue,
87-
getValues: func(stats *cgroups.Stats) []value {
87+
getValues: func(stats *cgroups.Metrics) []value {
8888
if stats.Cpu == nil {
8989
return nil
9090
}
@@ -100,7 +100,7 @@ var cpuMetrics = []*metric{
100100
help: "The total cpu throttled periods",
101101
unit: metrics.Total,
102102
vt: prometheus.GaugeValue,
103-
getValues: func(stats *cgroups.Stats) []value {
103+
getValues: func(stats *cgroups.Metrics) []value {
104104
if stats.Cpu == nil {
105105
return nil
106106
}
@@ -116,7 +116,7 @@ var cpuMetrics = []*metric{
116116
help: "The total cpu throttled time",
117117
unit: metrics.Nanoseconds,
118118
vt: prometheus.GaugeValue,
119-
getValues: func(stats *cgroups.Stats) []value {
119+
getValues: func(stats *cgroups.Metrics) []value {
120120
if stats.Cpu == nil {
121121
return nil
122122
}

metrics/cgroups/hugetlb.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var hugetlbMetrics = []*metric{
1515
unit: metrics.Bytes,
1616
vt: prometheus.GaugeValue,
1717
labels: []string{"page"},
18-
getValues: func(stats *cgroups.Stats) []value {
18+
getValues: func(stats *cgroups.Metrics) []value {
1919
if stats.Hugetlb == nil {
2020
return nil
2121
}
@@ -35,7 +35,7 @@ var hugetlbMetrics = []*metric{
3535
unit: metrics.Total,
3636
vt: prometheus.GaugeValue,
3737
labels: []string{"page"},
38-
getValues: func(stats *cgroups.Stats) []value {
38+
getValues: func(stats *cgroups.Metrics) []value {
3939
if stats.Hugetlb == nil {
4040
return nil
4141
}
@@ -55,7 +55,7 @@ var hugetlbMetrics = []*metric{
5555
unit: metrics.Bytes,
5656
vt: prometheus.GaugeValue,
5757
labels: []string{"page"},
58-
getValues: func(stats *cgroups.Stats) []value {
58+
getValues: func(stats *cgroups.Metrics) []value {
5959
if stats.Hugetlb == nil {
6060
return nil
6161
}

0 commit comments

Comments
 (0)