Skip to content

Commit

Permalink
Fix test asserts
Browse files Browse the repository at this point in the history
Have a function that can emulate the old GatherAndCompare

Follow up to prometheus/client_golang#1424

Signed-off-by: György Krajcsovits <gyorgy.krajcsovits@grafana.com>
  • Loading branch information
krajorama committed Aug 30, 2024
1 parent bf9048f commit 3186aa9
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 72 deletions.
7 changes: 1 addition & 6 deletions pkg/compactor/blocks_cleaner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,12 +464,7 @@ func TestBlocksCleaner_ShouldNotCleanupUserThatDoesntBelongToShardAnymore(t *tes
require.ElementsMatch(t, []string{"user-1", "user-2"}, cleaner.lastOwnedUsers)

// But there are no metrics for any user, because we did not in fact clean them.
assert.NoError(t, testutil.GatherAndCompare(reg, strings.NewReader(`
# HELP cortex_bucket_blocks_count Total number of blocks in the bucket. Includes blocks marked for deletion, but not partial blocks.
# TYPE cortex_bucket_blocks_count gauge
`),
"cortex_bucket_blocks_count",
))
test.AssertGatherAndCompare(t, reg, "", "cortex_bucket_blocks_count")

// Running cleanUsers again will see that users are no longer owned.
require.NoError(t, cleaner.runCleanupWithErr(ctx))
Expand Down
4 changes: 2 additions & 2 deletions pkg/compactor/compactor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -967,7 +967,7 @@ func TestMultitenantCompactor_ShouldNotCompactBlocksForUsersMarkedForDeletion(t
"cortex_compactor_block_cleanup_started_total", "cortex_compactor_block_cleanup_completed_total", "cortex_compactor_block_cleanup_failed_total",
"cortex_bucket_blocks_count", "cortex_bucket_blocks_marked_for_deletion_count", "cortex_bucket_index_last_successful_update_timestamp_seconds",
}
assert.NoError(t, prom_testutil.GatherAndCompare(registry, strings.NewReader(`
testutil.AssertGatherAndCompare(t, registry, `
# TYPE cortex_compactor_runs_started_total counter
# HELP cortex_compactor_runs_started_total Total number of compaction runs started.
cortex_compactor_runs_started_total 1
Expand Down Expand Up @@ -1006,7 +1006,7 @@ func TestMultitenantCompactor_ShouldNotCompactBlocksForUsersMarkedForDeletion(t
# TYPE cortex_compactor_block_cleanup_failed_total counter
# HELP cortex_compactor_block_cleanup_failed_total Total number of blocks cleanup runs failed.
cortex_compactor_block_cleanup_failed_total 0
`), testedMetrics...))
`, testedMetrics...)
}

func TestMultitenantCompactor_ShouldCompactAllUsersOnShardingEnabledButOnlyOneInstanceRunning(t *testing.T) {
Expand Down
22 changes: 11 additions & 11 deletions pkg/distributor/distributor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ func TestDistributor_MetricsCleanup(t *testing.T) {
d.dedupedSamples.WithLabelValues("userA", "cluster1").Inc() // We cannot clean this metric
d.latestSeenSampleTimestampPerUser.WithLabelValues("userA").Set(1111)

require.NoError(t, testutil.GatherAndCompare(reg, strings.NewReader(`
util_test.AssertGatherAndCompare(t, reg, `
# HELP cortex_distributor_deduped_samples_total The total number of deduplicated samples.
# TYPE cortex_distributor_deduped_samples_total counter
cortex_distributor_deduped_samples_total{cluster="cluster1",user="userA"} 1
Expand Down Expand Up @@ -414,11 +414,11 @@ func TestDistributor_MetricsCleanup(t *testing.T) {
# HELP cortex_distributor_exemplars_in_total The total number of exemplars that have come in to the distributor, including rejected or deduped exemplars.
# TYPE cortex_distributor_exemplars_in_total counter
cortex_distributor_exemplars_in_total{user="userA"} 5
`), metrics...))
`, metrics...)

d.cleanupInactiveUser("userA")

require.NoError(t, testutil.GatherAndCompare(reg, strings.NewReader(`
util_test.AssertGatherAndCompare(t, reg, `
# HELP cortex_distributor_deduped_samples_total The total number of deduplicated samples.
# TYPE cortex_distributor_deduped_samples_total counter
Expand Down Expand Up @@ -448,7 +448,7 @@ func TestDistributor_MetricsCleanup(t *testing.T) {
# HELP cortex_distributor_exemplars_in_total The total number of exemplars that have come in to the distributor, including rejected or deduped exemplars.
# TYPE cortex_distributor_exemplars_in_total counter
`), metrics...))
`, metrics...)
}

func TestDistributor_PushRequestRateLimiter(t *testing.T) {
Expand Down Expand Up @@ -1678,7 +1678,7 @@ func TestDistributor_ExemplarValidation(t *testing.T) {
}

assert.Equal(t, tc.expectedExemplars, tc.req.Timeseries)
assert.NoError(t, testutil.GatherAndCompare(regs[0], strings.NewReader(tc.expectedMetrics), "cortex_discarded_exemplars_total"))
util_test.AssertGatherAndCompare(t, regs[0], tc.expectedMetrics, "cortex_discarded_exemplars_total")
})
}
}
Expand Down Expand Up @@ -7181,15 +7181,15 @@ func TestDistributor_StorageConfigMetrics(t *testing.T) {
happyIngesters: 3,
replicationFactor: 3,
})
assert.NoError(t, testutil.GatherAndCompare(regs[0], strings.NewReader(`
util_test.AssertGatherAndCompare(t, regs[0], `
# HELP cortex_distributor_replication_factor The configured replication factor.
# TYPE cortex_distributor_replication_factor gauge
cortex_distributor_replication_factor 3
# HELP cortex_distributor_ingest_storage_enabled Whether writes are being processed via ingest storage. Equal to 1 if ingest storage is enabled, 0 if disabled.
# TYPE cortex_distributor_ingest_storage_enabled gauge
cortex_distributor_ingest_storage_enabled 0
`), "cortex_distributor_replication_factor", "cortex_distributor_ingest_storage_enabled"))
`, "cortex_distributor_replication_factor", "cortex_distributor_ingest_storage_enabled")
})

t.Run("migration to ingest storage", func(t *testing.T) {
Expand All @@ -7202,15 +7202,15 @@ func TestDistributor_StorageConfigMetrics(t *testing.T) {
happyIngesters: 3,
replicationFactor: 3,
})
assert.NoError(t, testutil.GatherAndCompare(regs[0], strings.NewReader(`
util_test.AssertGatherAndCompare(t, regs[0], `
# HELP cortex_distributor_replication_factor The configured replication factor.
# TYPE cortex_distributor_replication_factor gauge
cortex_distributor_replication_factor 3
# HELP cortex_distributor_ingest_storage_enabled Whether writes are being processed via ingest storage. Equal to 1 if ingest storage is enabled, 0 if disabled.
# TYPE cortex_distributor_ingest_storage_enabled gauge
cortex_distributor_ingest_storage_enabled 1
`), "cortex_distributor_replication_factor", "cortex_distributor_ingest_storage_enabled"))
`, "cortex_distributor_replication_factor", "cortex_distributor_ingest_storage_enabled")
})

t.Run("ingest storage", func(t *testing.T) {
Expand All @@ -7222,11 +7222,11 @@ func TestDistributor_StorageConfigMetrics(t *testing.T) {
happyIngesters: 3,
replicationFactor: 3,
})
assert.NoError(t, testutil.GatherAndCompare(regs[0], strings.NewReader(`
util_test.AssertGatherAndCompare(t, regs[0], `
# HELP cortex_distributor_ingest_storage_enabled Whether writes are being processed via ingest storage. Equal to 1 if ingest storage is enabled, 0 if disabled.
# TYPE cortex_distributor_ingest_storage_enabled gauge
cortex_distributor_ingest_storage_enabled 1
`), "cortex_distributor_replication_factor", "cortex_distributor_ingest_storage_enabled"))
`, "cortex_distributor_ingest_storage_enabled")
})
}

Expand Down
6 changes: 2 additions & 4 deletions pkg/frontend/v1/frontend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
"github.com/grafana/mimir/pkg/querier/stats"
querier_worker "github.com/grafana/mimir/pkg/querier/worker"
"github.com/grafana/mimir/pkg/scheduler/queue"
util_test "github.com/grafana/mimir/pkg/util/test"
)

const (
Expand Down Expand Up @@ -222,10 +223,7 @@ func TestFrontendMetricsCleanup(t *testing.T) {

fr.cleanupInactiveUserMetrics("1")

require.NoError(t, testutil.GatherAndCompare(reg, strings.NewReader(`
# HELP cortex_query_frontend_queue_length Number of queries in the queue.
# TYPE cortex_query_frontend_queue_length gauge
`), "cortex_query_frontend_queue_length"))
util_test.AssertGatherAndCompare(t, reg, "", "cortex_query_frontend_queue_length")
}

testFrontend(t, defaultFrontendConfig(), handler, test, nil, reg)
Expand Down
Loading

0 comments on commit 3186aa9

Please sign in to comment.