mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Elasticsearch: Improve backend instrumentation of CallResource
calls (#74530)
Elasticsearch: Improve backend instrumentation of calls
This commit is contained in:
parent
51391a762b
commit
408d0265ce
@ -12,6 +12,7 @@ import (
|
|||||||
"path"
|
"path"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||||
"github.com/grafana/grafana-plugin-sdk-go/backend/datasource"
|
"github.com/grafana/grafana-plugin-sdk-go/backend/datasource"
|
||||||
@ -189,37 +190,49 @@ func (s *Service) CallResource(ctx context.Context, req *backend.CallResourceReq
|
|||||||
// - ?/_mapping for fetching index mapping
|
// - ?/_mapping for fetching index mapping
|
||||||
// - _msearch for executing getTerms queries
|
// - _msearch for executing getTerms queries
|
||||||
if req.Path != "" && !strings.HasSuffix(req.Path, "/_mapping") && req.Path != "_msearch" {
|
if req.Path != "" && !strings.HasSuffix(req.Path, "/_mapping") && req.Path != "_msearch" {
|
||||||
|
logger.Error("Invalid resource path", "path", req.Path)
|
||||||
return fmt.Errorf("invalid resource URL: %s", req.Path)
|
return fmt.Errorf("invalid resource URL: %s", req.Path)
|
||||||
}
|
}
|
||||||
|
|
||||||
ds, err := s.getDSInfo(ctx, req.PluginContext)
|
ds, err := s.getDSInfo(ctx, req.PluginContext)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
logger.Error("Failed to get data source info", "error", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
esUrl, err := url.Parse(ds.URL)
|
esUrl, err := url.Parse(ds.URL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
logger.Error("Failed to parse data source URL", "error", err, "url", ds.URL)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
resourcePath, err := url.Parse(req.Path)
|
resourcePath, err := url.Parse(req.Path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
logger.Error("Failed to parse data source path", "error", err, "url", req.Path)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// We take the path and the query-string only
|
// We take the path and the query-string only
|
||||||
esUrl.RawQuery = resourcePath.RawQuery
|
esUrl.RawQuery = resourcePath.RawQuery
|
||||||
esUrl.Path = path.Join(esUrl.Path, resourcePath.Path)
|
esUrl.Path = path.Join(esUrl.Path, resourcePath.Path)
|
||||||
|
|
||||||
request, err := http.NewRequestWithContext(ctx, req.Method, esUrl.String(), bytes.NewBuffer(req.Body))
|
request, err := http.NewRequestWithContext(ctx, req.Method, esUrl.String(), bytes.NewBuffer(req.Body))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
logger.Error("Failed to create request", "error", err, "url", esUrl.String())
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logger.Debug("Sending request to Elasticsearch", "resourcePath", req.Path)
|
||||||
|
start := time.Now()
|
||||||
response, err := ds.HTTPClient.Do(request)
|
response, err := ds.HTTPClient.Do(request)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
status := "error"
|
||||||
|
if errors.Is(err, context.Canceled) {
|
||||||
|
status = "cancelled"
|
||||||
|
}
|
||||||
|
logger.Error("Error received from Elasticsearch", "error", err, "status", status, "statusCode", response.StatusCode, "duration", time.Since(start), "action", "databaseRequest")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
logger.Info("Response received from Elasticsearch", "statusCode", response.StatusCode, "status", "ok", "duration", time.Since(start), "action", "databaseRequest", "contentLength", response.Header.Get("Content-Length"))
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
if err := response.Body.Close(); err != nil {
|
if err := response.Body.Close(); err != nil {
|
||||||
@ -229,6 +242,7 @@ func (s *Service) CallResource(ctx context.Context, req *backend.CallResourceReq
|
|||||||
|
|
||||||
body, err := io.ReadAll(response.Body)
|
body, err := io.ReadAll(response.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
logger.Error("Error reading response body bytes", "error", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user