github.com/grafana/pyroscope@v1.18.0/pkg/metastore/compaction/compactor/metrics_test.go (about)

     1  package compactor
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/prometheus/client_golang/prometheus"
     7  
     8  	"github.com/grafana/pyroscope/pkg/metastore/compaction"
     9  )
    10  
    11  func TestCollectorRegistration(t *testing.T) {
    12  	reg := prometheus.NewRegistry()
    13  	for i := 0; i < 2; i++ {
    14  		entries := []compaction.BlockEntry{
    15  			{Tenant: "A", Shard: 0, Level: 0},
    16  			{Tenant: "A", Shard: 0, Level: 1},
    17  			{Tenant: "A", Shard: 0, Level: 1},
    18  			{Tenant: "A", Shard: 1, Level: 0},
    19  			{Tenant: "B", Shard: 0, Level: 0},
    20  		}
    21  		c := NewCompactor(testConfig, nil, nil, reg)
    22  		for _, e := range entries {
    23  			c.enqueue(e)
    24  		}
    25  		c.queue.reset()
    26  		for _, e := range entries {
    27  			c.enqueue(e)
    28  		}
    29  	}
    30  }
    31  
    32  func TestBlockQueueAggregatedMetrics(t *testing.T) {
    33  	reg := prometheus.NewRegistry()
    34  	c := NewCompactor(testConfig, nil, nil, reg)
    35  
    36  	entries := []compaction.BlockEntry{
    37  		{ID: "block1", Tenant: "A", Shard: 0, Level: 0},
    38  		{ID: "block2", Tenant: "A", Shard: 0, Level: 0},
    39  		{ID: "block3", Tenant: "A", Shard: 0, Level: 0},
    40  		{ID: "block4", Tenant: "A", Shard: 1, Level: 0},
    41  		{ID: "block5", Tenant: "B", Shard: 0, Level: 1},
    42  		{ID: "block6", Tenant: "B", Shard: 0, Level: 1},
    43  		{ID: "block7", Tenant: "B", Shard: 0, Level: 1},
    44  	}
    45  
    46  	for _, e := range entries {
    47  		c.enqueue(e)
    48  	}
    49  
    50  	metrics, err := reg.Gather()
    51  	if err != nil {
    52  		t.Fatalf("failed to gather metrics: %v", err)
    53  	}
    54  
    55  	var blocksTotal, queuesTotal, batchesTotal float64
    56  	var foundBlocks, foundQueues, foundBatches bool
    57  
    58  	for _, mf := range metrics {
    59  		if mf.GetName() == "compaction_global_queue_blocks_current" {
    60  			for _, m := range mf.GetMetric() {
    61  				blocksTotal += m.GetGauge().GetValue()
    62  				foundBlocks = true
    63  			}
    64  		}
    65  		if mf.GetName() == "compaction_global_queue_queues_current" {
    66  			for _, m := range mf.GetMetric() {
    67  				queuesTotal += m.GetGauge().GetValue()
    68  				foundQueues = true
    69  			}
    70  		}
    71  
    72  		if mf.GetName() == "compaction_global_queue_batches_current" {
    73  			for _, m := range mf.GetMetric() {
    74  				batchesTotal += m.GetGauge().GetValue()
    75  				foundBatches = true
    76  			}
    77  		}
    78  	}
    79  
    80  	if !foundBlocks {
    81  		t.Fatal("compaction_global_queue_blocks metric not found")
    82  	}
    83  	if !foundQueues {
    84  		t.Fatal("compaction_global_queue_queues metric not found")
    85  	}
    86  	if !foundBatches {
    87  		t.Fatal("compaction_global_queue_batches_current metric not found")
    88  	}
    89  
    90  	if blocksTotal != 7 {
    91  		t.Errorf("expected 7 total blocks, got %v", blocksTotal)
    92  	}
    93  
    94  	if queuesTotal != 3 {
    95  		t.Errorf("expected 3 total queues, got %v", queuesTotal)
    96  	}
    97  
    98  	// testConfig.Levels[0].MaxBlocks = 3
    99  	// testConfig.Levels[1].MaxBlocks = 2
   100  	// (A,0): 3 blocks → 3/3 = 1 batch
   101  	// (A,1): 1 block → 1/2 = 0 batches
   102  	// (B,1): 3 blocks → 3/2 = 1 batch
   103  	// Total = 2 batches
   104  	if batchesTotal != 2 {
   105  		t.Errorf("expected 2 total batches, got %v", batchesTotal)
   106  	}
   107  }