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

     1  package compactor
     2  
     3  import (
     4  	"strconv"
     5  	"sync/atomic"
     6  
     7  	"github.com/prometheus/client_golang/prometheus"
     8  )
     9  
    10  type queueStatsCollector struct {
    11  	stats *queueStats
    12  
    13  	blocks   *prometheus.Desc
    14  	batches  *prometheus.Desc
    15  	rejected *prometheus.Desc
    16  	missed   *prometheus.Desc
    17  }
    18  
    19  const blockQueueMetricsPrefix = "compaction_block_queue_"
    20  
    21  func newQueueStatsCollector(staged *stagedBlocks) *queueStatsCollector {
    22  	constLabels := map[string]string{
    23  		"tenant": staged.key.tenant,
    24  		"shard":  strconv.FormatUint(uint64(staged.key.shard), 10),
    25  		"level":  strconv.FormatUint(uint64(staged.key.level), 10),
    26  	}
    27  
    28  	return &queueStatsCollector{
    29  		stats: staged.stats,
    30  
    31  		blocks: prometheus.NewDesc(
    32  			blockQueueMetricsPrefix+"blocks",
    33  			"The total number of blocks in the queue.",
    34  			nil, constLabels,
    35  		),
    36  
    37  		batches: prometheus.NewDesc(
    38  			blockQueueMetricsPrefix+"batches",
    39  			"The total number of block batches in the queue.",
    40  			nil, constLabels,
    41  		),
    42  
    43  		rejected: prometheus.NewDesc(
    44  			blockQueueMetricsPrefix+"push_rejected_total",
    45  			"The total number of blocks rejected on push.",
    46  			nil, constLabels,
    47  		),
    48  
    49  		missed: prometheus.NewDesc(
    50  			blockQueueMetricsPrefix+"delete_missed_total",
    51  			"The total number of blocks missed on delete.",
    52  			nil, constLabels,
    53  		),
    54  	}
    55  }
    56  
    57  func (b *queueStatsCollector) Describe(c chan<- *prometheus.Desc) {
    58  	c <- b.blocks
    59  	c <- b.batches
    60  	c <- b.rejected
    61  	c <- b.missed
    62  }
    63  
    64  func (b *queueStatsCollector) Collect(m chan<- prometheus.Metric) {
    65  	m <- prometheus.MustNewConstMetric(b.blocks, prometheus.GaugeValue, float64(b.stats.blocks.Load()))
    66  	m <- prometheus.MustNewConstMetric(b.batches, prometheus.GaugeValue, float64(b.stats.batches.Load()))
    67  	m <- prometheus.MustNewConstMetric(b.rejected, prometheus.CounterValue, float64(b.stats.rejected.Load()))
    68  	m <- prometheus.MustNewConstMetric(b.missed, prometheus.CounterValue, float64(b.stats.missed.Load()))
    69  }
    70  
    71  type globalQueueStats struct {
    72  	blocksPerLevel  []atomic.Int32
    73  	queuesPerLevel  []atomic.Int32
    74  	batchesPerLevel []atomic.Int32
    75  }
    76  
    77  func newGlobalQueueStats(numLevels int) *globalQueueStats {
    78  	return &globalQueueStats{
    79  		blocksPerLevel:  make([]atomic.Int32, numLevels),
    80  		queuesPerLevel:  make([]atomic.Int32, numLevels),
    81  		batchesPerLevel: make([]atomic.Int32, numLevels),
    82  	}
    83  }
    84  
    85  func (g *globalQueueStats) AddBlocks(key compactionKey, delta int32) {
    86  	g.blocksPerLevel[key.level].Add(delta)
    87  }
    88  
    89  func (g *globalQueueStats) AddQueues(key compactionKey, delta int32) {
    90  	g.queuesPerLevel[key.level].Add(delta)
    91  }
    92  
    93  func (g *globalQueueStats) AddBatches(key compactionKey, delta int32) {
    94  	g.batchesPerLevel[key.level].Add(delta)
    95  }
    96  
    97  type globalQueueStatsCollector struct {
    98  	compactionQueue *compactionQueue
    99  
   100  	blocks  *prometheus.Desc
   101  	queues  *prometheus.Desc
   102  	batches *prometheus.Desc
   103  }
   104  
   105  const globalQueueMetricsPrefix = "compaction_global_queue_"
   106  
   107  func newGlobalQueueStatsCollector(compactionQueue *compactionQueue) *globalQueueStatsCollector {
   108  	variableLabels := []string{"level"}
   109  
   110  	return &globalQueueStatsCollector{
   111  		compactionQueue: compactionQueue,
   112  
   113  		blocks: prometheus.NewDesc(
   114  			globalQueueMetricsPrefix+"blocks_current",
   115  			"The current number of blocks across all queues, for a compaction level.",
   116  			variableLabels, nil,
   117  		),
   118  
   119  		queues: prometheus.NewDesc(
   120  			globalQueueMetricsPrefix+"queues_current",
   121  			"The current number of queues, for a compaction level.",
   122  			variableLabels, nil,
   123  		),
   124  
   125  		batches: prometheus.NewDesc(
   126  			globalQueueMetricsPrefix+"batches_current",
   127  			"The current number of batches (jobs that are not yet created), for a compaction level.",
   128  			variableLabels, nil,
   129  		),
   130  	}
   131  }
   132  
   133  func (c *globalQueueStatsCollector) Describe(ch chan<- *prometheus.Desc) {
   134  	ch <- c.blocks
   135  	ch <- c.queues
   136  	ch <- c.batches
   137  }
   138  
   139  func (c *globalQueueStatsCollector) Collect(ch chan<- prometheus.Metric) {
   140  	for levelIdx := range c.compactionQueue.config.Levels {
   141  		blocksAtLevel := c.compactionQueue.globalStats.blocksPerLevel[levelIdx].Load()
   142  		queuesAtLevel := c.compactionQueue.globalStats.queuesPerLevel[levelIdx].Load()
   143  		batchesAtLevel := c.compactionQueue.globalStats.batchesPerLevel[levelIdx].Load()
   144  
   145  		levelLabel := strconv.Itoa(levelIdx)
   146  
   147  		ch <- prometheus.MustNewConstMetric(c.blocks, prometheus.GaugeValue, float64(blocksAtLevel), levelLabel)
   148  		ch <- prometheus.MustNewConstMetric(c.queues, prometheus.GaugeValue, float64(queuesAtLevel), levelLabel)
   149  		ch <- prometheus.MustNewConstMetric(c.batches, prometheus.GaugeValue, float64(batchesAtLevel), levelLabel)
   150  	}
   151  }