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

     1  package aggregator
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/prometheus/client_golang/prometheus"
     9  	"github.com/prometheus/client_golang/prometheus/testutil"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func Test_Aggregation_Metrics(t *testing.T) {
    14  	w := time.Second * 15
    15  	d := time.Millisecond * 10
    16  
    17  	fn := func(i int) (int, error) {
    18  		return i + 1, nil
    19  	}
    20  
    21  	a := NewAggregator[int](w, d)
    22  	var start int64
    23  	a.now = func() int64 {
    24  		start += 10
    25  		return start
    26  	}
    27  
    28  	registry := prometheus.NewRegistry()
    29  	RegisterAggregatorCollector(a, registry)
    30  
    31  	_, _, _ = a.Aggregate(0, 0, fn)
    32  	r2, _, _ := a.Aggregate(0, 1, fn)
    33  	r3, _, _ := a.Aggregate(0, 2, fn)
    34  
    35  	assert.NoError(t, r2.Wait())
    36  	v, ok := r2.Value()
    37  	assert.Equal(t, 2, v)
    38  	assert.True(t, ok)
    39  	r2.Close(nil)
    40  
    41  	assert.NoError(t, r3.Wait())
    42  	_, ok = r3.Value()
    43  	assert.False(t, ok)
    44  	r3.Close(nil)
    45  
    46  	a.prune(0)
    47  	// Create a new aggregate.
    48  	_, _, _ = a.Aggregate(0, 0, fn)
    49  
    50  	expected := `
    51  # HELP active_aggregates The number of active aggregates.
    52  # TYPE active_aggregates gauge
    53  active_aggregates 1
    54  # HELP active_series The number of series being aggregated.
    55  # TYPE active_series gauge
    56  active_series 1
    57  # HELP aggregated_total Total number of aggregated requests.
    58  # TYPE aggregated_total counter
    59  aggregated_total 3
    60  # HELP errors_total Total number of failed aggregations.
    61  # TYPE errors_total counter
    62  errors_total 0
    63  # HELP period_duration Aggregation period duration.
    64  # TYPE period_duration counter
    65  period_duration 1e+07
    66  # HELP window_duration Aggregation window duration.
    67  # TYPE window_duration counter
    68  window_duration 1.5e+10
    69  `
    70  	assert.NoError(t, testutil.GatherAndCompare(registry, bytes.NewBufferString(expected)))
    71  }