mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
CloudWatch: Fix a few API status codes (#26578)
Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
This commit is contained in:
parent
49b86e88ac
commit
94d0934e90
@ -2,6 +2,7 @@ package api
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"sort"
|
"sort"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
@ -20,7 +21,7 @@ import (
|
|||||||
// POST /api/ds/query DataSource query w/ expressions
|
// POST /api/ds/query DataSource query w/ expressions
|
||||||
func (hs *HTTPServer) QueryMetricsV2(c *models.ReqContext, reqDto dtos.MetricRequest) Response {
|
func (hs *HTTPServer) QueryMetricsV2(c *models.ReqContext, reqDto dtos.MetricRequest) Response {
|
||||||
if len(reqDto.Queries) == 0 {
|
if len(reqDto.Queries) == 0 {
|
||||||
return Error(500, "No queries found in query", nil)
|
return Error(400, "No queries found in query", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
request := &tsdb.TsdbQuery{
|
request := &tsdb.TsdbQuery{
|
||||||
@ -32,6 +33,7 @@ func (hs *HTTPServer) QueryMetricsV2(c *models.ReqContext, reqDto dtos.MetricReq
|
|||||||
expr := false
|
expr := false
|
||||||
var ds *models.DataSource
|
var ds *models.DataSource
|
||||||
for i, query := range reqDto.Queries {
|
for i, query := range reqDto.Queries {
|
||||||
|
hs.log.Debug("Processing metrics query", "query", query)
|
||||||
name := query.Get("datasource").MustString("")
|
name := query.Get("datasource").MustString("")
|
||||||
if name == "__expr__" {
|
if name == "__expr__" {
|
||||||
expr = true
|
expr = true
|
||||||
@ -39,16 +41,21 @@ func (hs *HTTPServer) QueryMetricsV2(c *models.ReqContext, reqDto dtos.MetricReq
|
|||||||
|
|
||||||
datasourceID, err := query.Get("datasourceId").Int64()
|
datasourceID, err := query.Get("datasourceId").Int64()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Error(500, "datasource missing ID", nil)
|
hs.log.Debug("Can't process query since it's missing data source ID")
|
||||||
|
return Error(400, "Query missing data source ID", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
if i == 0 && !expr {
|
if i == 0 && !expr {
|
||||||
ds, err = hs.DatasourceCache.GetDatasource(datasourceID, c.SignedInUser, c.SkipCache)
|
ds, err = hs.DatasourceCache.GetDatasource(datasourceID, c.SignedInUser, c.SkipCache)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == models.ErrDataSourceAccessDenied {
|
hs.log.Debug("Encountered error getting data source", "err", err)
|
||||||
return Error(403, "Access denied to datasource", err)
|
if errors.Is(err, models.ErrDataSourceAccessDenied) {
|
||||||
|
return Error(403, "Access denied to data source", err)
|
||||||
}
|
}
|
||||||
return Error(500, "Unable to load datasource meta data", err)
|
if errors.Is(err, models.ErrDataSourceNotFound) {
|
||||||
|
return Error(400, "Invalid data source ID", err)
|
||||||
|
}
|
||||||
|
return Error(500, "Unable to load data source metadata", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -80,7 +80,7 @@ func (ss *SqlStore) Init() error {
|
|||||||
x = engine
|
x = engine
|
||||||
dialect = ss.Dialect
|
dialect = ss.Dialect
|
||||||
|
|
||||||
migrator := migrator.NewMigrator(x)
|
migrator := migrator.NewMigrator(engine)
|
||||||
migrations.AddMigrations(migrator)
|
migrations.AddMigrations(migrator)
|
||||||
|
|
||||||
for _, descriptor := range registry.GetServices() {
|
for _, descriptor := range registry.GetServices() {
|
||||||
|
@ -309,7 +309,8 @@ func parseMultiSelectValue(input string) []string {
|
|||||||
|
|
||||||
// Whenever this list is updated, the frontend list should also be updated.
|
// Whenever this list is updated, the frontend list should also be updated.
|
||||||
// Please update the region list in public/app/plugins/datasource/cloudwatch/partials/config.html
|
// Please update the region list in public/app/plugins/datasource/cloudwatch/partials/config.html
|
||||||
func (e *cloudWatchExecutor) handleGetRegions(ctx context.Context, parameters *simplejson.Json, queryContext *tsdb.TsdbQuery) ([]suggestData, error) {
|
func (e *cloudWatchExecutor) handleGetRegions(ctx context.Context, parameters *simplejson.Json,
|
||||||
|
queryContext *tsdb.TsdbQuery) ([]suggestData, error) {
|
||||||
dsInfo := e.getDSInfo(defaultRegion)
|
dsInfo := e.getDSInfo(defaultRegion)
|
||||||
profile := dsInfo.Profile
|
profile := dsInfo.Profile
|
||||||
if cache, ok := regionCache.Load(profile); ok {
|
if cache, ok := regionCache.Load(profile); ok {
|
||||||
@ -479,7 +480,8 @@ func (e *cloudWatchExecutor) handleGetDimensionValues(ctx context.Context, param
|
|||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *cloudWatchExecutor) handleGetEbsVolumeIds(ctx context.Context, parameters *simplejson.Json, queryContext *tsdb.TsdbQuery) ([]suggestData, error) {
|
func (e *cloudWatchExecutor) handleGetEbsVolumeIds(ctx context.Context, parameters *simplejson.Json,
|
||||||
|
queryContext *tsdb.TsdbQuery) ([]suggestData, error) {
|
||||||
region := parameters.Get("region").MustString()
|
region := parameters.Get("region").MustString()
|
||||||
instanceId := parameters.Get("instanceId").MustString()
|
instanceId := parameters.Get("instanceId").MustString()
|
||||||
|
|
||||||
@ -501,7 +503,8 @@ func (e *cloudWatchExecutor) handleGetEbsVolumeIds(ctx context.Context, paramete
|
|||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *cloudWatchExecutor) handleGetEc2InstanceAttribute(ctx context.Context, parameters *simplejson.Json, queryContext *tsdb.TsdbQuery) ([]suggestData, error) {
|
func (e *cloudWatchExecutor) handleGetEc2InstanceAttribute(ctx context.Context, parameters *simplejson.Json,
|
||||||
|
queryContext *tsdb.TsdbQuery) ([]suggestData, error) {
|
||||||
region := parameters.Get("region").MustString()
|
region := parameters.Get("region").MustString()
|
||||||
attributeName := parameters.Get("attributeName").MustString()
|
attributeName := parameters.Get("attributeName").MustString()
|
||||||
filterJson := parameters.Get("filters").MustMap()
|
filterJson := parameters.Get("filters").MustMap()
|
||||||
@ -580,7 +583,8 @@ func (e *cloudWatchExecutor) handleGetEc2InstanceAttribute(ctx context.Context,
|
|||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *cloudWatchExecutor) handleGetResourceArns(ctx context.Context, parameters *simplejson.Json, queryContext *tsdb.TsdbQuery) ([]suggestData, error) {
|
func (e *cloudWatchExecutor) handleGetResourceArns(ctx context.Context, parameters *simplejson.Json,
|
||||||
|
queryContext *tsdb.TsdbQuery) ([]suggestData, error) {
|
||||||
region := parameters.Get("region").MustString()
|
region := parameters.Get("region").MustString()
|
||||||
resourceType := parameters.Get("resourceType").MustString()
|
resourceType := parameters.Get("resourceType").MustString()
|
||||||
filterJson := parameters.Get("tags").MustMap()
|
filterJson := parameters.Get("tags").MustMap()
|
||||||
@ -618,7 +622,8 @@ func (e *cloudWatchExecutor) handleGetResourceArns(ctx context.Context, paramete
|
|||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *cloudWatchExecutor) cloudwatchListMetrics(region string, namespace string, metricName string, dimensions []*cloudwatch.DimensionFilter) (*cloudwatch.ListMetricsOutput, error) {
|
func (e *cloudWatchExecutor) cloudwatchListMetrics(region string, namespace string, metricName string,
|
||||||
|
dimensions []*cloudwatch.DimensionFilter) (*cloudwatch.ListMetricsOutput, error) {
|
||||||
svc, err := e.getCWClient(region)
|
svc, err := e.getCWClient(region)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -706,6 +711,7 @@ func (e *cloudWatchExecutor) getAllMetrics(region string) (cloudwatch.ListMetric
|
|||||||
Namespace: aws.String(dsInfo.Namespace),
|
Namespace: aws.String(dsInfo.Namespace),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
plog.Debug("Listing metrics pages")
|
||||||
var resp cloudwatch.ListMetricsOutput
|
var resp cloudwatch.ListMetricsOutput
|
||||||
err = client.ListMetricsPages(params, func(page *cloudwatch.ListMetricsOutput, lastPage bool) bool {
|
err = client.ListMetricsPages(params, func(page *cloudwatch.ListMetricsOutput, lastPage bool) bool {
|
||||||
metrics.MAwsCloudWatchListMetrics.Inc()
|
metrics.MAwsCloudWatchListMetrics.Inc()
|
||||||
@ -726,6 +732,7 @@ func (e *cloudWatchExecutor) getAllMetrics(region string) (cloudwatch.ListMetric
|
|||||||
var metricsCacheLock sync.Mutex
|
var metricsCacheLock sync.Mutex
|
||||||
|
|
||||||
func (e *cloudWatchExecutor) getMetricsForCustomMetrics(region, namespace string) ([]string, error) {
|
func (e *cloudWatchExecutor) getMetricsForCustomMetrics(region, namespace string) ([]string, error) {
|
||||||
|
plog.Debug("Getting metrics for custom metrics", "region", region, "namespace", namespace)
|
||||||
metricsCacheLock.Lock()
|
metricsCacheLock.Lock()
|
||||||
defer metricsCacheLock.Unlock()
|
defer metricsCacheLock.Unlock()
|
||||||
|
|
||||||
@ -750,6 +757,7 @@ func (e *cloudWatchExecutor) getMetricsForCustomMetrics(region, namespace string
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return []string{}, err
|
return []string{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache = make([]string, 0)
|
customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache = make([]string, 0)
|
||||||
customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Expire = time.Now().Add(5 * time.Minute)
|
customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Expire = time.Now().Add(5 * time.Minute)
|
||||||
|
|
||||||
@ -757,7 +765,8 @@ func (e *cloudWatchExecutor) getMetricsForCustomMetrics(region, namespace string
|
|||||||
if isDuplicate(customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache, *metric.MetricName) {
|
if isDuplicate(customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache, *metric.MetricName) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache = append(customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache, *metric.MetricName)
|
customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache = append(
|
||||||
|
customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache, *metric.MetricName)
|
||||||
}
|
}
|
||||||
|
|
||||||
return customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache, nil
|
return customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache, nil
|
||||||
@ -797,7 +806,8 @@ func (e *cloudWatchExecutor) getDimensionsForCustomMetrics(region string) ([]str
|
|||||||
if isDuplicate(customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache, *dimension.Name) {
|
if isDuplicate(customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache, *dimension.Name) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache = append(customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache, *dimension.Name)
|
customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache = append(
|
||||||
|
customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache, *dimension.Name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user