Skip to content

Commit

Permalink
Test query trace (#533)
Browse files Browse the repository at this point in the history
Signed-off-by: Gao Hongtao <[email protected]>
  • Loading branch information
hanahmily authored Sep 11, 2024
1 parent 36ef1c6 commit 58422e3
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 31 deletions.
4 changes: 2 additions & 2 deletions pkg/index/inverted/inverted_series.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ func (s *store) BuildQuery(seriesMatchers []index.SeriesMatcher, secondaryQuery
q := bluge.NewPrefixQuery(match)
q.SetField(entityField)
qs[i] = q
primaryNode.Append(newPrefixNode(match, nil))
primaryNode.Append(newPrefixNode(match))
case index.SeriesMatcherTypeWildcard:
match := convert.BytesToString(seriesMatchers[i].Match)
q := bluge.NewWildcardQuery(match)
q.SetField(entityField)
qs[i] = q
primaryNode.Append(newWildcardNode(match, nil))
primaryNode.Append(newWildcardNode(match))
default:
return nil, errors.Errorf("unsupported series matcher type: %v", seriesMatchers[i].Type)
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/index/inverted/inverted_series_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ func TestStore_Search(t *testing.T) {
}
t.Run(name, func(t *testing.T) {
query, err := s.BuildQuery(matchers, nil)
require.NotEmpty(t, query.String())
require.NoError(t, err)
got, err := s.Search(context.Background(), tt.projection, query)
require.NoError(t, err)
Expand Down Expand Up @@ -282,6 +283,7 @@ func TestStore_SearchWildcard(t *testing.T) {
},
}, nil)
require.NoError(t, err)
require.NotEmpty(t, query.String())
got, err := s.Search(context.Background(), tt.projection, query)
require.NoError(t, err)
assert.ElementsMatch(t, tt.want, got)
Expand Down Expand Up @@ -349,6 +351,7 @@ func TestStore_SearchPrefix(t *testing.T) {
},
}, nil)
require.NoError(t, err)
require.NotEmpty(t, query.String())
got, err := s.Search(context.Background(), tt.projection, query)
require.NoError(t, err)
assert.ElementsMatch(t, tt.want, got)
Expand Down
34 changes: 14 additions & 20 deletions pkg/index/inverted/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,9 @@ func (t *termRangeInclusiveNode) MarshalJSON() ([]byte, error) {
builder.WriteString(")")
}
inner["range"] = builder.String()
inner["index"] = t.indexRule.Metadata.Name + ":" + t.indexRule.Metadata.Group
if t.indexRule != nil {
inner["index"] = t.indexRule.Metadata.Name + ":" + t.indexRule.Metadata.Group
}
data := make(map[string]interface{}, 1)
data["termRangeInclusive"] = inner
return json.Marshal(data)
Expand All @@ -385,7 +387,9 @@ func newTermNode(term string, indexRule *databasev1.IndexRule) *termNode {

func (t *termNode) MarshalJSON() ([]byte, error) {
inner := make(map[string]interface{}, 1)
inner["index"] = t.indexRule.Metadata.Name + ":" + t.indexRule.Metadata.Group
if t.indexRule != nil {
inner["index"] = t.indexRule.Metadata.Name + ":" + t.indexRule.Metadata.Group
}
inner["value"] = t.term
data := make(map[string]interface{}, 1)
data["term"] = inner
Expand Down Expand Up @@ -423,23 +427,18 @@ func (m *matchNode) String() string {
}

type prefixNode struct {
indexRule *databasev1.IndexRule
prefix string
prefix string
}

func newPrefixNode(prefix string, indexRule *databasev1.IndexRule) *prefixNode {
func newPrefixNode(prefix string) *prefixNode {
return &prefixNode{
indexRule: indexRule,
prefix: prefix,
prefix: prefix,
}
}

func (m *prefixNode) MarshalJSON() ([]byte, error) {
inner := make(map[string]interface{}, 1)
inner["index"] = m.indexRule.Metadata.Name + ":" + m.indexRule.Metadata.Group
inner["value"] = m.prefix
data := make(map[string]interface{}, 1)
data["prefix"] = inner
data["prefix"] = m.prefix
return json.Marshal(data)
}

Expand All @@ -448,23 +447,18 @@ func (m *prefixNode) String() string {
}

type wildcardNode struct {
indexRule *databasev1.IndexRule
wildcard string
wildcard string
}

func newWildcardNode(wildcard string, indexRule *databasev1.IndexRule) *wildcardNode {
func newWildcardNode(wildcard string) *wildcardNode {
return &wildcardNode{
indexRule: indexRule,
wildcard: wildcard,
wildcard: wildcard,
}
}

func (m *wildcardNode) MarshalJSON() ([]byte, error) {
inner := make(map[string]interface{}, 1)
inner["index"] = m.indexRule.Metadata.Name + ":" + m.indexRule.Metadata.Group
inner["value"] = m.wildcard
data := make(map[string]interface{}, 1)
data["wildcard"] = inner
data["wildcard"] = m.wildcard
return json.Marshal(data)
}

Expand Down
16 changes: 13 additions & 3 deletions test/cases/measure/data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,23 +81,33 @@ var VerifyFn = func(innerGm gm.Gomega, sharedContext helpers.SharedContext, args
innerGm.Expect(resp.DataPoints[i].Sid).Should(gm.BeNumerically(">", 0))
}
}
innerGm.Expect(cmp.Equal(resp, want,
success := innerGm.Expect(cmp.Equal(resp, want,
protocmp.IgnoreUnknown(),
protocmp.IgnoreFields(&measurev1.DataPoint{}, "timestamp"),
protocmp.IgnoreFields(&measurev1.DataPoint{}, "version"),
protocmp.IgnoreFields(&measurev1.DataPoint{}, "sid"),
protocmp.Transform())).
To(gm.BeTrue(), func() string {
j, err := protojson.Marshal(resp)
var j []byte
j, err = protojson.Marshal(resp)
if err != nil {
return err.Error()
}
y, err := yaml.JSONToYAML(j)
var y []byte
y, err = yaml.JSONToYAML(j)
if err != nil {
return err.Error()
}
return string(y)
})
if !success {
return
}
query.Trace = true
resp, err = c.Query(ctx, query)
innerGm.Expect(err).NotTo(gm.HaveOccurred())
innerGm.Expect(resp.Trace).NotTo(gm.BeNil())
innerGm.Expect(resp.Trace.GetSpans()).NotTo(gm.BeEmpty())
}

//go:embed testdata/*.json
Expand Down
16 changes: 13 additions & 3 deletions test/cases/stream/data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,21 +97,31 @@ var VerifyFn = func(innerGm gm.Gomega, sharedContext helpers.SharedContext, args
return strings.Compare(a.ElementId, b.ElementId)
})
}
innerGm.Expect(cmp.Equal(resp, want,
success := innerGm.Expect(cmp.Equal(resp, want,
protocmp.IgnoreUnknown(),
protocmp.IgnoreFields(&streamv1.Element{}, "timestamp"),
protocmp.Transform())).
To(gm.BeTrue(), func() string {
j, err := protojson.Marshal(resp)
var j []byte
j, err = protojson.Marshal(resp)
if err != nil {
return err.Error()
}
y, err := yaml.JSONToYAML(j)
var y []byte
y, err = yaml.JSONToYAML(j)
if err != nil {
return err.Error()
}
return string(y)
})
if !success {
return
}
query.Trace = true
resp, err = c.Query(ctx, query)
innerGm.Expect(err).NotTo(gm.HaveOccurred())
innerGm.Expect(resp.Trace).NotTo(gm.BeNil())
innerGm.Expect(resp.Trace.GetSpans()).NotTo(gm.BeEmpty())
}

func loadData(stream streamv1.StreamService_WriteClient, metadata *commonv1.Metadata, dataFile string, baseTime time.Time, interval time.Duration) {
Expand Down
16 changes: 13 additions & 3 deletions test/cases/topn/data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,29 @@ var VerifyFn = func(innerGm gm.Gomega, sharedContext helpers.SharedContext, args
innerGm.Expect(err).NotTo(gm.HaveOccurred())
want := &measurev1.TopNResponse{}
helpers.UnmarshalYAML(ww, want)
innerGm.Expect(cmp.Equal(resp, want,
success := innerGm.Expect(cmp.Equal(resp, want,
protocmp.IgnoreUnknown(),
protocmp.IgnoreFields(&measurev1.TopNList{}, "timestamp"),
protocmp.Transform())).
To(gm.BeTrue(), func() string {
j, err := protojson.Marshal(resp)
var j []byte
j, err = protojson.Marshal(resp)
if err != nil {
return err.Error()
}
y, err := yaml.JSONToYAML(j)
var y []byte
y, err = yaml.JSONToYAML(j)
if err != nil {
return err.Error()
}
return string(y)
})
if !success {
return
}
query.Trace = true
resp, err = c.TopN(ctx, query)
innerGm.Expect(err).NotTo(gm.HaveOccurred())
innerGm.Expect(resp.Trace).NotTo(gm.BeNil())
innerGm.Expect(resp.Trace.GetSpans()).NotTo(gm.BeEmpty())
}

0 comments on commit 58422e3

Please sign in to comment.