mirror of
https://github.com/grafana/grafana.git
synced 2024-11-22 08:56:43 -06:00
AzureMonitor: Improve errorsource (#93533)
* Improve errorsource * Fix imports
This commit is contained in:
parent
76c8975b4f
commit
564ee32b04
@ -6,7 +6,6 @@ import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
@ -26,6 +25,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/tsdb/azuremonitor/kinds/dataquery"
|
||||
"github.com/grafana/grafana/pkg/tsdb/azuremonitor/macros"
|
||||
"github.com/grafana/grafana/pkg/tsdb/azuremonitor/types"
|
||||
"github.com/grafana/grafana/pkg/tsdb/azuremonitor/utils"
|
||||
)
|
||||
|
||||
func (e *AzureLogAnalyticsDatasource) ResourceRequest(rw http.ResponseWriter, req *http.Request, cli *http.Client) (http.ResponseWriter, error) {
|
||||
@ -243,11 +243,7 @@ func (e *AzureLogAnalyticsDatasource) buildQuery(ctx context.Context, query back
|
||||
azureLogAnalyticsQuery, err = buildLogAnalyticsQuery(query, dsInfo, appInsightsRegExp, fromAlert)
|
||||
if err != nil {
|
||||
errorMessage := fmt.Errorf("failed to build azure log analytics query: %w", err)
|
||||
var sourceError errorsource.Error
|
||||
if errors.As(err, &sourceError) {
|
||||
return nil, errorsource.SourceError(sourceError.Source(), errorMessage, false)
|
||||
}
|
||||
return nil, errorMessage
|
||||
return nil, utils.ApplySourceFromError(errorMessage, err)
|
||||
}
|
||||
}
|
||||
|
||||
@ -262,11 +258,7 @@ func (e *AzureLogAnalyticsDatasource) buildQuery(ctx context.Context, query back
|
||||
azureAppInsightsQuery, err := buildAppInsightsQuery(ctx, query, dsInfo, appInsightsRegExp, e.Logger)
|
||||
if err != nil {
|
||||
errorMessage := fmt.Errorf("failed to build azure application insights query: %w", err)
|
||||
var sourceError errorsource.Error
|
||||
if errors.As(err, &sourceError) {
|
||||
return nil, errorsource.SourceError(sourceError.Source(), errorMessage, false)
|
||||
}
|
||||
return nil, errorMessage
|
||||
return nil, utils.ApplySourceFromError(errorMessage, err)
|
||||
}
|
||||
azureLogAnalyticsQuery = azureAppInsightsQuery
|
||||
}
|
||||
@ -612,11 +604,11 @@ func getCorrelationWorkspaces(ctx context.Context, baseResource string, resource
|
||||
|
||||
res, err := azMonService.HTTPClient.Do(req)
|
||||
if err != nil {
|
||||
return AzureCorrelationAPIResponse{}, err
|
||||
return AzureCorrelationAPIResponse{}, errorsource.DownstreamError(err, false)
|
||||
}
|
||||
body, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return AzureCorrelationAPIResponse{}, err
|
||||
return AzureCorrelationAPIResponse{}, errorsource.DownstreamError(err, false)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
|
@ -201,7 +201,8 @@ func buildAppInsightsQuery(ctx context.Context, query backend.DataQuery, dsInfo
|
||||
if query.QueryType == string(dataquery.AzureQueryTypeTraceql) {
|
||||
subscription, err := utils.GetFirstSubscriptionOrDefault(ctx, dsInfo, logger)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to retrieve subscription for trace exemplars query: %w", err)
|
||||
errorMessage := fmt.Errorf("failed to retrieve subscription for trace exemplars query: %w", err)
|
||||
return nil, utils.ApplySourceFromError(errorMessage, err)
|
||||
}
|
||||
resources = []string{fmt.Sprintf("/subscriptions/%s", subscription)}
|
||||
}
|
||||
@ -222,7 +223,8 @@ func buildAppInsightsQuery(ctx context.Context, query backend.DataQuery, dsInfo
|
||||
operationId = *queryJSONModel.AzureTraces.OperationId
|
||||
resourcesMap, err = getCorrelationWorkspaces(ctx, resourceOrWorkspace, resourcesMap, dsInfo, operationId)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to retrieve correlation resources for operation ID - %s: %s", operationId, err)
|
||||
errorMessage := fmt.Errorf("failed to retrieve correlation resources for operation ID - %s: %s", operationId, err)
|
||||
return nil, utils.ApplySourceFromError(errorMessage, err)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -137,7 +137,6 @@ func (e *AzureResourceGraphDatasource) executeQuery(ctx context.Context, query *
|
||||
}
|
||||
|
||||
req, err := e.createRequest(ctx, reqBody, dsURL)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -3,10 +3,12 @@ package utils
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
|
||||
"github.com/grafana/grafana-plugin-sdk-go/experimental/errorsource"
|
||||
"github.com/grafana/grafana/pkg/tsdb/azuremonitor/types"
|
||||
)
|
||||
|
||||
@ -25,7 +27,7 @@ func GetFirstSubscriptionOrDefault(ctx context.Context, dsInfo types.DatasourceI
|
||||
|
||||
res, err := dsInfo.Services["Azure Monitor"].HTTPClient.Do(request)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to retrieve subscriptions: %v", err)
|
||||
return "", errorsource.DownstreamError(fmt.Errorf("failed to retrieve subscriptions: %v", err), false)
|
||||
}
|
||||
defer func() {
|
||||
if err := res.Body.Close(); err != nil {
|
||||
@ -39,7 +41,7 @@ func GetFirstSubscriptionOrDefault(ctx context.Context, dsInfo types.DatasourceI
|
||||
}
|
||||
|
||||
if len(subscriptions) == 0 {
|
||||
return "", fmt.Errorf("no subscriptions found: %v", err)
|
||||
return "", errorsource.DownstreamError(fmt.Errorf("no subscriptions found: %v", err), false)
|
||||
}
|
||||
|
||||
return subscriptions[0], nil
|
||||
@ -68,3 +70,11 @@ func ParseSubscriptions(res *http.Response, logger log.Logger) ([]string, error)
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func ApplySourceFromError(errorMessage error, err error) error {
|
||||
var sourceError errorsource.Error
|
||||
if errors.As(err, &sourceError) {
|
||||
return errorsource.SourceError(sourceError.Source(), errorMessage, false)
|
||||
}
|
||||
return errorMessage
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user