Cloudwatch: Refactor dimension keys resource request (#57148)
* use new layered architecture in get dimension keys request * go lint fixes * pr feedback * more pr feedback * remove not used code * refactor route middleware * change signature * add integration tests for the dimension keys route * use request suffix instead of query * use typed args also in frontend * remove unused import * harmonize naming * fix merge conflict
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
package clients
|
||||
|
||||
import (
|
||||
"github.com/aws/aws-sdk-go/aws/awsutil"
|
||||
"github.com/aws/aws-sdk-go/service/cloudwatch"
|
||||
"github.com/grafana/grafana/pkg/infra/metrics"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/grafana/grafana/pkg/tsdb/cloudwatch/models"
|
||||
)
|
||||
|
||||
type metricsClient struct {
|
||||
models.CloudWatchMetricsAPIProvider
|
||||
config *setting.Cfg
|
||||
}
|
||||
|
||||
func NewMetricsClient(api models.CloudWatchMetricsAPIProvider, config *setting.Cfg) *metricsClient {
|
||||
return &metricsClient{CloudWatchMetricsAPIProvider: api, config: config}
|
||||
}
|
||||
|
||||
func (l *metricsClient) ListMetricsWithPageLimit(params *cloudwatch.ListMetricsInput) ([]*cloudwatch.Metric, error) {
|
||||
var cloudWatchMetrics []*cloudwatch.Metric
|
||||
pageNum := 0
|
||||
err := l.ListMetricsPages(params, func(page *cloudwatch.ListMetricsOutput, lastPage bool) bool {
|
||||
pageNum++
|
||||
metrics.MAwsCloudWatchListMetrics.Inc()
|
||||
metrics, err := awsutil.ValuesAtPath(page, "Metrics")
|
||||
if err == nil {
|
||||
for _, metric := range metrics {
|
||||
cloudWatchMetrics = append(cloudWatchMetrics, metric.(*cloudwatch.Metric))
|
||||
}
|
||||
}
|
||||
return !lastPage && pageNum < l.config.AWSListMetricsPageLimit
|
||||
})
|
||||
|
||||
return cloudWatchMetrics, err
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package clients
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/service/cloudwatch"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/grafana/grafana/pkg/tsdb/cloudwatch/mocks"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestMetricsClient(t *testing.T) {
|
||||
metrics := []*cloudwatch.Metric{
|
||||
{MetricName: aws.String("Test_MetricName1")},
|
||||
{MetricName: aws.String("Test_MetricName2")},
|
||||
{MetricName: aws.String("Test_MetricName3")},
|
||||
{MetricName: aws.String("Test_MetricName4")},
|
||||
{MetricName: aws.String("Test_MetricName5")},
|
||||
{MetricName: aws.String("Test_MetricName6")},
|
||||
{MetricName: aws.String("Test_MetricName7")},
|
||||
{MetricName: aws.String("Test_MetricName8")},
|
||||
{MetricName: aws.String("Test_MetricName9")},
|
||||
{MetricName: aws.String("Test_MetricName10")},
|
||||
}
|
||||
|
||||
t.Run("List Metrics and page limit is reached", func(t *testing.T) {
|
||||
pageLimit := 3
|
||||
fakeApi := &mocks.FakeMetricsAPI{Metrics: metrics, MetricsPerPage: 2}
|
||||
client := NewMetricsClient(fakeApi, &setting.Cfg{AWSListMetricsPageLimit: pageLimit})
|
||||
response, err := client.ListMetricsWithPageLimit(&cloudwatch.ListMetricsInput{})
|
||||
require.NoError(t, err)
|
||||
|
||||
expectedMetrics := fakeApi.MetricsPerPage * pageLimit
|
||||
assert.Equal(t, expectedMetrics, len(response))
|
||||
})
|
||||
|
||||
t.Run("List Metrics and page limit is not reached", func(t *testing.T) {
|
||||
pageLimit := 2
|
||||
fakeApi := &mocks.FakeMetricsAPI{Metrics: metrics}
|
||||
client := NewMetricsClient(fakeApi, &setting.Cfg{AWSListMetricsPageLimit: pageLimit})
|
||||
|
||||
response, err := client.ListMetricsWithPageLimit(&cloudwatch.ListMetricsInput{})
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, len(metrics), len(response))
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user