Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support regex tags search for Elasticseach backend #2049

Merged
merged 8 commits into from
Mar 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions plugin/storage/es/spanstore/fixtures/query_01.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
{
"bool":{
"must":{
"match":{
"regexp":{
"tag.bat@foo":{
"query":"spook"
"value":"spook"
}
}
}
Expand All @@ -15,9 +15,9 @@
{
"bool":{
"must":{
"match":{
"regexp":{
"process.tag.bat@foo":{
"query":"spook"
"value":"spook"
}
}
}
Expand All @@ -37,9 +37,9 @@
}
},
{
"match":{
"regexp":{
"tags.value":{
"query":"spook"
"value":"spook"
}
}
}
Expand All @@ -62,9 +62,9 @@
}
},
{
"match":{
"regexp":{
"process.tags.value":{
"query":"spook"
"value":"spook"
}
}
}
Expand All @@ -87,9 +87,9 @@
}
},
{
"match":{
"regexp":{
"logs.fields.value":{
"query":"spook"
"value":"spook"
}
}
}
Expand Down
103 changes: 103 additions & 0 deletions plugin/storage/es/spanstore/fixtures/query_02.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
{
"bool":{
"should":[
{
"bool":{
"must":{
"regexp":{
"tag.bat@foo":{
"value":"spo.*"
}
}
}
}
},
{
"bool":{
"must":{
"regexp":{
"process.tag.bat@foo":{
"value":"spo.*"
}
}
}
}
},
{
"nested":{
"path":"tags",
"query":{
"bool":{
"must":[
{
"match":{
"tags.key":{
"query":"bat.foo"
}
}
},
{
"regexp":{
"tags.value":{
"value":"spo.*"
}
}
}
]
}
}
}
},
{
"nested":{
"path":"process.tags",
"query":{
"bool":{
"must":[
{
"match":{
"process.tags.key":{
"query":"bat.foo"
}
}
},
{
"regexp":{
"process.tags.value":{
"value":"spo.*"
}
}
}
]
}
}
}
},
{
"nested":{
"path":"logs.fields",
"query":{
"bool":{
"must":[
{
"match":{
"logs.fields.key":{
"query":"bat.foo"
}
}
},
{
"regexp":{
"logs.fields.value":{
"value":"spo.*"
}
}
}
]
}
}
}
}
]
}
}
103 changes: 103 additions & 0 deletions plugin/storage/es/spanstore/fixtures/query_03.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
{
"bool":{
"should":[
{
"bool":{
"must":{
"regexp":{
"tag.bat@foo":{
"value":"spo\\*"
}
}
}
}
},
{
"bool":{
"must":{
"regexp":{
"process.tag.bat@foo":{
"value":"spo\\*"
}
}
}
}
},
{
"nested":{
"path":"tags",
"query":{
"bool":{
"must":[
{
"match":{
"tags.key":{
"query":"bat.foo"
}
}
},
{
"regexp":{
"tags.value":{
"value":"spo\\*"
}
}
}
]
}
}
}
},
{
"nested":{
"path":"process.tags",
"query":{
"bool":{
"must":[
{
"match":{
"process.tags.key":{
"query":"bat.foo"
}
}
},
{
"regexp":{
"process.tags.value":{
"value":"spo\\*"
}
}
}
]
}
}
}
},
{
"nested":{
"path":"logs.fields",
"query":{
"bool":{
"must":[
{
"match":{
"logs.fields.key":{
"query":"bat.foo"
}
}
},
{
"regexp":{
"logs.fields.value":{
"value":"spo\\*"
}
}
}
]
}
}
}
}
]
}
}
4 changes: 2 additions & 2 deletions plugin/storage/es/spanstore/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,14 +622,14 @@ func (s *SpanReader) buildNestedQuery(field string, k string, v string) elastic.
keyField := fmt.Sprintf("%s.%s", field, tagKeyField)
valueField := fmt.Sprintf("%s.%s", field, tagValueField)
keyQuery := elastic.NewMatchQuery(keyField, k)
valueQuery := elastic.NewMatchQuery(valueField, v)
valueQuery := elastic.NewRegexpQuery(valueField, v)
tagBoolQuery := elastic.NewBoolQuery().Must(keyQuery, valueQuery)
return elastic.NewNestedQuery(field, tagBoolQuery)
}

func (s *SpanReader) buildObjectQuery(field string, k string, v string) elastic.Query {
keyField := fmt.Sprintf("%s.%s", field, k)
keyQuery := elastic.NewMatchQuery(keyField, v)
keyQuery := elastic.NewRegexpQuery(keyField, v)
return elastic.NewBoolQuery().Must(keyQuery)
}

Expand Down
30 changes: 30 additions & 0 deletions plugin/storage/es/spanstore/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,36 @@ func TestSpanReader_buildTagQuery(t *testing.T) {
})
}

func TestSpanReader_buildTagRegexQuery(t *testing.T) {
inStr, err := ioutil.ReadFile("fixtures/query_02.json")
require.NoError(t, err)
withSpanReader(func(r *spanReaderTest) {
tagQuery := r.reader.buildTagQuery("bat.foo", "spo.*")
actual, err := tagQuery.Source()
require.NoError(t, err)

expected := make(map[string]interface{})
json.Unmarshal(inStr, &expected)

assert.EqualValues(t, expected, actual)
})
}

func TestSpanReader_buildTagRegexEscapedQuery(t *testing.T) {
inStr, err := ioutil.ReadFile("fixtures/query_03.json")
require.NoError(t, err)
withSpanReader(func(r *spanReaderTest) {
tagQuery := r.reader.buildTagQuery("bat.foo", "spo\\*")
actual, err := tagQuery.Source()
require.NoError(t, err)

expected := make(map[string]interface{})
json.Unmarshal(inStr, &expected)

assert.EqualValues(t, expected, actual)
})
}

func TestSpanReader_GetEmptyIndex(t *testing.T) {
withSpanReader(func(r *spanReaderTest) {
mockSearchService(r).
Expand Down
2 changes: 2 additions & 0 deletions plugin/storage/integration/elasticsearch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ func testElasticsearchStorage(t *testing.T, allTagsAsFields, archive bool) {
s := &ESStorageIntegration{}
require.NoError(t, s.initializeES(allTagsAsFields, archive))

s.Fixtures = loadAndParseQueryTestCases(t, "fixtures/queries_es.json")

if archive {
t.Run("ArchiveTrace", s.testArchiveTrace)
} else {
Expand Down
34 changes: 34 additions & 0 deletions plugin/storage/integration/fixtures/queries_es.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[
{
"Caption": "Tag escaped operator + Operation name + max Duration",
"Query": {
"ServiceName": "query23-service",
"OperationName": "query23-operation",
"Tags": {
"sameplacetag1":"same\\*"
},
"StartTimeMin": "2017-01-26T15:46:31.639875Z",
"StartTimeMax": "2017-01-26T17:46:31.639875Z",
"DurationMin": 0,
"DurationMax": 1000,
"NumTraces": 1000
},
"ExpectedFixtures": ["tags_escaped_operator_trace_1"]
},
{
"Caption": "Tag wildcard regex",
"Query": {
"ServiceName": "query24-service",
"OperationName": "",
"Tags": {
"sameplacetag1":"same.*"
},
"StartTimeMin": "2017-01-26T15:46:31.639875Z",
"StartTimeMax": "2017-01-26T17:46:31.639875Z",
"DurationMin": 0,
"DurationMax": 0,
"NumTraces": 1000
},
"ExpectedFixtures": ["tags_wildcard_regex_1", "tags_wildcard_regex_2"]
}
]
Loading