1
1
package compactor
2
2
3
3
import (
4
+ "bytes"
4
5
"testing"
5
6
"time"
6
7
8
+ "github.com/prometheus/client_golang/prometheus/testutil"
9
+
7
10
"github.com/oklog/ulid"
8
11
"github.com/prometheus/client_golang/prometheus"
9
12
"github.com/prometheus/client_golang/prometheus/promauto"
@@ -112,6 +115,7 @@ func TestShuffleShardingGrouper_Groups(t *testing.T) {
112
115
ranges []time.Duration
113
116
blocks map [ulid.ULID ]* metadata.Meta
114
117
expected [][]ulid.ULID
118
+ metrics string
115
119
}{
116
120
"test basic grouping" : {
117
121
ranges : []time.Duration {2 * time .Hour , 4 * time .Hour },
@@ -121,11 +125,19 @@ func TestShuffleShardingGrouper_Groups(t *testing.T) {
121
125
{block1hto2hExt1Ulid , block0hto1hExt1Ulid },
122
126
{block3hto4hExt1Ulid , block2hto3hExt1Ulid },
123
127
},
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
+ ` ,
124
132
},
125
133
"test no compaction" : {
126
134
ranges : []time.Duration {2 * time .Hour , 4 * time .Hour },
127
135
blocks : map [ulid.ULID ]* metadata.Meta {block0hto1hExt1Ulid : blocks [block0hto1hExt1Ulid ], block0hto1hExt2Ulid : blocks [block0hto1hExt2Ulid ], block0to1hExt3Ulid : blocks [block0to1hExt3Ulid ]},
128
136
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
+ ` ,
129
141
},
130
142
"test smallest range first" : {
131
143
ranges : []time.Duration {2 * time .Hour , 4 * time .Hour },
@@ -135,6 +147,10 @@ func TestShuffleShardingGrouper_Groups(t *testing.T) {
135
147
{block3hto4hExt1Ulid , block2hto3hExt1Ulid },
136
148
{block4hto6hExt2Ulid , block6hto8hExt2Ulid },
137
149
},
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
+ ` ,
138
154
},
139
155
"test oldest min time first" : {
140
156
ranges : []time.Duration {2 * time .Hour , 4 * time .Hour },
@@ -143,25 +159,41 @@ func TestShuffleShardingGrouper_Groups(t *testing.T) {
143
159
{block1hto2hExt1Ulid , block0hto1hExt1Ulid , block1hto2hExt1UlidCopy },
144
160
{block3hto4hExt1Ulid , block2hto3hExt1Ulid },
145
161
},
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
+ ` ,
146
166
},
147
167
"test overlapping blocks" : {
148
168
ranges : []time.Duration {20 * time .Hour , 40 * time .Hour },
149
169
blocks : map [ulid.ULID ]* metadata.Meta {block0hto20hExt1Ulid : blocks [block0hto20hExt1Ulid ], block21hto40hExt1Ulid : blocks [block21hto40hExt1Ulid ], block21hto40hExt1UlidCopy : blocks [block21hto40hExt1UlidCopy ]},
150
170
expected : [][]ulid.ULID {
151
171
{block21hto40hExt1Ulid , block21hto40hExt1UlidCopy },
152
172
},
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
+ ` ,
153
177
},
154
178
"test imperfect maxTime blocks" : {
155
179
ranges : []time.Duration {2 * time .Hour },
156
180
blocks : map [ulid.ULID ]* metadata.Meta {block0hto1h30mExt1Ulid : blocks [block0hto1h30mExt1Ulid ], block0hto45mExt1Ulid : blocks [block0hto45mExt1Ulid ]},
157
181
expected : [][]ulid.ULID {
158
182
{block0hto45mExt1Ulid , block0hto1h30mExt1Ulid },
159
183
},
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
+ ` ,
160
188
},
161
189
"test prematurely created blocks" : {
162
190
ranges : []time.Duration {2 * time .Hour },
163
191
blocks : map [ulid.ULID ]* metadata.Meta {blocklast1hExt1UlidCopy : blocks [blocklast1hExt1UlidCopy ], blocklast1hExt1Ulid : blocks [blocklast1hExt1Ulid ]},
164
192
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
+ ` ,
165
197
},
166
198
}
167
199
@@ -188,7 +220,7 @@ func TestShuffleShardingGrouper_Groups(t *testing.T) {
188
220
ring := & RingMock {}
189
221
ring .On ("ShuffleShard" , mock .Anything , mock .Anything ).Return (subring , nil )
190
222
191
- registerer := prometheus .NewRegistry ()
223
+ registerer := prometheus .NewPedanticRegistry ()
192
224
remainingPlannedCompactions := promauto .With (registerer ).NewGauge (prometheus.GaugeOpts {
193
225
Name : "cortex_compactor_remaining_planned_compactions" ,
194
226
Help : "Total number of plans that remain to be compacted." ,
@@ -216,6 +248,9 @@ func TestShuffleShardingGrouper_Groups(t *testing.T) {
216
248
for idx , expectedIDs := range testData .expected {
217
249
assert .Equal (t , expectedIDs , actual [idx ].IDs ())
218
250
}
251
+
252
+ err = testutil .GatherAndCompare (registerer , bytes .NewBufferString (testData .metrics ), "cortex_compactor_remaining_planned_compactions" )
253
+ require .NoError (t , err )
219
254
})
220
255
}
221
256
}
0 commit comments