github.com/grafana/pyroscope@v1.18.0/pkg/phlaredb/metrics_test.go (about)

     1  package phlaredb
     2  
     3  import (
     4  	"context"
     5  	"strings"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/google/uuid"
    10  	"github.com/prometheus/client_golang/prometheus"
    11  	"github.com/prometheus/client_golang/prometheus/testutil"
    12  	"github.com/stretchr/testify/require"
    13  
    14  	"github.com/grafana/pyroscope/pkg/phlaredb/symdb"
    15  )
    16  
    17  func TestMultipleRegistrationMetrics(t *testing.T) {
    18  	reg := prometheus.NewRegistry()
    19  	m1 := newHeadMetrics(reg)
    20  	m2 := newHeadMetrics(reg)
    21  
    22  	m1.profilesCreated.WithLabelValues("test").Inc()
    23  	m2.profilesCreated.WithLabelValues("test").Inc()
    24  
    25  	// collect metrics and compare them
    26  	require.NoError(t, testutil.GatherAndCompare(reg, strings.NewReader(`
    27  # HELP pyroscope_head_profiles_created_total Total number of profiles created in the head
    28  # TYPE pyroscope_head_profiles_created_total counter
    29  pyroscope_head_profiles_created_total{profile_name="test"} 2
    30  `), "pyroscope_head_profiles_created_total"))
    31  }
    32  
    33  func TestHeadMetrics(t *testing.T) {
    34  	head := newTestHead(t)
    35  	require.NoError(t, head.Ingest(context.Background(), newProfileFoo(), uuid.New(), nil))
    36  	require.NoError(t, head.Ingest(context.Background(), newProfileBar(), uuid.New(), nil))
    37  	require.NoError(t, head.Ingest(context.Background(), newProfileBaz(), uuid.New(), nil))
    38  	head.updateSymbolsMemUsage(new(symdb.MemoryStats))
    39  	time.Sleep(time.Second)
    40  	require.NoError(t, testutil.GatherAndCompare(head.reg,
    41  		strings.NewReader(`
    42  # HELP pyroscope_head_ingested_sample_values_total Number of sample values ingested into the head per profile type.
    43  # TYPE pyroscope_head_ingested_sample_values_total counter
    44  pyroscope_head_ingested_sample_values_total{profile_name=""} 3
    45  # HELP pyroscope_head_profiles_created_total Total number of profiles created in the head
    46  # TYPE pyroscope_head_profiles_created_total counter
    47  pyroscope_head_profiles_created_total{profile_name=""} 2
    48  # HELP pyroscope_head_received_sample_values_total Number of sample values received into the head per profile type.
    49  # TYPE pyroscope_head_received_sample_values_total counter
    50  pyroscope_head_received_sample_values_total{profile_name=""} 3
    51  
    52  # HELP pyroscope_head_size_bytes Size of a particular in memory store within the head phlaredb block.
    53  # TYPE pyroscope_head_size_bytes gauge
    54  pyroscope_head_size_bytes{type="functions"} 96
    55  pyroscope_head_size_bytes{type="locations"} 152
    56  pyroscope_head_size_bytes{type="mappings"} 96
    57  pyroscope_head_size_bytes{type="profiles"} 516
    58  pyroscope_head_size_bytes{type="stacktraces"} 96
    59  pyroscope_head_size_bytes{type="strings"} 66
    60  
    61  `),
    62  		"pyroscope_head_received_sample_values_total",
    63  		"pyroscope_head_profiles_created_total",
    64  		"pyroscope_head_ingested_sample_values_total",
    65  		"pyroscope_head_size_bytes",
    66  	))
    67  }