Skip to content

Commit 3ba5246

Browse files
authored
fix metrics tests (#4146)
Signed-off-by: Joshua Kim <20001595+joshua-kim@users.noreply.github.com>
1 parent 0cb887b commit 3ba5246

File tree

3 files changed

+36
-104
lines changed

3 files changed

+36
-104
lines changed

.golangci.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ linters:
3636
- bodyclose
3737
- copyloopvar
3838
- depguard
39-
- dupword
4039
- errcheck
4140
- errorlint
4241
- forbidigo

api/metrics/label_gatherer_test.go

Lines changed: 20 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
package metrics
55

66
import (
7+
"strings"
78
"testing"
89

910
"github.com/prometheus/client_golang/prometheus"
11+
"github.com/prometheus/client_golang/prometheus/testutil"
1012
"github.com/stretchr/testify/require"
11-
"google.golang.org/protobuf/proto"
1213

1314
dto "github.com/prometheus/client_model/go"
1415
)
@@ -23,68 +24,24 @@ func TestLabelGatherer_Gather(t *testing.T) {
2324
customLabelValueB = "b"
2425
)
2526
tests := []struct {
26-
name string
27-
labelName string
28-
expectedMetrics []*dto.Metric
29-
expectErr bool
27+
name string
28+
labelName string
29+
wantMetrics string
30+
expectErr bool
3031
}{
3132
{
3233
name: "no overlap",
3334
labelName: customLabelName,
34-
expectedMetrics: []*dto.Metric{
35-
{
36-
Label: []*dto.LabelPair{
37-
{
38-
Name: proto.String(labelName),
39-
Value: proto.String(labelValueB),
40-
},
41-
{
42-
Name: proto.String(customLabelName),
43-
Value: proto.String(customLabelValueB),
44-
},
45-
},
46-
Counter: &dto.Counter{
47-
Value: proto.Float64(1),
48-
},
49-
},
50-
{
51-
Label: []*dto.LabelPair{
52-
{
53-
Name: proto.String(labelName),
54-
Value: proto.String(labelValueA),
55-
},
56-
{
57-
Name: proto.String(customLabelName),
58-
Value: proto.String(customLabelValueA),
59-
},
60-
},
61-
Counter: &dto.Counter{
62-
Value: proto.Float64(0),
63-
},
64-
},
65-
},
66-
expectErr: false,
35+
wantMetrics: `
36+
# HELP counter help
37+
# TYPE counter counter
38+
counter{smith="morty",tag="b"} 1
39+
counter{smith="rick",tag="a"} 0
40+
`,
6741
},
6842
{
6943
name: "has overlap",
7044
labelName: labelName,
71-
expectedMetrics: []*dto.Metric{
72-
{
73-
Label: []*dto.LabelPair{
74-
{
75-
Name: proto.String(labelName),
76-
Value: proto.String(labelValueB),
77-
},
78-
{
79-
Name: proto.String(customLabelName),
80-
Value: proto.String(customLabelValueB),
81-
},
82-
},
83-
Counter: &dto.Counter{
84-
Value: proto.Float64(1),
85-
},
86-
},
87-
},
8845
expectErr: true,
8946
},
9047
}
@@ -117,23 +74,17 @@ func TestLabelGatherer_Gather(t *testing.T) {
11774
require.NoError(registerB.Register(counterB))
11875
}
11976

120-
metrics, err := gatherer.Gather()
12177
if test.expectErr {
122-
require.Error(err) //nolint:forbidigo // the error is not exported
78+
require.Error(testutil.GatherAndCompare( //nolint:forbidigo // the error is not exported
79+
gatherer,
80+
strings.NewReader(test.wantMetrics),
81+
))
12382
} else {
124-
require.NoError(err)
83+
require.NoError(testutil.GatherAndCompare(
84+
gatherer,
85+
strings.NewReader(test.wantMetrics),
86+
))
12587
}
126-
require.Equal(
127-
[]*dto.MetricFamily{
128-
{
129-
Name: proto.String(counterOpts.Name),
130-
Help: proto.String(counterOpts.Help),
131-
Type: dto.MetricType_COUNTER.Enum(),
132-
Metric: test.expectedMetrics,
133-
},
134-
},
135-
metrics,
136-
)
13788
})
13889
}
13990
}

api/metrics/prefix_gatherer_test.go

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
package metrics
55

66
import (
7+
"strings"
78
"testing"
89

910
"github.com/prometheus/client_golang/prometheus"
11+
"github.com/prometheus/client_golang/prometheus/testutil"
1012
"github.com/stretchr/testify/require"
11-
"google.golang.org/protobuf/proto"
1213

1314
dto "github.com/prometheus/client_model/go"
1415
)
@@ -34,39 +35,20 @@ func TestPrefixGatherer_Gather(t *testing.T) {
3435
require.NoError(registerB.Register(counterB))
3536
}
3637

37-
metrics, err := gatherer.Gather()
38-
require.NoError(err)
39-
require.Equal(
40-
[]*dto.MetricFamily{
41-
{
42-
Name: proto.String("a_counter"),
43-
Help: proto.String(counterOpts.Help),
44-
Type: dto.MetricType_COUNTER.Enum(),
45-
Metric: []*dto.Metric{
46-
{
47-
Label: []*dto.LabelPair{},
48-
Counter: &dto.Counter{
49-
Value: proto.Float64(0),
50-
},
51-
},
52-
},
53-
},
54-
{
55-
Name: proto.String("b_counter"),
56-
Help: proto.String(counterOpts.Help),
57-
Type: dto.MetricType_COUNTER.Enum(),
58-
Metric: []*dto.Metric{
59-
{
60-
Label: []*dto.LabelPair{},
61-
Counter: &dto.Counter{
62-
Value: proto.Float64(1),
63-
},
64-
},
65-
},
66-
},
67-
},
68-
metrics,
69-
)
38+
wantMetrics := `
39+
# HELP a_counter help
40+
# TYPE a_counter counter
41+
a_counter 0
42+
43+
# HELP b_counter help
44+
# TYPE b_counter counter
45+
b_counter 1
46+
`
47+
48+
require.NoError(testutil.GatherAndCompare(
49+
gatherer,
50+
strings.NewReader(wantMetrics),
51+
))
7052
}
7153

7254
func TestPrefixGatherer_Register(t *testing.T) {

0 commit comments

Comments
 (0)