From 5232c0433879097b1e04223d6a98eecfa451a7b4 Mon Sep 17 00:00:00 2001 From: Annanay Date: Mon, 3 Feb 2020 17:14:36 +0530 Subject: [PATCH] Add query builder and integration tests Signed-off-by: Annanay --- .../es/spanstore/fixtures/query_02.json | 103 ++++++++++++++++++ .../es/spanstore/fixtures/query_03.json | 103 ++++++++++++++++++ plugin/storage/es/spanstore/reader_test.go | 30 +++++ .../storage/integration/fixtures/queries.json | 32 ++++++ .../fixtures/traces/tags_regex_trace.json | 33 ++++++ 5 files changed, 301 insertions(+) create mode 100644 plugin/storage/es/spanstore/fixtures/query_02.json create mode 100644 plugin/storage/es/spanstore/fixtures/query_03.json create mode 100644 plugin/storage/integration/fixtures/traces/tags_regex_trace.json diff --git a/plugin/storage/es/spanstore/fixtures/query_02.json b/plugin/storage/es/spanstore/fixtures/query_02.json new file mode 100644 index 000000000000..60d6516aef17 --- /dev/null +++ b/plugin/storage/es/spanstore/fixtures/query_02.json @@ -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.*" + } + } + } + ] + } + } + } + } + ] + } +} diff --git a/plugin/storage/es/spanstore/fixtures/query_03.json b/plugin/storage/es/spanstore/fixtures/query_03.json new file mode 100644 index 000000000000..05da6e4a0b62 --- /dev/null +++ b/plugin/storage/es/spanstore/fixtures/query_03.json @@ -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\\*" + } + } + } + ] + } + } + } + } + ] + } +} diff --git a/plugin/storage/es/spanstore/reader_test.go b/plugin/storage/es/spanstore/reader_test.go index 62f7e63eb587..b2431dd3a316 100644 --- a/plugin/storage/es/spanstore/reader_test.go +++ b/plugin/storage/es/spanstore/reader_test.go @@ -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). diff --git a/plugin/storage/integration/fixtures/queries.json b/plugin/storage/integration/fixtures/queries.json index 357073413793..e8fa4d271a64 100644 --- a/plugin/storage/integration/fixtures/queries.json +++ b/plugin/storage/integration/fixtures/queries.json @@ -376,5 +376,37 @@ "NumTraces": 1000 }, "ExpectedFixtures": ["multiple1_trace", "multiple2_trace", "multiple3_trace"] + }, + { + "Caption": "Tag regex + Operation name + max Duration", + "Query": { + "ServiceName": "query23-service", + "OperationName": "query23-operation", + "Tags": { + "sameplacetag1":"random\\*" + }, + "StartTimeMin": "2017-01-26T15:46:31.639875Z", + "StartTimeMax": "2017-01-26T17:46:31.639875Z", + "DurationMin": 0, + "DurationMax": 1000, + "NumTraces": 1000 + }, + "ExpectedFixtures": ["tags_regex_trace"] + }, + { + "Caption": "Tag regex + Operation name + max Duration - Multiple traces", + "Query": { + "ServiceName": "query23-service", + "OperationName": "", + "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_opname_trace", "tags_regex_trace"] } ] diff --git a/plugin/storage/integration/fixtures/traces/tags_regex_trace.json b/plugin/storage/integration/fixtures/traces/tags_regex_trace.json new file mode 100644 index 000000000000..d4699a736dc8 --- /dev/null +++ b/plugin/storage/integration/fixtures/traces/tags_regex_trace.json @@ -0,0 +1,33 @@ +{ + "spans": [ + { + "traceId": "AAAAAAAAAAAAAAAAAAAAEh==", + "spanId": "AAAAAAAAAAU=", + "operationName": "query23-operation", + "references": [], + "startTime": "2017-01-26T16:46:31.639875Z", + "duration": "1000ns", + "tags": [ + { + "key": "sameplacetag1", + "vType": "STRING", + "vStr": "random*" + } + ], + "process": { + "serviceName": "query23-service", + "tags": [] + }, + "logs": [ + { + "timestamp": "2017-01-26T16:46:31.639875Z", + "fields": [] + }, + { + "timestamp": "2017-01-26T16:46:31.639875Z", + "fields": [] + } + ] + } + ] +}