github.com/grafana/pyroscope@v1.18.0/pkg/distributor/aggregator/aggregator_metrics.go (about)

     1  package aggregator
     2  
     3  import "github.com/prometheus/client_golang/prometheus"
     4  
     5  type aggregatorStatsCollector[T any] struct {
     6  	aggregator *Aggregator[T]
     7  
     8  	activeSeries     *prometheus.Desc
     9  	activeAggregates *prometheus.Desc
    10  	aggregatedTotal  *prometheus.Desc
    11  	errorsTotal      *prometheus.Desc
    12  
    13  	windowDuration *prometheus.Desc
    14  	periodDuration *prometheus.Desc
    15  }
    16  
    17  func NewAggregatorCollector[T any](aggregator *Aggregator[T], prefix string) prometheus.Collector {
    18  	return &aggregatorStatsCollector[T]{
    19  		aggregator:       aggregator,
    20  		activeSeries:     prometheus.NewDesc(prefix+"active_series", "The number of series being aggregated.", nil, nil),
    21  		activeAggregates: prometheus.NewDesc(prefix+"active_aggregates", "The number of active aggregates.", nil, nil),
    22  		aggregatedTotal:  prometheus.NewDesc(prefix+"aggregated_total", "Total number of aggregated requests.", nil, nil),
    23  		errorsTotal:      prometheus.NewDesc(prefix+"errors_total", "Total number of failed aggregations.", nil, nil),
    24  		windowDuration:   prometheus.NewDesc(prefix+"window_duration", "Aggregation window duration.", nil, nil),
    25  		periodDuration:   prometheus.NewDesc(prefix+"period_duration", "Aggregation period duration.", nil, nil),
    26  	}
    27  }
    28  
    29  func (a *aggregatorStatsCollector[T]) Collect(ch chan<- prometheus.Metric) {
    30  	ch <- prometheus.MustNewConstMetric(a.activeSeries, prometheus.GaugeValue, float64(a.aggregator.stats.activeSeries.Load()))
    31  	ch <- prometheus.MustNewConstMetric(a.activeAggregates, prometheus.GaugeValue, float64(a.aggregator.stats.activeAggregates.Load()))
    32  	ch <- prometheus.MustNewConstMetric(a.aggregatedTotal, prometheus.CounterValue, float64(a.aggregator.stats.aggregated.Load()))
    33  	ch <- prometheus.MustNewConstMetric(a.errorsTotal, prometheus.CounterValue, float64(a.aggregator.stats.errors.Load()))
    34  	ch <- prometheus.MustNewConstMetric(a.windowDuration, prometheus.CounterValue, float64(a.aggregator.window))
    35  	ch <- prometheus.MustNewConstMetric(a.periodDuration, prometheus.CounterValue, float64(a.aggregator.period))
    36  }
    37  
    38  func (a *aggregatorStatsCollector[T]) Describe(ch chan<- *prometheus.Desc) {
    39  	prometheus.DescribeByCollect(a, ch)
    40  }
    41  
    42  // RegisterAggregatorCollector registers aggregator metrics collector.
    43  func RegisterAggregatorCollector[T any](aggregator *Aggregator[T], reg prometheus.Registerer) {
    44  	reg.MustRegister(NewAggregatorCollector(aggregator, ""))
    45  }