Elasticsearch and Cloudwatch: Replace deprecated experimental/errorsource (#97363)

* Update elasticsearch

* Update azure monitor

* Update test

* Revert "Update azure monitor"

This reverts commit 21b53845b85c3a43d679d729c33879fdc3e9f027.

* Update cloudwatch
This commit is contained in:
Ivana Huckova
2024-12-12 10:12:03 +01:00
committed by GitHub
parent ded90fa28d
commit 7de65b39b5
12 changed files with 47 additions and 49 deletions

View File

@ -11,7 +11,6 @@ import (
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
"github.com/grafana/grafana-plugin-sdk-go/experimental/errorsource"
"github.com/grafana/grafana/pkg/components/simplejson"
es "github.com/grafana/grafana/pkg/tsdb/elasticsearch/client"
@ -52,7 +51,8 @@ func (e *elasticsearchDataQuery) execute() (*backend.QueryDataResponse, error) {
if err != nil {
mq, _ := json.Marshal(e.dataQueries)
e.logger.Error("Failed to parse queries", "error", err, "queries", string(mq), "queriesLength", len(queries), "duration", time.Since(start), "stage", es.StagePrepareRequest)
return errorsource.AddPluginErrorToResponse(e.dataQueries[0].RefID, response, err), nil
response.Responses[e.dataQueries[0].RefID] = backend.ErrorResponseWithErrorSource(err)
return response, nil
}
ms := e.client.MultiSearch()
@ -63,7 +63,8 @@ func (e *elasticsearchDataQuery) execute() (*backend.QueryDataResponse, error) {
if err := e.processQuery(q, ms, from, to); err != nil {
mq, _ := json.Marshal(q)
e.logger.Error("Failed to process query to multisearch request builder", "error", err, "query", string(mq), "queriesLength", len(queries), "duration", time.Since(start), "stage", es.StagePrepareRequest)
return errorsource.AddPluginErrorToResponse(q.RefID, response, err), nil
response.Responses[q.RefID] = backend.ErrorResponseWithErrorSource(err)
return response, nil
}
}
@ -71,21 +72,28 @@ func (e *elasticsearchDataQuery) execute() (*backend.QueryDataResponse, error) {
if err != nil {
mqs, _ := json.Marshal(e.dataQueries)
e.logger.Error("Failed to build multisearch request", "error", err, "queriesLength", len(queries), "queries", string(mqs), "duration", time.Since(start), "stage", es.StagePrepareRequest)
return errorsource.AddPluginErrorToResponse(e.dataQueries[0].RefID, response, err), nil
response.Responses[e.dataQueries[0].RefID] = backend.ErrorResponseWithErrorSource(err)
return response, nil
}
e.logger.Info("Prepared request", "queriesLength", len(queries), "duration", time.Since(start), "stage", es.StagePrepareRequest)
res, err := e.client.ExecuteMultisearch(req)
if err != nil {
if backend.IsDownstreamHTTPError(err) {
err = errorsource.DownstreamError(err, false)
err = backend.DownstreamError(err)
}
return errorsource.AddErrorToResponse(e.dataQueries[0].RefID, response, err), nil
response.Responses[e.dataQueries[0].RefID] = backend.ErrorResponseWithErrorSource(err)
return response, 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
statusErr := fmt.Errorf("unexpected status code: %d", res.Status)
if backend.ErrorSourceFromHTTPStatus(res.Status) == backend.ErrorSourceDownstream {
response.Responses[e.dataQueries[0].RefID] = backend.ErrorResponseWithErrorSource(backend.DownstreamError(statusErr))
} else {
response.Responses[e.dataQueries[0].RefID] = backend.ErrorResponseWithErrorSource(backend.PluginError(statusErr))
}
return response, nil
}
return parseResponse(e.ctx, res.Responses, queries, e.client.GetConfiguredFields(), e.keepLabelsInResponse, e.logger)
@ -94,8 +102,7 @@ func (e *elasticsearchDataQuery) execute() (*backend.QueryDataResponse, error) {
func (e *elasticsearchDataQuery) processQuery(q *Query, ms *es.MultiSearchRequestBuilder, from, to int64) error {
err := isQueryWithError(q)
if err != nil {
err = errorsource.DownstreamError(fmt.Errorf("received invalid query. %w", err), false)
return err
return backend.DownstreamError(fmt.Errorf("received invalid query. %w", err))
}
defaultTimeField := e.client.GetConfiguredFields().TimeField