From 3caa374f2fc9c2a4de315dfc423d6b6481cbce31 Mon Sep 17 00:00:00 2001 From: Anton Kolesnikov Date: Mon, 9 Dec 2024 19:41:25 +0800 Subject: [PATCH] test and fix metadata label querier --- pkg/experiment/metastore/index/index.go | 2 +- pkg/experiment/metastore/index/query.go | 8 +- pkg/experiment/metastore/index/query_test.go | 92 ++++++++++++-------- 3 files changed, 61 insertions(+), 41 deletions(-) diff --git a/pkg/experiment/metastore/index/index.go b/pkg/experiment/metastore/index/index.go index a1ba5d928e..0fd4d4e8b9 100644 --- a/pkg/experiment/metastore/index/index.go +++ b/pkg/experiment/metastore/index/index.go @@ -454,7 +454,7 @@ func (i *Index) QueryMetadata(tx *bbolt.Tx, query MetadataQuery) iter.Iterator[* } func (i *Index) QueryMetadataLabels(tx *bbolt.Tx, query MetadataLabelQuery) ([]*typesv1.Labels, error) { - q, err := newMetadataQuery(i, query.MetadataQuery) + q, err := newMetadataQuery(i, query.MetadataQuery, query.Labels...) if err != nil { return nil, err } diff --git a/pkg/experiment/metastore/index/query.go b/pkg/experiment/metastore/index/query.go index d1fd3b0fa9..634b89bd3c 100644 --- a/pkg/experiment/metastore/index/query.go +++ b/pkg/experiment/metastore/index/query.go @@ -225,7 +225,7 @@ type metadataLabelQuerier struct { func (mi *metadataLabelQuerier) queryLabels() (*model.LabelMerger, error) { if len(mi.query.labels) == 0 { - return nil, nil + return mi.labels, nil } for mi.shards.Next() { mi.collectLabels(mi.shards.At()) @@ -237,7 +237,11 @@ func (mi *metadataLabelQuerier) queryLabels() (*model.LabelMerger, error) { } func (mi *metadataLabelQuerier) collectLabels(shard *indexShard) { - m := block.NewLabelMatcher(shard.StringTable, mi.query.matchers) + m := block.NewLabelMatcher( + shard.StringTable, + mi.query.matchers, + mi.query.labels..., + ) if !m.IsValid() { return } diff --git a/pkg/experiment/metastore/index/query_test.go b/pkg/experiment/metastore/index/query_test.go index f389aef319..0f41a7ef2e 100644 --- a/pkg/experiment/metastore/index/query_test.go +++ b/pkg/experiment/metastore/index/query_test.go @@ -4,11 +4,14 @@ import ( "testing" "time" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.etcd.io/bbolt" metastorev1 "github.com/grafana/pyroscope/api/gen/proto/go/metastore/v1" + typesv1 "github.com/grafana/pyroscope/api/gen/proto/go/types/v1" "github.com/grafana/pyroscope/pkg/iter" + "github.com/grafana/pyroscope/pkg/model" "github.com/grafana/pyroscope/pkg/test" "github.com/grafana/pyroscope/pkg/util" ) @@ -26,12 +29,12 @@ func TestIndex_Query(t *testing.T) { MaxTime: maxT, CreatedBy: 1, Datasets: []*metastorev1.Dataset{ - {Tenant: 2, Name: 3, ProfileTypes: []int32{4, 5, 6}, MinTime: minT, MaxTime: minT}, - {Tenant: 7, Name: 8, ProfileTypes: []int32{5, 6, 9}, MinTime: maxT, MaxTime: maxT}, + {Tenant: 2, Name: 3, MinTime: minT, MaxTime: minT, Labels: []int32{2, 4, 3, 5, 6}}, + {Tenant: 7, Name: 8, MinTime: maxT, MaxTime: maxT, Labels: []int32{2, 4, 8, 5, 9}}, }, StringTable: []string{ "", "ingester", - "tenant-a", "dataset-a", "1", "2", "3", + "tenant-a", "dataset-a", "service_name", "__profile_type__", "1", "tenant-b", "dataset-b", "4", }, } @@ -44,11 +47,10 @@ func TestIndex_Query(t *testing.T) { MaxTime: maxT, CreatedBy: 2, Datasets: []*metastorev1.Dataset{ - {Tenant: 1, Name: 3, ProfileTypes: []int32{4, 5, 6}, MinTime: minT, MaxTime: minT}, + {Tenant: 1, Name: 3, MinTime: minT, MaxTime: minT, Labels: []int32{2, 4, 3, 5, 6}}, }, StringTable: []string{ - "", "tenant-a", "ingester", - "dataset-a", "1", "2", "3", + "", "tenant-a", "ingester", "dataset-a", "service_name", "__profile_type__", "1", }, } @@ -60,11 +62,10 @@ func TestIndex_Query(t *testing.T) { MaxTime: maxT, CreatedBy: 2, Datasets: []*metastorev1.Dataset{ - {Tenant: 1, Name: 3, ProfileTypes: []int32{4, 5, 6}, MinTime: minT, MaxTime: minT}, + {Tenant: 1, Name: 3, MinTime: minT, MaxTime: minT, Labels: []int32{2, 4, 3, 5, 6}}, }, StringTable: []string{ - "", "tenant-a", "ingester", - "dataset-a", "1", "2", "3", + "", "tenant-a", "ingester", "dataset-a", "service_name", "__profile_type__", "1", }, } @@ -104,39 +105,33 @@ func TestIndex_Query(t *testing.T) { t.Run("DatasetFilter", func(t *testing.T) { expected := []*metastorev1.BlockMeta{ { - Id: md.Id, - Tenant: 0, - MinTime: minT, - MaxTime: maxT, - CreatedBy: 1, - Datasets: []*metastorev1.Dataset{ - {Tenant: 2, Name: 3, ProfileTypes: []int32{4, 5, 6}, MinTime: minT, MaxTime: minT}, - }, - StringTable: []string{"", "ingester", "tenant-a", "dataset-a", "1", "2", "3"}, + Id: md.Id, + Tenant: 0, + MinTime: minT, + MaxTime: maxT, + CreatedBy: 1, + Datasets: []*metastorev1.Dataset{{Tenant: 2, Name: 3, MinTime: minT, MaxTime: minT}}, + StringTable: []string{"", "ingester", "tenant-a", "dataset-a"}, }, { - Id: md2.Id, - Tenant: 1, - Shard: 1, - MinTime: minT, - MaxTime: maxT, - CreatedBy: 2, - Datasets: []*metastorev1.Dataset{ - {Tenant: 1, Name: 3, ProfileTypes: []int32{4, 5, 6}, MinTime: minT, MaxTime: minT}, - }, - StringTable: []string{"", "tenant-a", "ingester", "dataset-a", "1", "2", "3"}, + Id: md2.Id, + Tenant: 1, + Shard: 1, + MinTime: minT, + MaxTime: maxT, + CreatedBy: 2, + Datasets: []*metastorev1.Dataset{{Tenant: 1, Name: 3, MinTime: minT, MaxTime: minT}}, + StringTable: []string{"", "tenant-a", "ingester", "dataset-a"}, }, { - Id: md3.Id, - Tenant: 1, - Shard: 1, - MinTime: minT, - MaxTime: maxT, - CreatedBy: 2, - Datasets: []*metastorev1.Dataset{ - {Tenant: 1, Name: 3, ProfileTypes: []int32{4, 5, 6}, MinTime: minT, MaxTime: minT}, - }, - StringTable: []string{"", "tenant-a", "ingester", "dataset-a", "1", "2", "3"}, + Id: md3.Id, + Tenant: 1, + Shard: 1, + MinTime: minT, + MaxTime: maxT, + CreatedBy: 2, + Datasets: []*metastorev1.Dataset{{Tenant: 1, Name: 3, MinTime: minT, MaxTime: minT}}, + StringTable: []string{"", "tenant-a", "ingester", "dataset-a"}, }, } @@ -160,6 +155,27 @@ func TestIndex_Query(t *testing.T) { require.NoError(t, err) require.Empty(t, found) }) + + t.Run("Labels", func(t *testing.T) { + labels, err := index.QueryMetadataLabels(tx, MetadataLabelQuery{ + Labels: []string{ + model.LabelNameProfileType, + model.LabelNameServiceName, + }, + MetadataQuery: MetadataQuery{ + Expr: `{service_name=~"dataset.*"}`, + StartTime: time.UnixMilli(minT), + EndTime: time.UnixMilli(maxT), + Tenant: []string{"tenant-a"}, + }, + }) + require.NoError(t, err) + require.NotEmpty(t, labels) + assert.Equal(t, []*typesv1.Labels{{Labels: []*typesv1.LabelPair{ + {Name: model.LabelNameProfileType, Value: "1"}, + {Name: model.LabelNameServiceName, Value: "dataset-a"}, + }}}, labels) + }) } idx := NewIndex(util.Logger, NewStore(), &DefaultConfig)