Cloudwatch: Revert aws sdk go v2 (#103644)
* Revert "CloudWatch: Import new grafana-aws-sdk with PDC fix (#103249)" This reverts commitf2b5b4e0c0. * Revert "CloudWatch: Migrate to aws-sdk-go-v2 (#103106)" This reverts commita65cc0df93. * make update-workspace
This commit is contained in:
@@ -3,42 +3,41 @@ package clients
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/service/cloudwatch"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws/awsutil"
|
||||
"github.com/aws/aws-sdk-go/service/cloudwatch"
|
||||
"github.com/grafana/grafana/pkg/tsdb/cloudwatch/models"
|
||||
"github.com/grafana/grafana/pkg/tsdb/cloudwatch/models/resources"
|
||||
"github.com/grafana/grafana/pkg/tsdb/cloudwatch/utils"
|
||||
)
|
||||
|
||||
type MetricsClient struct {
|
||||
cloudwatch.ListMetricsAPIClient
|
||||
|
||||
// this client wraps the CloudWatch API and handles pagination and the composition of the MetricResponse DTO
|
||||
type metricsClient struct {
|
||||
models.CloudWatchMetricsAPIProvider
|
||||
listMetricsPageLimit int
|
||||
}
|
||||
|
||||
func NewMetricsClient(client cloudwatch.ListMetricsAPIClient, listMetricsPageLimit int) *MetricsClient {
|
||||
return &MetricsClient{
|
||||
ListMetricsAPIClient: client,
|
||||
listMetricsPageLimit: listMetricsPageLimit,
|
||||
}
|
||||
func NewMetricsClient(api models.CloudWatchMetricsAPIProvider, pageLimit int) *metricsClient {
|
||||
return &metricsClient{CloudWatchMetricsAPIProvider: api, listMetricsPageLimit: pageLimit}
|
||||
}
|
||||
|
||||
func (mc *MetricsClient) ListMetricsWithPageLimit(ctx context.Context, params *cloudwatch.ListMetricsInput) ([]resources.MetricResponse, error) {
|
||||
var response []resources.MetricResponse
|
||||
paginator := cloudwatch.NewListMetricsPaginator(mc.ListMetricsAPIClient, params)
|
||||
includeAccount := params.IncludeLinkedAccounts != nil && *params.IncludeLinkedAccounts
|
||||
pages := 0
|
||||
for paginator.HasMorePages() && pages < mc.listMetricsPageLimit {
|
||||
pages += 1
|
||||
page, err := paginator.NextPage(ctx)
|
||||
if err != nil {
|
||||
return response, err
|
||||
}
|
||||
for i, metric := range page.Metrics {
|
||||
resp := resources.MetricResponse{Metric: metric}
|
||||
if includeAccount && len(page.OwningAccounts) >= i {
|
||||
resp.AccountId = &page.OwningAccounts[i]
|
||||
func (l *metricsClient) ListMetricsWithPageLimit(ctx context.Context, params *cloudwatch.ListMetricsInput) ([]resources.MetricResponse, error) {
|
||||
var cloudWatchMetrics []resources.MetricResponse
|
||||
pageNum := 0
|
||||
err := l.ListMetricsPagesWithContext(ctx, params, func(page *cloudwatch.ListMetricsOutput, lastPage bool) bool {
|
||||
pageNum++
|
||||
utils.QueriesTotalCounter.WithLabelValues(utils.ListMetricsLabel).Inc()
|
||||
metrics, err := awsutil.ValuesAtPath(page, "Metrics")
|
||||
if err == nil {
|
||||
for idx, metric := range metrics {
|
||||
metric := resources.MetricResponse{Metric: metric.(*cloudwatch.Metric)}
|
||||
if len(page.OwningAccounts) >= idx && params.IncludeLinkedAccounts != nil && *params.IncludeLinkedAccounts {
|
||||
metric.AccountId = page.OwningAccounts[idx]
|
||||
}
|
||||
cloudWatchMetrics = append(cloudWatchMetrics, metric)
|
||||
}
|
||||
response = append(response, resp)
|
||||
}
|
||||
}
|
||||
return response, nil
|
||||
return !lastPage && pageNum < l.listMetricsPageLimit
|
||||
})
|
||||
|
||||
return cloudWatchMetrics, err
|
||||
}
|
||||
|
||||
@@ -4,19 +4,16 @@ import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/aws"
|
||||
"github.com/aws/aws-sdk-go-v2/service/cloudwatch"
|
||||
cloudwatchtypes "github.com/aws/aws-sdk-go-v2/service/cloudwatch/types"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/service/cloudwatch"
|
||||
"github.com/grafana/grafana/pkg/tsdb/cloudwatch/mocks"
|
||||
"github.com/grafana/grafana/pkg/tsdb/cloudwatch/models/resources"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestMetricsClient(t *testing.T) {
|
||||
metrics := []cloudwatchtypes.Metric{
|
||||
metrics := []*cloudwatch.Metric{
|
||||
{MetricName: aws.String("Test_MetricName1")},
|
||||
{MetricName: aws.String("Test_MetricName2")},
|
||||
{MetricName: aws.String("Test_MetricName3")},
|
||||
@@ -53,25 +50,25 @@ func TestMetricsClient(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("Should return account id in case IncludeLinkedAccounts is set to true", func(t *testing.T) {
|
||||
fakeApi := &mocks.FakeMetricsAPI{Metrics: []cloudwatchtypes.Metric{
|
||||
fakeApi := &mocks.FakeMetricsAPI{Metrics: []*cloudwatch.Metric{
|
||||
{MetricName: aws.String("Test_MetricName1")},
|
||||
{MetricName: aws.String("Test_MetricName2")},
|
||||
{MetricName: aws.String("Test_MetricName3")},
|
||||
}, OwningAccounts: []string{"1234567890", "1234567890", "1234567895"}}
|
||||
}, OwningAccounts: []*string{aws.String("1234567890"), aws.String("1234567890"), aws.String("1234567895")}}
|
||||
client := NewMetricsClient(fakeApi, 100)
|
||||
|
||||
response, err := client.ListMetricsWithPageLimit(ctx, &cloudwatch.ListMetricsInput{IncludeLinkedAccounts: aws.Bool(true)})
|
||||
require.NoError(t, err)
|
||||
expected := []resources.MetricResponse{
|
||||
{Metric: cloudwatchtypes.Metric{MetricName: aws.String("Test_MetricName1")}, AccountId: stringPtr("1234567890")},
|
||||
{Metric: cloudwatchtypes.Metric{MetricName: aws.String("Test_MetricName2")}, AccountId: stringPtr("1234567890")},
|
||||
{Metric: cloudwatchtypes.Metric{MetricName: aws.String("Test_MetricName3")}, AccountId: stringPtr("1234567895")},
|
||||
{Metric: &cloudwatch.Metric{MetricName: aws.String("Test_MetricName1")}, AccountId: stringPtr("1234567890")},
|
||||
{Metric: &cloudwatch.Metric{MetricName: aws.String("Test_MetricName2")}, AccountId: stringPtr("1234567890")},
|
||||
{Metric: &cloudwatch.Metric{MetricName: aws.String("Test_MetricName3")}, AccountId: stringPtr("1234567895")},
|
||||
}
|
||||
assert.Equal(t, expected, response)
|
||||
})
|
||||
|
||||
t.Run("Should not return account id in case IncludeLinkedAccounts is set to false", func(t *testing.T) {
|
||||
fakeApi := &mocks.FakeMetricsAPI{Metrics: []cloudwatchtypes.Metric{{MetricName: aws.String("Test_MetricName1")}}, OwningAccounts: []string{"1234567890"}}
|
||||
fakeApi := &mocks.FakeMetricsAPI{Metrics: []*cloudwatch.Metric{{MetricName: aws.String("Test_MetricName1")}}, OwningAccounts: []*string{aws.String("1234567890")}}
|
||||
client := NewMetricsClient(fakeApi, 100)
|
||||
|
||||
response, err := client.ListMetricsWithPageLimit(ctx, &cloudwatch.ListMetricsInput{IncludeLinkedAccounts: aws.Bool(false)})
|
||||
|
||||
Reference in New Issue
Block a user