44package metrics
55
66import (
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}
0 commit comments