|
|
@@ -192,8 +192,10 @@ func handleGetMetrics(req *cwRequest, c *middleware.Context) {
|
|
|
}
|
|
|
} else {
|
|
|
var err error
|
|
|
- assumeRoleArn := req.DataSource.JsonData.Get("assumeRoleArn").MustString()
|
|
|
- if namespaceMetrics, err = getMetricsForCustomMetrics(req.Region, reqParam.Parameters.Namespace, req.DataSource.Database, assumeRoleArn, getAllMetrics); err != nil {
|
|
|
+ cwData := req.GetDatasourceInfo()
|
|
|
+ cwData.Namespace = reqParam.Parameters.Namespace
|
|
|
+
|
|
|
+ if namespaceMetrics, err = getMetricsForCustomMetrics(cwData, getAllMetrics); err != nil {
|
|
|
c.JsonApiErr(500, "Unable to call AWS API", err)
|
|
|
return
|
|
|
}
|
|
|
@@ -226,8 +228,10 @@ func handleGetDimensions(req *cwRequest, c *middleware.Context) {
|
|
|
}
|
|
|
} else {
|
|
|
var err error
|
|
|
- assumeRoleArn := req.DataSource.JsonData.Get("assumeRoleArn").MustString()
|
|
|
- if dimensionValues, err = getDimensionsForCustomMetrics(req.Region, reqParam.Parameters.Namespace, req.DataSource.Database, assumeRoleArn, getAllMetrics); err != nil {
|
|
|
+ dsInfo := req.GetDatasourceInfo()
|
|
|
+ dsInfo.Namespace = reqParam.Parameters.Namespace
|
|
|
+
|
|
|
+ if dimensionValues, err = getDimensionsForCustomMetrics(dsInfo, getAllMetrics); err != nil {
|
|
|
c.JsonApiErr(500, "Unable to call AWS API", err)
|
|
|
return
|
|
|
}
|
|
|
@@ -242,16 +246,16 @@ func handleGetDimensions(req *cwRequest, c *middleware.Context) {
|
|
|
c.JSON(200, result)
|
|
|
}
|
|
|
|
|
|
-func getAllMetrics(region string, namespace string, database string, assumeRoleArn string) (cloudwatch.ListMetricsOutput, error) {
|
|
|
+func getAllMetrics(cwData *datasourceInfo) (cloudwatch.ListMetricsOutput, error) {
|
|
|
cfg := &aws.Config{
|
|
|
- Region: aws.String(region),
|
|
|
- Credentials: getCredentials(database, region, assumeRoleArn),
|
|
|
+ Region: aws.String(cwData.Region),
|
|
|
+ Credentials: getCredentials(cwData),
|
|
|
}
|
|
|
|
|
|
svc := cloudwatch.New(session.New(cfg), cfg)
|
|
|
|
|
|
params := &cloudwatch.ListMetricsInput{
|
|
|
- Namespace: aws.String(namespace),
|
|
|
+ Namespace: aws.String(cwData.Namespace),
|
|
|
}
|
|
|
|
|
|
var resp cloudwatch.ListMetricsOutput
|
|
|
@@ -272,8 +276,8 @@ func getAllMetrics(region string, namespace string, database string, assumeRoleA
|
|
|
|
|
|
var metricsCacheLock sync.Mutex
|
|
|
|
|
|
-func getMetricsForCustomMetrics(region string, namespace string, database string, assumeRoleArn string, getAllMetrics func(string, string, string, string) (cloudwatch.ListMetricsOutput, error)) ([]string, error) {
|
|
|
- result, err := getAllMetrics(region, namespace, database, assumeRoleArn)
|
|
|
+func getMetricsForCustomMetrics(dsInfo *datasourceInfo, getAllMetrics func(*datasourceInfo) (cloudwatch.ListMetricsOutput, error)) ([]string, error) {
|
|
|
+ result, err := getAllMetrics(dsInfo)
|
|
|
if err != nil {
|
|
|
return []string{}, err
|
|
|
}
|
|
|
@@ -281,37 +285,37 @@ func getMetricsForCustomMetrics(region string, namespace string, database string
|
|
|
metricsCacheLock.Lock()
|
|
|
defer metricsCacheLock.Unlock()
|
|
|
|
|
|
- if _, ok := customMetricsMetricsMap[database]; !ok {
|
|
|
- customMetricsMetricsMap[database] = make(map[string]map[string]*CustomMetricsCache)
|
|
|
+ if _, ok := customMetricsMetricsMap[dsInfo.Profile]; !ok {
|
|
|
+ customMetricsMetricsMap[dsInfo.Profile] = make(map[string]map[string]*CustomMetricsCache)
|
|
|
}
|
|
|
- if _, ok := customMetricsMetricsMap[database][region]; !ok {
|
|
|
- customMetricsMetricsMap[database][region] = make(map[string]*CustomMetricsCache)
|
|
|
+ if _, ok := customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region]; !ok {
|
|
|
+ customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region] = make(map[string]*CustomMetricsCache)
|
|
|
}
|
|
|
- if _, ok := customMetricsMetricsMap[database][region][namespace]; !ok {
|
|
|
- customMetricsMetricsMap[database][region][namespace] = &CustomMetricsCache{}
|
|
|
- customMetricsMetricsMap[database][region][namespace].Cache = make([]string, 0)
|
|
|
+ if _, ok := customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace]; !ok {
|
|
|
+ customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace] = &CustomMetricsCache{}
|
|
|
+ customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache = make([]string, 0)
|
|
|
}
|
|
|
|
|
|
- if customMetricsMetricsMap[database][region][namespace].Expire.After(time.Now()) {
|
|
|
- return customMetricsMetricsMap[database][region][namespace].Cache, nil
|
|
|
+ if customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Expire.After(time.Now()) {
|
|
|
+ return customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache, nil
|
|
|
}
|
|
|
- customMetricsMetricsMap[database][region][namespace].Cache = make([]string, 0)
|
|
|
- customMetricsMetricsMap[database][region][namespace].Expire = time.Now().Add(5 * time.Minute)
|
|
|
+ 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)
|
|
|
|
|
|
for _, metric := range result.Metrics {
|
|
|
- if isDuplicate(customMetricsMetricsMap[database][region][namespace].Cache, *metric.MetricName) {
|
|
|
+ if isDuplicate(customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache, *metric.MetricName) {
|
|
|
continue
|
|
|
}
|
|
|
- customMetricsMetricsMap[database][region][namespace].Cache = append(customMetricsMetricsMap[database][region][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[database][region][namespace].Cache, nil
|
|
|
+ return customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache, nil
|
|
|
}
|
|
|
|
|
|
var dimensionsCacheLock sync.Mutex
|
|
|
|
|
|
-func getDimensionsForCustomMetrics(region string, namespace string, database string, assumeRoleArn string, getAllMetrics func(string, string, string, string) (cloudwatch.ListMetricsOutput, error)) ([]string, error) {
|
|
|
- result, err := getAllMetrics(region, namespace, database, assumeRoleArn)
|
|
|
+func getDimensionsForCustomMetrics(dsInfo *datasourceInfo, getAllMetrics func(*datasourceInfo) (cloudwatch.ListMetricsOutput, error)) ([]string, error) {
|
|
|
+ result, err := getAllMetrics(dsInfo)
|
|
|
if err != nil {
|
|
|
return []string{}, err
|
|
|
}
|
|
|
@@ -319,33 +323,33 @@ func getDimensionsForCustomMetrics(region string, namespace string, database str
|
|
|
dimensionsCacheLock.Lock()
|
|
|
defer dimensionsCacheLock.Unlock()
|
|
|
|
|
|
- if _, ok := customMetricsDimensionsMap[database]; !ok {
|
|
|
- customMetricsDimensionsMap[database] = make(map[string]map[string]*CustomMetricsCache)
|
|
|
+ if _, ok := customMetricsDimensionsMap[dsInfo.Profile]; !ok {
|
|
|
+ customMetricsDimensionsMap[dsInfo.Profile] = make(map[string]map[string]*CustomMetricsCache)
|
|
|
}
|
|
|
- if _, ok := customMetricsDimensionsMap[database][region]; !ok {
|
|
|
- customMetricsDimensionsMap[database][region] = make(map[string]*CustomMetricsCache)
|
|
|
+ if _, ok := customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region]; !ok {
|
|
|
+ customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region] = make(map[string]*CustomMetricsCache)
|
|
|
}
|
|
|
- if _, ok := customMetricsDimensionsMap[database][region][namespace]; !ok {
|
|
|
- customMetricsDimensionsMap[database][region][namespace] = &CustomMetricsCache{}
|
|
|
- customMetricsDimensionsMap[database][region][namespace].Cache = make([]string, 0)
|
|
|
+ if _, ok := customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace]; !ok {
|
|
|
+ customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace] = &CustomMetricsCache{}
|
|
|
+ customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache = make([]string, 0)
|
|
|
}
|
|
|
|
|
|
- if customMetricsDimensionsMap[database][region][namespace].Expire.After(time.Now()) {
|
|
|
- return customMetricsDimensionsMap[database][region][namespace].Cache, nil
|
|
|
+ if customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Expire.After(time.Now()) {
|
|
|
+ return customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache, nil
|
|
|
}
|
|
|
- customMetricsDimensionsMap[database][region][namespace].Cache = make([]string, 0)
|
|
|
- customMetricsDimensionsMap[database][region][namespace].Expire = time.Now().Add(5 * time.Minute)
|
|
|
+ customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache = make([]string, 0)
|
|
|
+ customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Expire = time.Now().Add(5 * time.Minute)
|
|
|
|
|
|
for _, metric := range result.Metrics {
|
|
|
for _, dimension := range metric.Dimensions {
|
|
|
- if isDuplicate(customMetricsDimensionsMap[database][region][namespace].Cache, *dimension.Name) {
|
|
|
+ if isDuplicate(customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache, *dimension.Name) {
|
|
|
continue
|
|
|
}
|
|
|
- customMetricsDimensionsMap[database][region][namespace].Cache = append(customMetricsDimensionsMap[database][region][namespace].Cache, *dimension.Name)
|
|
|
+ customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache = append(customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache, *dimension.Name)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- return customMetricsDimensionsMap[database][region][namespace].Cache, nil
|
|
|
+ return customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache, nil
|
|
|
}
|
|
|
|
|
|
func isDuplicate(nameList []string, target string) bool {
|