11package compactor
22
33import (
4+ "bytes"
45 "testing"
56 "time"
67
8+ "github.com/prometheus/client_golang/prometheus/testutil"
9+
710 "github.com/oklog/ulid"
811 "github.com/prometheus/client_golang/prometheus"
912 "github.com/prometheus/client_golang/prometheus/promauto"
@@ -112,6 +115,7 @@ func TestShuffleShardingGrouper_Groups(t *testing.T) {
112115 ranges []time.Duration
113116 blocks map [ulid.ULID ]* metadata.Meta
114117 expected [][]ulid.ULID
118+ metrics string
115119 }{
116120 "test basic grouping" : {
117121 ranges : []time.Duration {2 * time .Hour , 4 * time .Hour },
@@ -121,11 +125,19 @@ func TestShuffleShardingGrouper_Groups(t *testing.T) {
121125 {block1hto2hExt1Ulid , block0hto1hExt1Ulid },
122126 {block3hto4hExt1Ulid , block2hto3hExt1Ulid },
123127 },
128+ metrics : `# HELP cortex_compactor_remaining_planned_compactions Total number of plans that remain to be compacted.
129+ # TYPE cortex_compactor_remaining_planned_compactions gauge
130+ cortex_compactor_remaining_planned_compactions 3
131+ ` ,
124132 },
125133 "test no compaction" : {
126134 ranges : []time.Duration {2 * time .Hour , 4 * time .Hour },
127135 blocks : map [ulid.ULID ]* metadata.Meta {block0hto1hExt1Ulid : blocks [block0hto1hExt1Ulid ], block0hto1hExt2Ulid : blocks [block0hto1hExt2Ulid ], block0to1hExt3Ulid : blocks [block0to1hExt3Ulid ]},
128136 expected : [][]ulid.ULID {},
137+ metrics : `# HELP cortex_compactor_remaining_planned_compactions Total number of plans that remain to be compacted.
138+ # TYPE cortex_compactor_remaining_planned_compactions gauge
139+ cortex_compactor_remaining_planned_compactions 0
140+ ` ,
129141 },
130142 "test smallest range first" : {
131143 ranges : []time.Duration {2 * time .Hour , 4 * time .Hour },
@@ -135,6 +147,10 @@ func TestShuffleShardingGrouper_Groups(t *testing.T) {
135147 {block3hto4hExt1Ulid , block2hto3hExt1Ulid },
136148 {block4hto6hExt2Ulid , block6hto8hExt2Ulid },
137149 },
150+ metrics : `# HELP cortex_compactor_remaining_planned_compactions Total number of plans that remain to be compacted.
151+ # TYPE cortex_compactor_remaining_planned_compactions gauge
152+ cortex_compactor_remaining_planned_compactions 3
153+ ` ,
138154 },
139155 "test oldest min time first" : {
140156 ranges : []time.Duration {2 * time .Hour , 4 * time .Hour },
@@ -143,25 +159,41 @@ func TestShuffleShardingGrouper_Groups(t *testing.T) {
143159 {block1hto2hExt1Ulid , block0hto1hExt1Ulid , block1hto2hExt1UlidCopy },
144160 {block3hto4hExt1Ulid , block2hto3hExt1Ulid },
145161 },
162+ metrics : `# HELP cortex_compactor_remaining_planned_compactions Total number of plans that remain to be compacted.
163+ # TYPE cortex_compactor_remaining_planned_compactions gauge
164+ cortex_compactor_remaining_planned_compactions 2
165+ ` ,
146166 },
147167 "test overlapping blocks" : {
148168 ranges : []time.Duration {20 * time .Hour , 40 * time .Hour },
149169 blocks : map [ulid.ULID ]* metadata.Meta {block0hto20hExt1Ulid : blocks [block0hto20hExt1Ulid ], block21hto40hExt1Ulid : blocks [block21hto40hExt1Ulid ], block21hto40hExt1UlidCopy : blocks [block21hto40hExt1UlidCopy ]},
150170 expected : [][]ulid.ULID {
151171 {block21hto40hExt1Ulid , block21hto40hExt1UlidCopy },
152172 },
173+ metrics : `# HELP cortex_compactor_remaining_planned_compactions Total number of plans that remain to be compacted.
174+ # TYPE cortex_compactor_remaining_planned_compactions gauge
175+ cortex_compactor_remaining_planned_compactions 1
176+ ` ,
153177 },
154178 "test imperfect maxTime blocks" : {
155179 ranges : []time.Duration {2 * time .Hour },
156180 blocks : map [ulid.ULID ]* metadata.Meta {block0hto1h30mExt1Ulid : blocks [block0hto1h30mExt1Ulid ], block0hto45mExt1Ulid : blocks [block0hto45mExt1Ulid ]},
157181 expected : [][]ulid.ULID {
158182 {block0hto45mExt1Ulid , block0hto1h30mExt1Ulid },
159183 },
184+ metrics : `# HELP cortex_compactor_remaining_planned_compactions Total number of plans that remain to be compacted.
185+ # TYPE cortex_compactor_remaining_planned_compactions gauge
186+ cortex_compactor_remaining_planned_compactions 1
187+ ` ,
160188 },
161189 "test prematurely created blocks" : {
162190 ranges : []time.Duration {2 * time .Hour },
163191 blocks : map [ulid.ULID ]* metadata.Meta {blocklast1hExt1UlidCopy : blocks [blocklast1hExt1UlidCopy ], blocklast1hExt1Ulid : blocks [blocklast1hExt1Ulid ]},
164192 expected : [][]ulid.ULID {},
193+ metrics : `# HELP cortex_compactor_remaining_planned_compactions Total number of plans that remain to be compacted.
194+ # TYPE cortex_compactor_remaining_planned_compactions gauge
195+ cortex_compactor_remaining_planned_compactions 0
196+ ` ,
165197 },
166198 }
167199
@@ -188,7 +220,7 @@ func TestShuffleShardingGrouper_Groups(t *testing.T) {
188220 ring := & RingMock {}
189221 ring .On ("ShuffleShard" , mock .Anything , mock .Anything ).Return (subring , nil )
190222
191- registerer := prometheus .NewRegistry ()
223+ registerer := prometheus .NewPedanticRegistry ()
192224 remainingPlannedCompactions := promauto .With (registerer ).NewGauge (prometheus.GaugeOpts {
193225 Name : "cortex_compactor_remaining_planned_compactions" ,
194226 Help : "Total number of plans that remain to be compacted." ,
@@ -216,6 +248,9 @@ func TestShuffleShardingGrouper_Groups(t *testing.T) {
216248 for idx , expectedIDs := range testData .expected {
217249 assert .Equal (t , expectedIDs , actual [idx ].IDs ())
218250 }
251+
252+ err = testutil .GatherAndCompare (registerer , bytes .NewBufferString (testData .metrics ), "cortex_compactor_remaining_planned_compactions" )
253+ require .NoError (t , err )
219254 })
220255 }
221256}
0 commit comments