Skip to content

Commit

Permalink
Merge branch 'main' into feat_datalinks
Browse files Browse the repository at this point in the history
  • Loading branch information
idrissneumann committed Dec 9, 2023
2 parents 1dc167b + 28ebe43 commit 05e8fc6
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 28 deletions.
15 changes: 11 additions & 4 deletions pkg/quickwit/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,17 @@ func (c *baseClientImpl) ExecuteMultisearch(r *MultiSearchRequest) (*MultiSearch
c.logger.Debug("Received multisearch response", "code", res.StatusCode, "status", res.Status, "content-length", res.ContentLength)

if res.StatusCode >= 400 {
jsonResponseBody, _ := json.Marshal(res.Body)
jsonQueryParam, _ := json.Marshal(queryParams)
jsonRequestBody, _ := json.Marshal(r.Requests)
c.logger.Error("Error on multisearch: statusCode = " + strconv.Itoa(res.StatusCode) + ", responseBody = " + string(jsonResponseBody) + ", queryParam = " + string(jsonQueryParam) + ", requestBody = " + string(jsonRequestBody))
qe := QuickwitQueryError{
Status: res.StatusCode,
Message: "Error on multisearch",
ResponseBody: res.Body,
QueryParam: queryParams,
RequestBody: r.Requests,
}

errorPayload, _ := json.Marshal(qe)
c.logger.Error(string(errorPayload))
return nil, fmt.Errorf(string(errorPayload))
}

start := time.Now()
Expand Down
9 changes: 9 additions & 0 deletions pkg/quickwit/client/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package es

import (
"encoding/json"
"io"
"time"
)

Expand Down Expand Up @@ -43,6 +44,14 @@ type SearchResponseHits struct {
Hits []map[string]interface{}
}

type QuickwitQueryError struct {
Status int `json:"status"`
Message string `json:"message"`
ResponseBody io.ReadCloser `json:"response_body"`
RequestBody []*SearchRequest `json:"request_body"`
QueryParam string `json:"query_param"`
}

// SearchResponse represents a search response
type SearchResponse struct {
Error map[string]interface{} `json:"error"`
Expand Down
31 changes: 25 additions & 6 deletions pkg/quickwit/data_query.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package quickwit

import (
"encoding/json"
"fmt"
"regexp"
"strconv"
"time"

"github.com/grafana/grafana-plugin-sdk-go/backend"

es "github.com/quickwit-oss/quickwit-datasource/pkg/quickwit/client"
"github.com/quickwit-oss/quickwit-datasource/pkg/quickwit/simplejson"
)
Expand All @@ -28,6 +28,26 @@ var newElasticsearchDataQuery = func(client es.Client, dataQuery []backend.DataQ
}
}

func handleQuickwitErrors(e *elasticsearchDataQuery, err error) (*backend.QueryDataResponse, error) {
if nil == err {
return nil, nil
}

var payload = err.Error()
var qe es.QuickwitQueryError
unmarshall_err := json.Unmarshal([]byte(payload), &qe)
if unmarshall_err == nil {
return nil, err
}

result := backend.QueryDataResponse{
Responses: backend.Responses{},
}

result.Responses[e.dataQueries[0].RefID] = backend.ErrDataResponse(backend.Status(qe.Status), payload)
return &result, nil
}

func (e *elasticsearchDataQuery) execute() (*backend.QueryDataResponse, error) {
queries, err := parseQuery(e.dataQueries)
if err != nil {
Expand All @@ -50,14 +70,13 @@ func (e *elasticsearchDataQuery) execute() (*backend.QueryDataResponse, error) {
}

res, err := e.client.ExecuteMultisearch(req)
if err != nil {
result, err := handleQuickwitErrors(e, err)
if result != nil {
return result, nil
} else if err != nil {
return &backend.QueryDataResponse{}, err
}

if res.Status == 404 {
return &backend.QueryDataResponse{}, fmt.Errorf("/_msearch endpoint not found, please check your Quickwit version")
}

return parseResponse(res.Responses, queries, e.client.GetConfiguredFields())
}

Expand Down
25 changes: 10 additions & 15 deletions pkg/quickwit/error_handling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,10 @@ func TestErrorAvgMissingField(t *testing.T) {
LogMessageField: "line",
LogLevelField: "lvl",
}
result, err := queryDataTestWithResponseCode(query, 400, response, configuredFields)
require.NoError(t, err)

// FIXME: we should return the received error message
require.Len(t, result.response.Responses, 0)
result, err := queryDataTestWithResponseCode(query, 400, response, configuredFields)
require.Nil(t, err)
require.Contains(t, result.response.Responses["A"].Error.Error(), "\"status\":400")
}

func TestErrorAvgMissingFieldNoDetailedErrors(t *testing.T) {
Expand Down Expand Up @@ -78,11 +77,10 @@ func TestErrorAvgMissingFieldNoDetailedErrors(t *testing.T) {
LogMessageField: "line",
LogLevelField: "lvl",
}
result, err := queryDataTestWithResponseCode(query, 400, response, configuredFields)
require.NoError(t, err)

// FIXME: we should return the received error message
require.Len(t, result.response.Responses, 0)
result, err := queryDataTestWithResponseCode(query, 400, response, configuredFields)
require.Nil(t, err)
require.Contains(t, result.response.Responses["A"].Error.Error(), "\"status\":400")
}

func TestErrorTooManyDateHistogramBuckets(t *testing.T) {
Expand Down Expand Up @@ -164,11 +162,8 @@ func TestNonElasticError(t *testing.T) {
LogMessageField: "line",
LogLevelField: "lvl",
}
_, err := queryDataTestWithResponseCode(query, 403, response, configuredFields)
// FIXME: we should return something better.
// currently it returns the error-message about being unable to decode JSON
// it is not 100% clear what we should return to the browser
// (and what to debug-log for example), we could return
// at least something like "unknown response, http status code 403"
require.ErrorContains(t, err, "invalid character")

result, err := queryDataTestWithResponseCode(query, 403, response, configuredFields)
require.Nil(t, err)
require.Contains(t, result.response.Responses["A"].Error.Error(), "\"status\":403")
}
5 changes: 2 additions & 3 deletions src/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,7 @@ export class QuickwitDataSource
catchError((err) => {
if (err.status === 404) {
return of({ status: 'error', message: 'Index does not exists.' });
}
else if (err.message) {
} else if (err.message) {
return of({ status: 'error', message: err.message });
} else {
return of({ status: 'error', message: err.status });
Expand Down Expand Up @@ -492,7 +491,7 @@ export class QuickwitDataSource
const error: DataQueryError = {
message: 'Error during context query. Please check JS console logs.',
status: err.status,
statusText: err.statusText,
statusText: err.message,
};
throw error;
})
Expand Down

0 comments on commit 05e8fc6

Please sign in to comment.