From 30424a861857e19ee86d28dbdb698f7c6ae6e31f Mon Sep 17 00:00:00 2001 From: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com> Date: Fri, 6 Jan 2023 11:14:17 +0100 Subject: [PATCH] Elasticsearch: Return error if invalid query (#61044) * Elasticsearch: Return error if invalid query * Add test * Fix query in test --- .../api/elasticsearch/elasticsearch_test.go | 5 +++++ pkg/tsdb/elasticsearch/time_series_query.go | 13 +++---------- .../elasticsearch/time_series_query_test.go | 17 +++++++++++++++-- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/pkg/tests/api/elasticsearch/elasticsearch_test.go b/pkg/tests/api/elasticsearch/elasticsearch_test.go index 7d46ca2c8a3..9c138f1ac5f 100644 --- a/pkg/tests/api/elasticsearch/elasticsearch_test.go +++ b/pkg/tests/api/elasticsearch/elasticsearch_test.go @@ -77,6 +77,11 @@ func TestIntegrationElasticsearch(t *testing.T) { "rawQuery": "*", "type": "", "timeField": "@timestamp", + "metrics": []interface{}{ + map[string]interface{}{ + "type": "logs", + }, + }, }) buf1 := &bytes.Buffer{} err = json.NewEncoder(buf1).Encode(dtos.MetricRequest{ diff --git a/pkg/tsdb/elasticsearch/time_series_query.go b/pkg/tsdb/elasticsearch/time_series_query.go index f41db468917..dc3a62bb3fc 100644 --- a/pkg/tsdb/elasticsearch/time_series_query.go +++ b/pkg/tsdb/elasticsearch/time_series_query.go @@ -37,11 +37,8 @@ func (e *timeSeriesQuery) execute() (*backend.QueryDataResponse, error) { from := e.dataQueries[0].TimeRange.From.UnixNano() / int64(time.Millisecond) to := e.dataQueries[0].TimeRange.To.UnixNano() / int64(time.Millisecond) - result := backend.QueryDataResponse{ - Responses: backend.Responses{}, - } for _, q := range queries { - if err := e.processQuery(q, ms, from, to, result); err != nil { + if err := e.processQuery(q, ms, from, to); err != nil { return &backend.QueryDataResponse{}, err } } @@ -59,8 +56,7 @@ func (e *timeSeriesQuery) execute() (*backend.QueryDataResponse, error) { return parseResponse(res.Responses, queries) } -func (e *timeSeriesQuery) processQuery(q *Query, ms *es.MultiSearchRequestBuilder, from, to int64, - result backend.QueryDataResponse) error { +func (e *timeSeriesQuery) processQuery(q *Query, ms *es.MultiSearchRequestBuilder, from, to int64) error { defaultTimeField := e.client.GetTimeField() b := ms.Search(q.Interval) @@ -72,10 +68,7 @@ func (e *timeSeriesQuery) processQuery(q *Query, ms *es.MultiSearchRequestBuilde if len(q.BucketAggs) == 0 { // If no aggregations, only document and logs queries are valid if len(q.Metrics) == 0 || !(q.Metrics[0].Type == rawDataType || q.Metrics[0].Type == rawDocumentType || q.Metrics[0].Type == logsType) { - result.Responses[q.RefID] = backend.DataResponse{ - Error: fmt.Errorf("invalid query, missing metrics and aggregations"), - } - return nil + return fmt.Errorf("invalid query, missing metrics and aggregations") } // Defaults for log and document queries diff --git a/pkg/tsdb/elasticsearch/time_series_query_test.go b/pkg/tsdb/elasticsearch/time_series_query_test.go index fe9cd710c4b..527a7c7c119 100644 --- a/pkg/tsdb/elasticsearch/time_series_query_test.go +++ b/pkg/tsdb/elasticsearch/time_series_query_test.go @@ -1316,7 +1316,9 @@ func TestExecuteTimeSeriesQuery(t *testing.T) { c := newFakeClient() _, err := executeTsdbQuery(c, `{ "timeField": "@timestamp", - "query": "foo" + "query": "foo", + "bucketAggs": [], + "metrics": [{ "id": "1", "type": "raw_data", "settings": {} }] }`, from, to) require.NoError(t, err) sr := c.multisearchRequests[0].Requests[0] @@ -1329,7 +1331,9 @@ func TestExecuteTimeSeriesQuery(t *testing.T) { c := newFakeClient() _, err := executeTsdbQuery(c, `{ "timeField": "@timestamp", - "query": "foo" + "query": "foo", + "bucketAggs": [], + "metrics": [{ "id": "1", "type": "raw_data", "settings": {} }] }`, from, to) require.NoError(t, err) sr := c.multisearchRequests[0].Requests[0] @@ -1399,6 +1403,15 @@ func TestExecuteTimeSeriesQuery(t *testing.T) { "pre_tags": []string{"@HIGHLIGHT@"}, }) }) + + t.Run("With invalid query should return error", (func(t *testing.T) { + c := newFakeClient() + _, err := executeTsdbQuery(c, `{ + "timeField": "@timestamp", + "query": "foo", + }`, from, to) + require.Error(t, err) + })) }) }