2015-01-06 02:11:00 -06:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2023-09-11 05:13:13 -05:00
|
|
|
"context"
|
2020-07-24 05:34:56 -05:00
|
|
|
"errors"
|
2022-07-13 08:27:03 -05:00
|
|
|
"fmt"
|
2021-02-03 13:47:45 -06:00
|
|
|
"net/http"
|
2016-06-03 08:06:57 -05:00
|
|
|
|
2021-03-31 10:35:03 -05:00
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
2022-05-17 13:52:22 -05:00
|
|
|
|
2016-06-03 08:06:57 -05:00
|
|
|
"github.com/grafana/grafana/pkg/api/dtos"
|
2021-01-15 07:43:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/api/response"
|
2023-09-11 05:13:13 -05:00
|
|
|
"github.com/grafana/grafana/pkg/middleware/requestmeta"
|
2023-01-27 01:50:36 -06:00
|
|
|
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
|
2022-06-27 11:23:15 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/datasources"
|
2022-03-07 12:33:01 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
2021-11-29 03:18:01 -06:00
|
|
|
"github.com/grafana/grafana/pkg/web"
|
2015-01-06 02:11:00 -06:00
|
|
|
)
|
|
|
|
|
2022-04-29 05:08:40 -05:00
|
|
|
func (hs *HTTPServer) handleQueryMetricsError(err error) *response.NormalResponse {
|
2022-06-27 11:23:15 -05:00
|
|
|
if errors.Is(err, datasources.ErrDataSourceAccessDenied) {
|
2022-04-29 05:08:40 -05:00
|
|
|
return response.Error(http.StatusForbidden, "Access denied to data source", err)
|
|
|
|
}
|
2022-06-27 11:23:15 -05:00
|
|
|
if errors.Is(err, datasources.ErrDataSourceNotFound) {
|
2022-04-29 05:08:40 -05:00
|
|
|
return response.Error(http.StatusNotFound, "Data source not found", err)
|
|
|
|
}
|
2022-07-13 08:27:03 -05:00
|
|
|
|
|
|
|
var secretsPlugin datasources.ErrDatasourceSecretsPluginUserFriendly
|
|
|
|
if errors.As(err, &secretsPlugin) {
|
|
|
|
return response.Error(http.StatusInternalServerError, fmt.Sprint("Secrets Plugin error: ", err.Error()), err)
|
|
|
|
}
|
|
|
|
|
2022-09-14 11:19:57 -05:00
|
|
|
return response.ErrOrFallback(http.StatusInternalServerError, "Query data error", err)
|
2022-04-29 05:08:40 -05:00
|
|
|
}
|
|
|
|
|
2022-05-16 16:17:05 -05:00
|
|
|
// QueryMetricsV2 returns query metrics.
|
2022-07-27 08:54:37 -05:00
|
|
|
// swagger:route POST /ds/query ds queryMetricsWithExpressions
|
|
|
|
//
|
2022-09-12 02:40:35 -05:00
|
|
|
// DataSource query metrics with expressions.
|
2022-07-27 08:54:37 -05:00
|
|
|
//
|
|
|
|
// If you are running Grafana Enterprise and have Fine-grained access control enabled
|
|
|
|
// you need to have a permission with action: `datasources:query`.
|
|
|
|
//
|
|
|
|
// Responses:
|
|
|
|
// 200: queryMetricsWithExpressionsRespons
|
|
|
|
// 207: queryMetricsWithExpressionsRespons
|
|
|
|
// 401: unauthorisedError
|
|
|
|
// 400: badRequestError
|
|
|
|
// 403: forbiddenError
|
|
|
|
// 500: internalServerError
|
2023-01-27 01:50:36 -06:00
|
|
|
func (hs *HTTPServer) QueryMetricsV2(c *contextmodel.ReqContext) response.Response {
|
2022-03-07 12:33:01 -06:00
|
|
|
reqDTO := dtos.MetricRequest{}
|
|
|
|
if err := web.Bind(c.Req, &reqDTO); err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
|
|
}
|
|
|
|
|
2023-04-12 11:30:33 -05:00
|
|
|
resp, err := hs.queryDataService.QueryData(c.Req.Context(), c.SignedInUser, c.SkipDSCache, reqDTO)
|
2022-03-07 12:33:01 -06:00
|
|
|
if err != nil {
|
|
|
|
return hs.handleQueryMetricsError(err)
|
|
|
|
}
|
2023-09-11 05:13:13 -05:00
|
|
|
return hs.toJsonStreamingResponse(c.Req.Context(), resp)
|
2022-03-07 12:33:01 -06:00
|
|
|
}
|
|
|
|
|
2023-09-11 05:13:13 -05:00
|
|
|
func (hs *HTTPServer) toJsonStreamingResponse(ctx context.Context, qdr *backend.QueryDataResponse) response.Response {
|
2022-05-03 11:02:20 -05:00
|
|
|
statusWhenError := http.StatusBadRequest
|
2023-11-14 14:50:27 -06:00
|
|
|
if hs.Features.IsEnabled(ctx, featuremgmt.FlagDatasourceQueryMultiStatus) {
|
2022-05-03 11:02:20 -05:00
|
|
|
statusWhenError = http.StatusMultiStatus
|
|
|
|
}
|
|
|
|
|
2021-11-05 10:12:55 -05:00
|
|
|
statusCode := http.StatusOK
|
|
|
|
for _, res := range qdr.Responses {
|
|
|
|
if res.Error != nil {
|
2022-05-03 11:02:20 -05:00
|
|
|
statusCode = statusWhenError
|
2021-11-05 10:12:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-11 05:13:13 -05:00
|
|
|
if statusCode == statusWhenError {
|
|
|
|
// an error in the response we treat as downstream.
|
|
|
|
requestmeta.WithDownstreamStatusSource(ctx)
|
|
|
|
}
|
|
|
|
|
2021-11-05 10:12:55 -05:00
|
|
|
return response.JSONStreaming(statusCode, qdr)
|
2019-10-25 08:28:26 -05:00
|
|
|
}
|
2022-07-27 08:54:37 -05:00
|
|
|
|
|
|
|
// swagger:parameters queryMetricsWithExpressions
|
|
|
|
type QueryMetricsWithExpressionsBodyParams struct {
|
|
|
|
// in:body
|
|
|
|
// required:true
|
|
|
|
Body dtos.MetricRequest `json:"body"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// swagger:response queryMetricsWithExpressionsRespons
|
|
|
|
type QueryMetricsWithExpressionsRespons struct {
|
|
|
|
// The response message
|
|
|
|
// in: body
|
|
|
|
Body *backend.QueryDataResponse `json:"body"`
|
|
|
|
}
|