Skip to content

Commit f60e0b3

Browse files
authored
Support cgroup v2 (#28)
Fixes #27
1 parent 74271ef commit f60e0b3

File tree

11 files changed

+7023
-697
lines changed

11 files changed

+7023
-697
lines changed

cgroup_exporter.go

Lines changed: 8 additions & 448 deletions
Large diffs are not rendered by default.

cgroup_exporter_test.go

Lines changed: 3 additions & 244 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ import (
1919
"net/http"
2020
"os"
2121
"path/filepath"
22-
"reflect"
2322
"runtime"
2423
"strings"
2524
"testing"
2625
"time"
2726

2827
kingpin "github.com/alecthomas/kingpin/v2"
2928
"github.com/go-kit/log"
29+
"github.com/treydock/cgroup_exporter/collector"
3030
)
3131

3232
const (
@@ -40,12 +40,11 @@ func TestMain(m *testing.M) {
4040
_, filename, _, _ := runtime.Caller(0)
4141
dir := filepath.Dir(filename)
4242
fixture := filepath.Join(dir, "fixtures")
43-
cgroupRoot = &fixture
43+
collector.CgroupRoot = &fixture
4444
procFixture := filepath.Join(fixture, "proc")
45-
procRoot = &procFixture
45+
collector.ProcRoot = &procFixture
4646
varTrue := true
4747
disableExporterMetrics = &varTrue
48-
collectProc = &varTrue
4948
w := log.NewSyncWriter(os.Stderr)
5049
logger := log.NewLogfmtLogger(w)
5150
go func() {
@@ -62,246 +61,6 @@ func TestMain(m *testing.M) {
6261
os.Exit(exitVal)
6362
}
6463

65-
func TestParseCpuSet(t *testing.T) {
66-
expected := []string{"0", "1", "2"}
67-
if cpus, err := parseCpuSet("0-2"); err != nil {
68-
t.Errorf("Unexpected error: %s", err.Error())
69-
} else if !reflect.DeepEqual(cpus, expected) {
70-
t.Errorf("Unexpected cpus, expected %v got %v", expected, cpus)
71-
}
72-
expected = []string{"0", "1", "4", "5", "8", "9"}
73-
if cpus, err := parseCpuSet("0-1,4-5,8-9"); err != nil {
74-
t.Errorf("Unexpected error: %s", err.Error())
75-
} else if !reflect.DeepEqual(cpus, expected) {
76-
t.Errorf("Unexpected cpus, expected %v got %v", expected, cpus)
77-
}
78-
expected = []string{"1", "3", "5", "7"}
79-
if cpus, err := parseCpuSet("1,3,5,7"); err != nil {
80-
t.Errorf("Unexpected error: %s", err.Error())
81-
} else if !reflect.DeepEqual(cpus, expected) {
82-
t.Errorf("Unexpected cpus, expected %v got %v", expected, cpus)
83-
}
84-
}
85-
86-
func TestGetProcInfo(t *testing.T) {
87-
metric := CgroupMetric{}
88-
w := log.NewSyncWriter(os.Stderr)
89-
logger := log.NewLogfmtLogger(w)
90-
getProcInfo([]int{95521, 95525}, &metric, logger)
91-
if val, ok := metric.processExec["/bin/bash"]; !ok {
92-
t.Errorf("Process /bin/bash not in metrics")
93-
return
94-
} else {
95-
if val != 2 {
96-
t.Errorf("Expected 2 /bin/bash processes, got %v", val)
97-
}
98-
}
99-
varLen := 6
100-
collectProcMaxExec = &varLen
101-
getProcInfo([]int{95521, 95525}, &metric, logger)
102-
if val, ok := metric.processExec["/bi...ash"]; !ok {
103-
t.Errorf("Process /bin/bash not in metrics, found: %v", metric.processExec)
104-
return
105-
} else {
106-
if val != 2 {
107-
t.Errorf("Expected 2 /b...sh processes, got %v", val)
108-
}
109-
}
110-
}
111-
112-
func TestCollectUserSlice(t *testing.T) {
113-
varFalse := false
114-
collectProc = &varFalse
115-
w := log.NewSyncWriter(os.Stderr)
116-
logger := log.NewLogfmtLogger(w)
117-
exporter := NewExporter([]string{"/user.slice"}, logger)
118-
metrics, err := exporter.collect()
119-
if err != nil {
120-
t.Errorf("Unexpected error: %s", err.Error())
121-
return
122-
}
123-
if val := len(metrics); val != 1 {
124-
t.Errorf("Unexpected number of metrics, got %d expected 1", val)
125-
return
126-
}
127-
if val := metrics[0].cpuUser; val != 0.41 {
128-
t.Errorf("Unexpected value for cpuUser, got %v", val)
129-
}
130-
if val := metrics[0].cpuSystem; val != 0.39 {
131-
t.Errorf("Unexpected value for cpuSystem, got %v", val)
132-
}
133-
if val := metrics[0].cpuTotal; val != 0.831825022 {
134-
t.Errorf("Unexpected value for cpuTotal, got %v", val)
135-
}
136-
if val := metrics[0].cpus; val != 0 {
137-
t.Errorf("Unexpected value for cpus, got %v", val)
138-
}
139-
if val := metrics[0].memoryRSS; val != 5378048 {
140-
t.Errorf("Unexpected value for memoryRSS, got %v", val)
141-
}
142-
if val := metrics[0].memoryCache; val != 2322432 {
143-
t.Errorf("Unexpected value for memoryCache, got %v", val)
144-
}
145-
if val := metrics[0].memoryUsed; val != 8081408 {
146-
t.Errorf("Unexpected value for memoryUsed, got %v", val)
147-
}
148-
if val := metrics[0].memoryTotal; val != 68719476736 {
149-
t.Errorf("Unexpected value for memoryTotal, got %v", val)
150-
}
151-
if val := metrics[0].memoryFailCount; val != 0 {
152-
t.Errorf("Unexpected value for memoryFailCount, got %v", val)
153-
}
154-
if val := metrics[0].memswUsed; val != 8081408 {
155-
t.Errorf("Unexpected value for swapUsed, got %v", val)
156-
}
157-
if val := metrics[0].memswTotal; val != 9.223372036854772e+18 {
158-
t.Errorf("Unexpected value for swapTotal, got %v", val)
159-
}
160-
if val := metrics[0].memswFailCount; val != 0 {
161-
t.Errorf("Unexpected value for swapFailCount, got %v", val)
162-
}
163-
if val := metrics[0].uid; val != "20821" {
164-
t.Errorf("Unexpected value for uid, got %v", val)
165-
}
166-
}
167-
168-
func TestCollectSLURM(t *testing.T) {
169-
varTrue := true
170-
collectProc = &varTrue
171-
varLen := 100
172-
collectProcMaxExec = &varLen
173-
w := log.NewSyncWriter(os.Stderr)
174-
logger := log.NewLogfmtLogger(w)
175-
exporter := NewExporter([]string{"/slurm"}, logger)
176-
metrics, err := exporter.collect()
177-
if err != nil {
178-
t.Errorf("Unexpected error: %s", err.Error())
179-
return
180-
}
181-
if val := len(metrics); val != 2 {
182-
t.Errorf("Unexpected number of metrics, got %d expected 2", val)
183-
return
184-
}
185-
var m CgroupMetric
186-
for _, metric := range metrics {
187-
if metric.jobid == "10" {
188-
m = metric
189-
}
190-
}
191-
if m.jobid == "" {
192-
t.Errorf("Metrics with jobid=10 not found")
193-
return
194-
}
195-
if val := m.cpuUser; val != 0 {
196-
t.Errorf("Unexpected value for cpuUser, got %v", val)
197-
}
198-
if val := m.cpuSystem; val != 0 {
199-
t.Errorf("Unexpected value for cpuSystem, got %v", val)
200-
}
201-
if val := m.cpuTotal; val != 0.007710215 {
202-
t.Errorf("Unexpected value for cpuTotal, got %v", val)
203-
}
204-
if val := m.cpus; val != 2 {
205-
t.Errorf("Unexpected value for cpus, got %v", val)
206-
}
207-
if val := m.memoryRSS; val != 311296 {
208-
t.Errorf("Unexpected value for memoryRSS, got %v", val)
209-
}
210-
if val := m.memoryCache; val != 4096 {
211-
t.Errorf("Unexpected value for memoryCache, got %v", val)
212-
}
213-
if val := m.memoryUsed; val != 356352 {
214-
t.Errorf("Unexpected value for memoryUsed, got %v", val)
215-
}
216-
if val := m.memoryTotal; val != 2147483648 {
217-
t.Errorf("Unexpected value for memoryTotal, got %v", val)
218-
}
219-
if val := m.memoryFailCount; val != 0 {
220-
t.Errorf("Unexpected value for memoryFailCount, got %v", val)
221-
}
222-
if val := m.memswUsed; val != 356352 {
223-
t.Errorf("Unexpected value for swapUsed, got %v", val)
224-
}
225-
if val := m.memswTotal; val != 2147483648 {
226-
t.Errorf("Unexpected value for swapTotal, got %v", val)
227-
}
228-
if val := m.memswFailCount; val != 0 {
229-
t.Errorf("Unexpected value for swapFailCount, got %v", val)
230-
}
231-
if val := m.uid; val != "20821" {
232-
t.Errorf("Unexpected value for uid, got %v", val)
233-
}
234-
if val := m.jobid; val != "10" {
235-
t.Errorf("Unexpected value for jobid, got %v", val)
236-
}
237-
if val, ok := m.processExec["/bin/bash"]; !ok {
238-
t.Errorf("processExec does not contain /bin/bash")
239-
} else {
240-
if val != 2 {
241-
t.Errorf("Unexpected 2 values for processExec /bin/bash, got %v", val)
242-
}
243-
}
244-
}
245-
246-
func TestCollectTorque(t *testing.T) {
247-
varFalse := false
248-
collectProc = &varFalse
249-
w := log.NewSyncWriter(os.Stderr)
250-
logger := log.NewLogfmtLogger(w)
251-
exporter := NewExporter([]string{"/torque"}, logger)
252-
metrics, err := exporter.collect()
253-
if err != nil {
254-
t.Errorf("Unexpected error: %s", err.Error())
255-
return
256-
}
257-
if val := len(metrics); val != 1 {
258-
t.Errorf("Unexpected number of metrics, got %d expected 1", val)
259-
return
260-
}
261-
if val := metrics[0].cpuUser; val != 153146.31 {
262-
t.Errorf("Unexpected value for cpuUser, got %v", val)
263-
}
264-
if val := metrics[0].cpuSystem; val != 260.77 {
265-
t.Errorf("Unexpected value for cpuSystem, got %v", val)
266-
}
267-
if val := metrics[0].cpuTotal; val != 152995.785583781 {
268-
t.Errorf("Unexpected value for cpuTotal, got %v", val)
269-
}
270-
if val := metrics[0].cpus; val != 40 {
271-
t.Errorf("Unexpected value for cpus, got %v", val)
272-
}
273-
if val := metrics[0].memoryRSS; val != 82444320768 {
274-
t.Errorf("Unexpected value for memoryRSS, got %v", val)
275-
}
276-
if val := metrics[0].memoryCache; val != 109678592 {
277-
t.Errorf("Unexpected value for memoryCache, got %v", val)
278-
}
279-
if val := metrics[0].memoryUsed; val != 82553999360 {
280-
t.Errorf("Unexpected value for memoryUsed, got %v", val)
281-
}
282-
if val := metrics[0].memoryTotal; val != 196755132416 {
283-
t.Errorf("Unexpected value for memoryTotal, got %v", val)
284-
}
285-
if val := metrics[0].memoryFailCount; val != 0 {
286-
t.Errorf("Unexpected value for memoryFailCount, got %v", val)
287-
}
288-
if val := metrics[0].memswUsed; val != 82553999360 {
289-
t.Errorf("Unexpected value for swapUsed, got %v", val)
290-
}
291-
if val := metrics[0].memswTotal; val != 196755132416 {
292-
t.Errorf("Unexpected value for swapTotal, got %v", val)
293-
}
294-
if val := metrics[0].memswFailCount; val != 0 {
295-
t.Errorf("Unexpected value for swapFailCount, got %v", val)
296-
}
297-
if val := metrics[0].uid; val != "" {
298-
t.Errorf("Unexpected value for uid, got %v", val)
299-
}
300-
if val := metrics[0].jobid; val != "1182724" {
301-
t.Errorf("Unexpected value for jobid, got %v", val)
302-
}
303-
}
304-
30564
func TestMetricsHandler(t *testing.T) {
30665
body, err := queryExporter()
30766
if err != nil {

0 commit comments

Comments
 (0)