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"
|
2024-01-31 12:36:51 -06:00
|
|
|
"github.com/grafana/grafana/pkg/api/routing"
|
2024-06-20 09:53:07 -05:00
|
|
|
"github.com/grafana/grafana/pkg/apimachinery/identity"
|
2023-09-11 05:13:13 -05:00
|
|
|
"github.com/grafana/grafana/pkg/middleware/requestmeta"
|
2024-02-01 16:27:30 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/apiserver/endpoints/request"
|
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"
|
2024-06-12 23:11:35 -05:00
|
|
|
"github.com/grafana/grafana/pkg/util/errhttp"
|
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
|
|
|
}
|
|
|
|
|
2024-01-31 12:36:51 -06:00
|
|
|
// metrics.go
|
|
|
|
func (hs *HTTPServer) getDSQueryEndpoint() web.Handler {
|
2024-04-19 04:26:21 -05:00
|
|
|
if hs.Features.IsEnabledGlobally(featuremgmt.FlagQueryServiceRewrite) {
|
2024-01-31 12:36:51 -06:00
|
|
|
// rewrite requests from /ds/query to the new query service
|
|
|
|
namespaceMapper := request.GetNamespaceMapper(hs.Cfg)
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2024-06-20 09:53:07 -05:00
|
|
|
user, err := identity.GetRequester(r.Context())
|
2024-01-31 12:36:51 -06:00
|
|
|
if err != nil || user == nil {
|
|
|
|
errhttp.Write(r.Context(), fmt.Errorf("no user"), w)
|
|
|
|
return
|
|
|
|
}
|
2024-06-20 09:53:07 -05:00
|
|
|
r.URL.Path = "/apis/query.grafana.app/v0alpha1/namespaces/" + namespaceMapper(user.GetOrgID()) + "/query"
|
2024-01-31 12:36:51 -06:00
|
|
|
hs.clientConfigProvider.DirectlyServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return routing.Wrap(hs.QueryMetricsV2)
|
|
|
|
}
|
|
|
|
|
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 {
|
2021-11-05 10:12:55 -05:00
|
|
|
statusCode := http.StatusOK
|
|
|
|
for _, res := range qdr.Responses {
|
|
|
|
if res.Error != nil {
|
2024-07-10 04:15:10 -05:00
|
|
|
statusCode = http.StatusBadRequest
|
|
|
|
break
|
2021-11-05 10:12:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-10 04:15:10 -05:00
|
|
|
if statusCode == http.StatusBadRequest {
|
2023-09-11 05:13:13 -05:00
|
|
|
// 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"`
|
|
|
|
}
|