Commit af17f9fd by Arve Knudsen Committed by GitHub

CloudWatch: Fix custom metrics (#28391)

* CloudWatch: Fix querying for custom metrics

Co-authored by Mitch McKenzie <mitch.mckenzie@outlook.com>

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
parent b497063f
...@@ -414,10 +414,7 @@ func (e *cloudWatchExecutor) handleGetDimensions(ctx context.Context, parameters ...@@ -414,10 +414,7 @@ func (e *cloudWatchExecutor) handleGetDimensions(ctx context.Context, parameters
} }
} else { } else {
var err error var err error
dsInfo := e.getDSInfo(region) if dimensionValues, err = e.getDimensionsForCustomMetrics(region, namespace); err != nil {
dsInfo.Namespace = namespace
if dimensionValues, err = e.getDimensionsForCustomMetrics(region); err != nil {
return nil, errutil.Wrap("unable to call AWS API", err) return nil, errutil.Wrap("unable to call AWS API", err)
} }
} }
...@@ -702,15 +699,14 @@ func (e *cloudWatchExecutor) resourceGroupsGetResources(region string, filters [ ...@@ -702,15 +699,14 @@ func (e *cloudWatchExecutor) resourceGroupsGetResources(region string, filters [
return &resp, nil return &resp, nil
} }
func (e *cloudWatchExecutor) getAllMetrics(region string) (cloudwatch.ListMetricsOutput, error) { func (e *cloudWatchExecutor) getAllMetrics(region, namespace string) (cloudwatch.ListMetricsOutput, error) {
client, err := e.getCWClient(region) client, err := e.getCWClient(region)
if err != nil { if err != nil {
return cloudwatch.ListMetricsOutput{}, err return cloudwatch.ListMetricsOutput{}, err
} }
dsInfo := e.getDSInfo(region)
params := &cloudwatch.ListMetricsInput{ params := &cloudwatch.ListMetricsInput{
Namespace: aws.String(dsInfo.Namespace), Namespace: aws.String(namespace),
} }
plog.Debug("Listing metrics pages") plog.Debug("Listing metrics pages")
...@@ -739,7 +735,6 @@ func (e *cloudWatchExecutor) getMetricsForCustomMetrics(region, namespace string ...@@ -739,7 +735,6 @@ func (e *cloudWatchExecutor) getMetricsForCustomMetrics(region, namespace string
defer metricsCacheLock.Unlock() defer metricsCacheLock.Unlock()
dsInfo := e.getDSInfo(region) dsInfo := e.getDSInfo(region)
dsInfo.Namespace = namespace
if _, ok := customMetricsMetricsMap[dsInfo.Profile]; !ok { if _, ok := customMetricsMetricsMap[dsInfo.Profile]; !ok {
customMetricsMetricsMap[dsInfo.Profile] = make(map[string]map[string]*customMetricsCache) customMetricsMetricsMap[dsInfo.Profile] = make(map[string]map[string]*customMetricsCache)
...@@ -747,36 +742,36 @@ func (e *cloudWatchExecutor) getMetricsForCustomMetrics(region, namespace string ...@@ -747,36 +742,36 @@ func (e *cloudWatchExecutor) getMetricsForCustomMetrics(region, namespace string
if _, ok := customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region]; !ok { if _, ok := customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region]; !ok {
customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region] = make(map[string]*customMetricsCache) customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region] = make(map[string]*customMetricsCache)
} }
if _, ok := customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace]; !ok { if _, ok := customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][namespace]; !ok {
customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace] = &customMetricsCache{} customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][namespace] = &customMetricsCache{}
customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache = make([]string, 0) customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][namespace].Cache = make([]string, 0)
} }
if customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Expire.After(time.Now()) { if customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][namespace].Expire.After(time.Now()) {
return customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache, nil return customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][namespace].Cache, nil
} }
result, err := e.getAllMetrics(region) result, err := e.getAllMetrics(region, namespace)
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][namespace].Cache = make([]string, 0)
customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Expire = time.Now().Add(5 * time.Minute) customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][namespace].Expire = time.Now().Add(5 * time.Minute)
for _, metric := range result.Metrics { for _, metric := range result.Metrics {
if isDuplicate(customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache, *metric.MetricName) { if isDuplicate(customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][namespace].Cache, *metric.MetricName) {
continue continue
} }
customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache = append( customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][namespace].Cache = append(
customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache, *metric.MetricName) customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][namespace].Cache, *metric.MetricName)
} }
return customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache, nil return customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][namespace].Cache, nil
} }
var dimensionsCacheLock sync.Mutex var dimensionsCacheLock sync.Mutex
func (e *cloudWatchExecutor) getDimensionsForCustomMetrics(region string) ([]string, error) { func (e *cloudWatchExecutor) getDimensionsForCustomMetrics(region, namespace string) ([]string, error) {
dimensionsCacheLock.Lock() dimensionsCacheLock.Lock()
defer dimensionsCacheLock.Unlock() defer dimensionsCacheLock.Unlock()
...@@ -788,32 +783,32 @@ func (e *cloudWatchExecutor) getDimensionsForCustomMetrics(region string) ([]str ...@@ -788,32 +783,32 @@ func (e *cloudWatchExecutor) getDimensionsForCustomMetrics(region string) ([]str
if _, ok := customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region]; !ok { if _, ok := customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region]; !ok {
customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region] = make(map[string]*customMetricsCache) customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region] = make(map[string]*customMetricsCache)
} }
if _, ok := customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace]; !ok { if _, ok := customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][namespace]; !ok {
customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace] = &customMetricsCache{} customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][namespace] = &customMetricsCache{}
customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache = make([]string, 0) customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][namespace].Cache = make([]string, 0)
} }
if customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Expire.After(time.Now()) { if customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][namespace].Expire.After(time.Now()) {
return customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache, nil return customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][namespace].Cache, nil
} }
result, err := e.getAllMetrics(region) result, err := e.getAllMetrics(region, namespace)
if err != nil { if err != nil {
return []string{}, err return []string{}, err
} }
customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache = make([]string, 0) customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][namespace].Cache = make([]string, 0)
customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Expire = time.Now().Add(5 * time.Minute) customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][namespace].Expire = time.Now().Add(5 * time.Minute)
for _, metric := range result.Metrics { for _, metric := range result.Metrics {
for _, dimension := range metric.Dimensions { for _, dimension := range metric.Dimensions {
if isDuplicate(customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache, *dimension.Name) { if isDuplicate(customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][namespace].Cache, *dimension.Name) {
continue continue
} }
customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache = append( customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][namespace].Cache = append(
customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache, *dimension.Name) customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][namespace].Cache, *dimension.Name)
} }
} }
return customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache, nil return customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][namespace].Cache, nil
} }
func isDuplicate(nameList []string, target string) bool { func isDuplicate(nameList []string, target string) bool {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment