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

     1  package labels
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/prometheus/common/model"
     7  	"github.com/stretchr/testify/require"
     8  
     9  	profilev1 "github.com/grafana/pyroscope/api/gen/proto/go/google/v1"
    10  	phlaremodel "github.com/grafana/pyroscope/pkg/model"
    11  )
    12  
    13  func TestLabelsForProfiles(t *testing.T) {
    14  	for _, tt := range []struct {
    15  		name     string
    16  		in       phlaremodel.Labels
    17  		expected phlaremodel.Labels
    18  	}{
    19  		{
    20  			"default",
    21  			phlaremodel.Labels{{Name: model.MetricNameLabel, Value: "cpu"}},
    22  			phlaremodel.Labels{
    23  				{Name: phlaremodel.LabelNameProfileType, Value: "cpu:type:unit:type:unit"},
    24  				{Name: model.MetricNameLabel, Value: "cpu"},
    25  				{Name: phlaremodel.LabelNamePeriodType, Value: "type"},
    26  				{Name: phlaremodel.LabelNamePeriodUnit, Value: "unit"},
    27  				{Name: phlaremodel.LabelNameType, Value: "type"},
    28  				{Name: phlaremodel.LabelNameUnit, Value: "unit"},
    29  			},
    30  		},
    31  		{
    32  			"with service_name",
    33  			phlaremodel.Labels{
    34  				{Name: model.MetricNameLabel, Value: "cpu"},
    35  				{Name: phlaremodel.LabelNameServiceName, Value: "service_name"},
    36  			},
    37  			phlaremodel.Labels{
    38  				{Name: phlaremodel.LabelNameProfileType, Value: "cpu:type:unit:type:unit"},
    39  				{Name: phlaremodel.LabelNameServiceNamePrivate, Value: "service_name"},
    40  				{Name: model.MetricNameLabel, Value: "cpu"},
    41  				{Name: phlaremodel.LabelNamePeriodType, Value: "type"},
    42  				{Name: phlaremodel.LabelNamePeriodUnit, Value: "unit"},
    43  				{Name: phlaremodel.LabelNameType, Value: "type"},
    44  				{Name: phlaremodel.LabelNameUnit, Value: "unit"},
    45  				{Name: phlaremodel.LabelNameServiceName, Value: "service_name"},
    46  			},
    47  		},
    48  	} {
    49  		tt := tt
    50  		t.Run(tt.name, func(t *testing.T) {
    51  			result, fps := CreateProfileLabels(true, newProfileFoo(), tt.in...)
    52  			require.Equal(t, tt.expected, result[0])
    53  			require.Equal(t, model.Fingerprint(tt.expected.Hash()), fps[0])
    54  		})
    55  	}
    56  }
    57  
    58  func newProfileFoo() *profilev1.Profile {
    59  	return &profilev1.Profile{
    60  		StringTable: append([]string{""}, []string{"unit", "type"}...),
    61  		PeriodType: &profilev1.ValueType{
    62  			Unit: 1,
    63  			Type: 2,
    64  		},
    65  		SampleType: []*profilev1.ValueType{{
    66  			Unit: 1,
    67  			Type: 2,
    68  		}},
    69  	}
    70  }