Skip to content

Commit 53f4006

Browse files
Sebastian Borzasparrc
Sebastian Borza
authored andcommitted
Moving cgroup path name to field from tag to reduce cardinality (influxdata#1457)
adding assertContainsFields function to cgroup_test for custom validation
1 parent 97d92bb commit 53f4006

File tree

3 files changed

+53
-41
lines changed

3 files changed

+53
-41
lines changed

plugins/inputs/cgroup/README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ KEY1 VAL1\n
3333

3434
### Tags:
3535

36-
All measurements have the following tags:
37-
- path
36+
Measurements don't have any specific tags unless you define them at the telegraf level (defaults). We
37+
used to have the path listed as a tag, but to keep cardinality in check it's easier to move this
38+
value to a field. Thanks @sebito91!
3839

3940

4041
### Configuration:

plugins/inputs/cgroup/cgroup_linux.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,9 @@ func (g *CGroup) gatherDir(dir string, acc telegraf.Accumulator) error {
5656
return err
5757
}
5858
}
59+
fields["path"] = dir
5960

60-
tags := map[string]string{"path": dir}
61-
62-
acc.AddFields(metricName, fields, tags)
61+
acc.AddFields(metricName, fields, nil)
6362

6463
return nil
6564
}

plugins/inputs/cgroup/cgroup_test.go

+48-36
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
package cgroup
44

55
import (
6+
"fmt"
67
"testing"
78

89
"github.com/influxdata/telegraf/testutil"
10+
"github.com/stretchr/testify/assert"
911
"github.com/stretchr/testify/require"
12+
"reflect"
1013
)
1114

1215
var cg1 = &CGroup{
@@ -21,15 +24,32 @@ var cg1 = &CGroup{
2124
},
2225
}
2326

27+
func assertContainsFields(a *testutil.Accumulator, t *testing.T, measurement string, fieldSet []map[string]interface{}) {
28+
a.Lock()
29+
defer a.Unlock()
30+
31+
numEquals := 0
32+
for _, p := range a.Metrics {
33+
if p.Measurement == measurement {
34+
for _, fields := range fieldSet {
35+
if reflect.DeepEqual(fields, p.Fields) {
36+
numEquals++
37+
}
38+
}
39+
}
40+
}
41+
42+
if numEquals != len(fieldSet) {
43+
assert.Fail(t, fmt.Sprintf("only %d of %d are equal", numEquals, len(fieldSet)))
44+
}
45+
}
46+
2447
func TestCgroupStatistics_1(t *testing.T) {
2548
var acc testutil.Accumulator
2649

2750
err := cg1.Gather(&acc)
2851
require.NoError(t, err)
2952

30-
tags := map[string]string{
31-
"path": "testdata/memory",
32-
}
3353
fields := map[string]interface{}{
3454
"memory.stat.cache": 1739362304123123123,
3555
"memory.stat.rss": 1775325184,
@@ -42,8 +62,9 @@ func TestCgroupStatistics_1(t *testing.T) {
4262
"memory.limit_in_bytes": 223372036854771712,
4363
"memory.use_hierarchy": "12-781",
4464
"notify_on_release": 0,
65+
"path": "testdata/memory",
4566
}
46-
acc.AssertContainsTaggedFields(t, "cgroup", fields, tags)
67+
assertContainsFields(&acc, t, "cgroup", []map[string]interface{}{fields})
4768
}
4869

4970
// ======================================================================
@@ -59,16 +80,14 @@ func TestCgroupStatistics_2(t *testing.T) {
5980
err := cg2.Gather(&acc)
6081
require.NoError(t, err)
6182

62-
tags := map[string]string{
63-
"path": "testdata/cpu",
64-
}
6583
fields := map[string]interface{}{
6684
"cpuacct.usage_percpu.0": -1452543795404,
6785
"cpuacct.usage_percpu.1": 1376681271659,
6886
"cpuacct.usage_percpu.2": 1450950799997,
6987
"cpuacct.usage_percpu.3": -1473113374257,
88+
"path": "testdata/cpu",
7089
}
71-
acc.AssertContainsTaggedFields(t, "cgroup", fields, tags)
90+
assertContainsFields(&acc, t, "cgroup", []map[string]interface{}{fields})
7291
}
7392

7493
// ======================================================================
@@ -84,18 +103,16 @@ func TestCgroupStatistics_3(t *testing.T) {
84103
err := cg3.Gather(&acc)
85104
require.NoError(t, err)
86105

87-
tags := map[string]string{
88-
"path": "testdata/memory/group_1",
89-
}
90106
fields := map[string]interface{}{
91107
"memory.limit_in_bytes": 223372036854771712,
108+
"path": "testdata/memory/group_1",
92109
}
93-
acc.AssertContainsTaggedFields(t, "cgroup", fields, tags)
94110

95-
tags = map[string]string{
96-
"path": "testdata/memory/group_2",
111+
fieldsTwo := map[string]interface{}{
112+
"memory.limit_in_bytes": 223372036854771712,
113+
"path": "testdata/memory/group_2",
97114
}
98-
acc.AssertContainsTaggedFields(t, "cgroup", fields, tags)
115+
assertContainsFields(&acc, t, "cgroup", []map[string]interface{}{fields, fieldsTwo})
99116
}
100117

101118
// ======================================================================
@@ -111,23 +128,22 @@ func TestCgroupStatistics_4(t *testing.T) {
111128
err := cg4.Gather(&acc)
112129
require.NoError(t, err)
113130

114-
tags := map[string]string{
115-
"path": "testdata/memory/group_1/group_1_1",
116-
}
117131
fields := map[string]interface{}{
118132
"memory.limit_in_bytes": 223372036854771712,
133+
"path": "testdata/memory/group_1/group_1_1",
119134
}
120-
acc.AssertContainsTaggedFields(t, "cgroup", fields, tags)
121135

122-
tags = map[string]string{
123-
"path": "testdata/memory/group_1/group_1_2",
136+
fieldsTwo := map[string]interface{}{
137+
"memory.limit_in_bytes": 223372036854771712,
138+
"path": "testdata/memory/group_1/group_1_2",
124139
}
125-
acc.AssertContainsTaggedFields(t, "cgroup", fields, tags)
126140

127-
tags = map[string]string{
128-
"path": "testdata/memory/group_2",
141+
fieldsThree := map[string]interface{}{
142+
"memory.limit_in_bytes": 223372036854771712,
143+
"path": "testdata/memory/group_2",
129144
}
130-
acc.AssertContainsTaggedFields(t, "cgroup", fields, tags)
145+
146+
assertContainsFields(&acc, t, "cgroup", []map[string]interface{}{fields, fieldsTwo, fieldsThree})
131147
}
132148

133149
// ======================================================================
@@ -143,18 +159,16 @@ func TestCgroupStatistics_5(t *testing.T) {
143159
err := cg5.Gather(&acc)
144160
require.NoError(t, err)
145161

146-
tags := map[string]string{
147-
"path": "testdata/memory/group_1/group_1_1",
148-
}
149162
fields := map[string]interface{}{
150163
"memory.limit_in_bytes": 223372036854771712,
164+
"path": "testdata/memory/group_1/group_1_1",
151165
}
152-
acc.AssertContainsTaggedFields(t, "cgroup", fields, tags)
153166

154-
tags = map[string]string{
155-
"path": "testdata/memory/group_2/group_1_1",
167+
fieldsTwo := map[string]interface{}{
168+
"memory.limit_in_bytes": 223372036854771712,
169+
"path": "testdata/memory/group_2/group_1_1",
156170
}
157-
acc.AssertContainsTaggedFields(t, "cgroup", fields, tags)
171+
assertContainsFields(&acc, t, "cgroup", []map[string]interface{}{fields, fieldsTwo})
158172
}
159173

160174
// ======================================================================
@@ -170,13 +184,11 @@ func TestCgroupStatistics_6(t *testing.T) {
170184
err := cg6.Gather(&acc)
171185
require.NoError(t, err)
172186

173-
tags := map[string]string{
174-
"path": "testdata/memory",
175-
}
176187
fields := map[string]interface{}{
177188
"memory.usage_in_bytes": 3513667584,
178189
"memory.use_hierarchy": "12-781",
179190
"memory.kmem.limit_in_bytes": 9223372036854771712,
191+
"path": "testdata/memory",
180192
}
181-
acc.AssertContainsTaggedFields(t, "cgroup", fields, tags)
193+
assertContainsFields(&acc, t, "cgroup", []map[string]interface{}{fields})
182194
}

0 commit comments

Comments
 (0)