mirror of
https://github.com/grafana/grafana.git
synced 2025-07-30 04:52:26 +08:00

* CloudWatch: Backport aws-sdk-go-v2 update from external plugin * Review feedback & cleaning up a couple typos
45 lines
1.2 KiB
Go
45 lines
1.2 KiB
Go
package clients
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/service/cloudwatch"
|
|
|
|
"github.com/grafana/grafana/pkg/tsdb/cloudwatch/models/resources"
|
|
)
|
|
|
|
type MetricsClient struct {
|
|
cloudwatch.ListMetricsAPIClient
|
|
|
|
listMetricsPageLimit int
|
|
}
|
|
|
|
func NewMetricsClient(client cloudwatch.ListMetricsAPIClient, listMetricsPageLimit int) *MetricsClient {
|
|
return &MetricsClient{
|
|
ListMetricsAPIClient: client,
|
|
listMetricsPageLimit: listMetricsPageLimit,
|
|
}
|
|
}
|
|
|
|
func (mc *MetricsClient) ListMetricsWithPageLimit(ctx context.Context, params *cloudwatch.ListMetricsInput) ([]resources.MetricResponse, error) {
|
|
var responses []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 responses, err
|
|
}
|
|
for i, metric := range page.Metrics {
|
|
resp := resources.MetricResponse{Metric: metric}
|
|
if includeAccount && len(page.OwningAccounts) >= i {
|
|
resp.AccountId = &page.OwningAccounts[i]
|
|
}
|
|
responses = append(responses, resp)
|
|
}
|
|
}
|
|
return responses, nil
|
|
}
|