2015-01-06 02:11:00 -06:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2020-07-24 05:34:56 -05:00
|
|
|
"errors"
|
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"
|
2021-08-18 02:37:13 -05:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2022-04-29 05:08:40 -05:00
|
|
|
"github.com/grafana/grafana/pkg/plugins/backendplugin"
|
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-12-14 11:38:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/query"
|
2021-11-29 07:21:54 -06:00
|
|
|
"github.com/grafana/grafana/pkg/util"
|
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)
|
|
|
|
}
|
|
|
|
var badQuery *query.ErrBadQuery
|
|
|
|
if errors.As(err, &badQuery) {
|
|
|
|
return response.Error(http.StatusBadRequest, util.Capitalize(badQuery.Message), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if errors.Is(err, backendplugin.ErrPluginNotRegistered) {
|
|
|
|
return response.Error(http.StatusNotFound, "Plugin not found", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.Error(http.StatusInternalServerError, "Query data error", err)
|
|
|
|
}
|
|
|
|
|
2022-05-16 16:17:05 -05:00
|
|
|
// QueryMetricsV2 returns query metrics.
|
|
|
|
// POST /api/ds/query DataSource query w/ expressions
|
|
|
|
func (hs *HTTPServer) QueryMetricsV2(c *models.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)
|
|
|
|
}
|
|
|
|
|
2022-05-31 10:02:58 -05:00
|
|
|
reqDTO.HTTPRequest = c.Req
|
|
|
|
|
2022-03-07 12:33:01 -06:00
|
|
|
resp, err := hs.queryDataService.QueryData(c.Req.Context(), c.SignedInUser, c.SkipCache, reqDTO, true)
|
|
|
|
if err != nil {
|
|
|
|
return hs.handleQueryMetricsError(err)
|
|
|
|
}
|
2022-05-03 11:02:20 -05:00
|
|
|
return hs.toJsonStreamingResponse(resp)
|
2022-03-07 12:33:01 -06:00
|
|
|
}
|
|
|
|
|
2022-05-03 11:02:20 -05:00
|
|
|
func (hs *HTTPServer) toJsonStreamingResponse(qdr *backend.QueryDataResponse) response.Response {
|
|
|
|
statusWhenError := http.StatusBadRequest
|
|
|
|
if hs.Features.IsEnabled(featuremgmt.FlagDatasourceQueryMultiStatus) {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.JSONStreaming(statusCode, qdr)
|
2019-10-25 08:28:26 -05:00
|
|
|
}
|