From d00c4fd35a1a0e02d55510c7f85eada5619d995e Mon Sep 17 00:00:00 2001 From: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com> Date: Mon, 18 Nov 2024 14:39:21 +0100 Subject: [PATCH] Elasticsearch: Replace error source http client with a new error source methods (#96570) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Elasticsearch: Replace error source http client with methods * Update * Update * Update tests * Update * Update * Update pkg/tsdb/elasticsearch/data_query.go Co-authored-by: Nathan Vērzemnieks * Trigger build --------- Co-authored-by: Nathan Vērzemnieks --- pkg/tsdb/elasticsearch/data_query.go | 9 ++++++++- pkg/tsdb/elasticsearch/elasticsearch.go | 6 +----- pkg/tsdb/elasticsearch/error_handling_test.go | 6 ++++-- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/pkg/tsdb/elasticsearch/data_query.go b/pkg/tsdb/elasticsearch/data_query.go index 405d3b87cb6..4fb4eaaa9f1 100644 --- a/pkg/tsdb/elasticsearch/data_query.go +++ b/pkg/tsdb/elasticsearch/data_query.go @@ -77,10 +77,17 @@ func (e *elasticsearchDataQuery) execute() (*backend.QueryDataResponse, error) { e.logger.Info("Prepared request", "queriesLength", len(queries), "duration", time.Since(start), "stage", es.StagePrepareRequest) res, err := e.client.ExecuteMultisearch(req) if err != nil { - // We are returning error containing the source that was added through errorsource.Middleware + if backend.IsDownstreamHTTPError(err) { + err = errorsource.DownstreamError(err, false) + } return errorsource.AddErrorToResponse(e.dataQueries[0].RefID, response, err), nil } + if res.Status >= 400 { + errWithSource := errorsource.SourceError(backend.ErrorSourceFromHTTPStatus(res.Status), fmt.Errorf("unexpected status code: %d", res.Status), false) + return errorsource.AddErrorToResponse(e.dataQueries[0].RefID, response, errWithSource), nil + } + return parseResponse(e.ctx, res.Responses, queries, e.client.GetConfiguredFields(), e.keepLabelsInResponse, e.logger) } diff --git a/pkg/tsdb/elasticsearch/elasticsearch.go b/pkg/tsdb/elasticsearch/elasticsearch.go index 2b7a43725ba..f48d1d64889 100644 --- a/pkg/tsdb/elasticsearch/elasticsearch.go +++ b/pkg/tsdb/elasticsearch/elasticsearch.go @@ -20,7 +20,6 @@ import ( "github.com/grafana/grafana-plugin-sdk-go/backend/instancemgmt" "github.com/grafana/grafana-plugin-sdk-go/backend/log" exp "github.com/grafana/grafana-plugin-sdk-go/experimental/errorsource" - exphttpclient "github.com/grafana/grafana-plugin-sdk-go/experimental/errorsource/httpclient" es "github.com/grafana/grafana/pkg/tsdb/elasticsearch/client" ) @@ -90,10 +89,7 @@ func newInstanceSettings(httpClientProvider *httpclient.Provider) datasource.Ins httpCliOpts.SigV4.Service = "es" } - // set the default middlewars from the httpClientProvider - httpCliOpts.Middlewares = httpClientProvider.Opts.Middlewares - // enable experimental http client to support errors with source - httpCli, err := exphttpclient.New(httpCliOpts) + httpCli, err := httpClientProvider.New(httpCliOpts) if err != nil { return nil, err } diff --git a/pkg/tsdb/elasticsearch/error_handling_test.go b/pkg/tsdb/elasticsearch/error_handling_test.go index 30cc6c0c43c..c6407a7e35f 100644 --- a/pkg/tsdb/elasticsearch/error_handling_test.go +++ b/pkg/tsdb/elasticsearch/error_handling_test.go @@ -4,6 +4,7 @@ import ( "errors" "testing" + "github.com/grafana/grafana-plugin-sdk-go/backend" "github.com/grafana/grafana-plugin-sdk-go/experimental/errorsource" "github.com/stretchr/testify/require" ) @@ -43,7 +44,8 @@ func TestErrorAvgMissingField(t *testing.T) { require.NoError(t, err) // FIXME: we should return the received error message - require.Len(t, result.response.Responses, 0) + require.Equal(t, "unexpected status code: 400", result.response.Responses["A"].Error.Error()) + require.Equal(t, backend.ErrorSourceDownstream, result.response.Responses["A"].ErrorSource) } func TestErrorAvgMissingFieldNoDetailedErrors(t *testing.T) { @@ -71,7 +73,7 @@ func TestErrorAvgMissingFieldNoDetailedErrors(t *testing.T) { require.NoError(t, err) // FIXME: we should return the received error message - require.Len(t, result.response.Responses, 0) + require.Equal(t, "unexpected status code: 400", result.response.Responses["A"].Error.Error()) } func TestErrorTooManyDateHistogramBuckets(t *testing.T) {