mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
re-implement get regions
This commit is contained in:
parent
0c95148486
commit
feed90c0e2
@ -80,10 +80,6 @@ func init() {
|
|||||||
"DescribeAlarmsForMetric": handleDescribeAlarmsForMetric,
|
"DescribeAlarmsForMetric": handleDescribeAlarmsForMetric,
|
||||||
"DescribeAlarmHistory": handleDescribeAlarmHistory,
|
"DescribeAlarmHistory": handleDescribeAlarmHistory,
|
||||||
"DescribeInstances": handleDescribeInstances,
|
"DescribeInstances": handleDescribeInstances,
|
||||||
"__GetRegions": handleGetRegions,
|
|
||||||
"__GetNamespaces": handleGetNamespaces,
|
|
||||||
"__GetMetrics": handleGetMetrics,
|
|
||||||
"__GetDimensions": handleGetDimensions,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,6 +53,20 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (e *CloudWatchExecutor) Execute(ctx context.Context, queries tsdb.QuerySlice, queryContext *tsdb.QueryContext) *tsdb.BatchResult {
|
func (e *CloudWatchExecutor) Execute(ctx context.Context, queries tsdb.QuerySlice, queryContext *tsdb.QueryContext) *tsdb.BatchResult {
|
||||||
|
var result *tsdb.BatchResult
|
||||||
|
queryType := queries[0].Model.Get("type").MustString()
|
||||||
|
switch queryType {
|
||||||
|
case "timeSeriesQuery":
|
||||||
|
result = e.executeTimeSeriesQuery(ctx, queries, queryContext)
|
||||||
|
break
|
||||||
|
case "metricFindQuery":
|
||||||
|
result = e.executeMetricFindQuery(ctx, queries, queryContext)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *CloudWatchExecutor) executeTimeSeriesQuery(ctx context.Context, queries tsdb.QuerySlice, queryContext *tsdb.QueryContext) *tsdb.BatchResult {
|
||||||
result := &tsdb.BatchResult{
|
result := &tsdb.BatchResult{
|
||||||
QueryResults: make(map[string]*tsdb.QueryResult),
|
QueryResults: make(map[string]*tsdb.QueryResult),
|
||||||
}
|
}
|
||||||
|
@ -1,24 +1,21 @@
|
|||||||
package cloudwatch
|
package cloudwatch
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"context"
|
||||||
"sort"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/aws/aws-sdk-go/aws"
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||||
"github.com/aws/aws-sdk-go/aws/awsutil"
|
"github.com/grafana/grafana/pkg/tsdb"
|
||||||
"github.com/aws/aws-sdk-go/aws/session"
|
|
||||||
"github.com/aws/aws-sdk-go/service/cloudwatch"
|
|
||||||
"github.com/grafana/grafana/pkg/metrics"
|
|
||||||
"github.com/grafana/grafana/pkg/middleware"
|
|
||||||
"github.com/grafana/grafana/pkg/util"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var metricsMap map[string][]string
|
var metricsMap map[string][]string
|
||||||
var dimensionsMap map[string][]string
|
var dimensionsMap map[string][]string
|
||||||
|
|
||||||
|
type suggestData struct {
|
||||||
|
Text string
|
||||||
|
Value string
|
||||||
|
}
|
||||||
|
|
||||||
type CustomMetricsCache struct {
|
type CustomMetricsCache struct {
|
||||||
Expire time.Time
|
Expire time.Time
|
||||||
Cache []string
|
Cache []string
|
||||||
@ -144,236 +141,279 @@ func init() {
|
|||||||
customMetricsDimensionsMap = make(map[string]map[string]map[string]*CustomMetricsCache)
|
customMetricsDimensionsMap = make(map[string]map[string]map[string]*CustomMetricsCache)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (e *CloudWatchExecutor) executeMetricFindQuery(ctx context.Context, queries tsdb.QuerySlice, queryContext *tsdb.QueryContext) *tsdb.BatchResult {
|
||||||
|
result := &tsdb.BatchResult{
|
||||||
|
QueryResults: make(map[string]*tsdb.QueryResult),
|
||||||
|
}
|
||||||
|
queryResult := &tsdb.QueryResult{Meta: simplejson.New(), RefId: queries[0].RefId}
|
||||||
|
|
||||||
|
parameters := queries[0].Model.Get("parameters")
|
||||||
|
subType := queries[0].Model.Get("subtype").MustString()
|
||||||
|
var data []suggestData
|
||||||
|
var err error
|
||||||
|
switch subType {
|
||||||
|
case "regions":
|
||||||
|
data, err = e.handleGetRegions(ctx, parameters, queryContext)
|
||||||
|
if err != nil {
|
||||||
|
queryResult.Error = err
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
transformToTable(data, queryResult)
|
||||||
|
result.QueryResults[queries[0].RefId] = queryResult
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func transformToTable(data []suggestData, result *tsdb.QueryResult) {
|
||||||
|
table := &tsdb.Table{
|
||||||
|
Columns: make([]tsdb.TableColumn, 2),
|
||||||
|
Rows: make([]tsdb.RowValues, 0),
|
||||||
|
}
|
||||||
|
table.Columns[0].Text = "text"
|
||||||
|
table.Columns[1].Text = "value"
|
||||||
|
|
||||||
|
for _, r := range data {
|
||||||
|
values := make([]interface{}, 2)
|
||||||
|
values[0] = r.Text
|
||||||
|
values[1] = r.Value
|
||||||
|
table.Rows = append(table.Rows, values)
|
||||||
|
}
|
||||||
|
result.Tables = append(result.Tables, table)
|
||||||
|
result.Meta.Set("rowCount", len(data))
|
||||||
|
}
|
||||||
|
|
||||||
// Whenever this list is updated, frontend list should also be updated.
|
// Whenever this list is updated, frontend list should also be updated.
|
||||||
// Please update the region list in public/app/plugins/datasource/cloudwatch/partials/config.html
|
// Please update the region list in public/app/plugins/datasource/cloudwatch/partials/config.html
|
||||||
func handleGetRegions(req *cwRequest, c *middleware.Context) {
|
func (e *CloudWatchExecutor) handleGetRegions(ctx context.Context, parameters *simplejson.Json, queryContext *tsdb.QueryContext) ([]suggestData, error) {
|
||||||
regions := []string{
|
regions := []string{
|
||||||
"ap-northeast-1", "ap-northeast-2", "ap-southeast-1", "ap-southeast-2", "ap-south-1", "ca-central-1", "cn-north-1",
|
"ap-northeast-1", "ap-northeast-2", "ap-southeast-1", "ap-southeast-2", "ap-south-1", "ca-central-1", "cn-north-1",
|
||||||
"eu-central-1", "eu-west-1", "eu-west-2", "sa-east-1", "us-east-1", "us-east-2", "us-gov-west-1", "us-west-1", "us-west-2",
|
"eu-central-1", "eu-west-1", "eu-west-2", "sa-east-1", "us-east-1", "us-east-2", "us-gov-west-1", "us-west-1", "us-west-2",
|
||||||
}
|
}
|
||||||
|
|
||||||
result := []interface{}{}
|
result := make([]suggestData, 0)
|
||||||
for _, region := range regions {
|
for _, region := range regions {
|
||||||
result = append(result, util.DynMap{"text": region, "value": region})
|
result = append(result, suggestData{Text: region, Value: region})
|
||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(200, result)
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleGetNamespaces(req *cwRequest, c *middleware.Context) {
|
//func handleGetNamespaces(req *cwRequest, c *middleware.Context) {
|
||||||
keys := []string{}
|
// keys := []string{}
|
||||||
for key := range metricsMap {
|
// for key := range metricsMap {
|
||||||
keys = append(keys, key)
|
// keys = append(keys, key)
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
customNamespaces := req.DataSource.JsonData.Get("customMetricsNamespaces").MustString()
|
// customNamespaces := req.DataSource.JsonData.Get("customMetricsNamespaces").MustString()
|
||||||
if customNamespaces != "" {
|
// if customNamespaces != "" {
|
||||||
keys = append(keys, strings.Split(customNamespaces, ",")...)
|
// for _, key := range strings.Split(customNamespaces, ",") {
|
||||||
}
|
// keys = append(keys, key)
|
||||||
|
// }
|
||||||
sort.Sort(sort.StringSlice(keys))
|
// }
|
||||||
|
//
|
||||||
result := []interface{}{}
|
// sort.Sort(sort.StringSlice(keys))
|
||||||
for _, key := range keys {
|
//
|
||||||
result = append(result, util.DynMap{"text": key, "value": key})
|
// result := []interface{}{}
|
||||||
}
|
// for _, key := range keys {
|
||||||
|
// result = append(result, util.DynMap{"text": key, "value": key})
|
||||||
c.JSON(200, result)
|
// }
|
||||||
}
|
//
|
||||||
|
// c.JSON(200, result)
|
||||||
func handleGetMetrics(req *cwRequest, c *middleware.Context) {
|
//}
|
||||||
reqParam := &struct {
|
//
|
||||||
Parameters struct {
|
//func handleGetMetrics(req *cwRequest, c *middleware.Context) {
|
||||||
Namespace string `json:"namespace"`
|
// reqParam := &struct {
|
||||||
} `json:"parameters"`
|
// Parameters struct {
|
||||||
}{}
|
// Namespace string `json:"namespace"`
|
||||||
|
// } `json:"parameters"`
|
||||||
json.Unmarshal(req.Body, reqParam)
|
// }{}
|
||||||
|
//
|
||||||
var namespaceMetrics []string
|
// json.Unmarshal(req.Body, reqParam)
|
||||||
if !isCustomMetrics(reqParam.Parameters.Namespace) {
|
//
|
||||||
var exists bool
|
// var namespaceMetrics []string
|
||||||
if namespaceMetrics, exists = metricsMap[reqParam.Parameters.Namespace]; !exists {
|
// if !isCustomMetrics(reqParam.Parameters.Namespace) {
|
||||||
c.JsonApiErr(404, "Unable to find namespace "+reqParam.Parameters.Namespace, nil)
|
// var exists bool
|
||||||
return
|
// if namespaceMetrics, exists = metricsMap[reqParam.Parameters.Namespace]; !exists {
|
||||||
}
|
// c.JsonApiErr(404, "Unable to find namespace "+reqParam.Parameters.Namespace, nil)
|
||||||
} else {
|
// return
|
||||||
var err error
|
// }
|
||||||
cwData := req.GetDatasourceInfo()
|
// } else {
|
||||||
cwData.Namespace = reqParam.Parameters.Namespace
|
// var err error
|
||||||
|
// cwData := req.GetDatasourceInfo()
|
||||||
if namespaceMetrics, err = getMetricsForCustomMetrics(cwData, getAllMetrics); err != nil {
|
// cwData.Namespace = reqParam.Parameters.Namespace
|
||||||
c.JsonApiErr(500, "Unable to call AWS API", err)
|
//
|
||||||
return
|
// if namespaceMetrics, err = getMetricsForCustomMetrics(cwData, getAllMetrics); err != nil {
|
||||||
}
|
// c.JsonApiErr(500, "Unable to call AWS API", err)
|
||||||
}
|
// return
|
||||||
sort.Sort(sort.StringSlice(namespaceMetrics))
|
// }
|
||||||
|
// }
|
||||||
result := []interface{}{}
|
// sort.Sort(sort.StringSlice(namespaceMetrics))
|
||||||
for _, name := range namespaceMetrics {
|
//
|
||||||
result = append(result, util.DynMap{"text": name, "value": name})
|
// result := []interface{}{}
|
||||||
}
|
// for _, name := range namespaceMetrics {
|
||||||
|
// result = append(result, util.DynMap{"text": name, "value": name})
|
||||||
c.JSON(200, result)
|
// }
|
||||||
}
|
//
|
||||||
|
// c.JSON(200, result)
|
||||||
func handleGetDimensions(req *cwRequest, c *middleware.Context) {
|
//}
|
||||||
reqParam := &struct {
|
//
|
||||||
Parameters struct {
|
//func handleGetDimensions(req *cwRequest, c *middleware.Context) {
|
||||||
Namespace string `json:"namespace"`
|
// reqParam := &struct {
|
||||||
} `json:"parameters"`
|
// Parameters struct {
|
||||||
}{}
|
// Namespace string `json:"namespace"`
|
||||||
|
// } `json:"parameters"`
|
||||||
json.Unmarshal(req.Body, reqParam)
|
// }{}
|
||||||
|
//
|
||||||
var dimensionValues []string
|
// json.Unmarshal(req.Body, reqParam)
|
||||||
if !isCustomMetrics(reqParam.Parameters.Namespace) {
|
//
|
||||||
var exists bool
|
// var dimensionValues []string
|
||||||
if dimensionValues, exists = dimensionsMap[reqParam.Parameters.Namespace]; !exists {
|
// if !isCustomMetrics(reqParam.Parameters.Namespace) {
|
||||||
c.JsonApiErr(404, "Unable to find dimension "+reqParam.Parameters.Namespace, nil)
|
// var exists bool
|
||||||
return
|
// if dimensionValues, exists = dimensionsMap[reqParam.Parameters.Namespace]; !exists {
|
||||||
}
|
// c.JsonApiErr(404, "Unable to find dimension "+reqParam.Parameters.Namespace, nil)
|
||||||
} else {
|
// return
|
||||||
var err error
|
// }
|
||||||
dsInfo := req.GetDatasourceInfo()
|
// } else {
|
||||||
dsInfo.Namespace = reqParam.Parameters.Namespace
|
// var err error
|
||||||
|
// dsInfo := req.GetDatasourceInfo()
|
||||||
if dimensionValues, err = getDimensionsForCustomMetrics(dsInfo, getAllMetrics); err != nil {
|
// dsInfo.Namespace = reqParam.Parameters.Namespace
|
||||||
c.JsonApiErr(500, "Unable to call AWS API", err)
|
//
|
||||||
return
|
// if dimensionValues, err = getDimensionsForCustomMetrics(dsInfo, getAllMetrics); err != nil {
|
||||||
}
|
// c.JsonApiErr(500, "Unable to call AWS API", err)
|
||||||
}
|
// return
|
||||||
sort.Sort(sort.StringSlice(dimensionValues))
|
// }
|
||||||
|
// }
|
||||||
result := []interface{}{}
|
// sort.Sort(sort.StringSlice(dimensionValues))
|
||||||
for _, name := range dimensionValues {
|
//
|
||||||
result = append(result, util.DynMap{"text": name, "value": name})
|
// result := []interface{}{}
|
||||||
}
|
// for _, name := range dimensionValues {
|
||||||
|
// result = append(result, util.DynMap{"text": name, "value": name})
|
||||||
c.JSON(200, result)
|
// }
|
||||||
}
|
//
|
||||||
|
// c.JSON(200, result)
|
||||||
func getAllMetrics(cwData *DatasourceInfo) (cloudwatch.ListMetricsOutput, error) {
|
//}
|
||||||
creds, err := GetCredentials(cwData)
|
//
|
||||||
if err != nil {
|
//func getAllMetrics(cwData *DatasourceInfo) (cloudwatch.ListMetricsOutput, error) {
|
||||||
return cloudwatch.ListMetricsOutput{}, err
|
// creds, err := GetCredentials(cwData)
|
||||||
}
|
// if err != nil {
|
||||||
cfg := &aws.Config{
|
// return cloudwatch.ListMetricsOutput{}, err
|
||||||
Region: aws.String(cwData.Region),
|
// }
|
||||||
Credentials: creds,
|
// cfg := &aws.Config{
|
||||||
}
|
// Region: aws.String(cwData.Region),
|
||||||
sess, err := session.NewSession(cfg)
|
// Credentials: creds,
|
||||||
if err != nil {
|
// }
|
||||||
return cloudwatch.ListMetricsOutput{}, err
|
// sess, err := session.NewSession(cfg)
|
||||||
}
|
// if err != nil {
|
||||||
svc := cloudwatch.New(sess, cfg)
|
// return cloudwatch.ListMetricsOutput{}, err
|
||||||
|
// }
|
||||||
params := &cloudwatch.ListMetricsInput{
|
// svc := cloudwatch.New(sess, cfg)
|
||||||
Namespace: aws.String(cwData.Namespace),
|
//
|
||||||
}
|
// params := &cloudwatch.ListMetricsInput{
|
||||||
|
// Namespace: aws.String(cwData.Namespace),
|
||||||
var resp cloudwatch.ListMetricsOutput
|
// }
|
||||||
err = svc.ListMetricsPages(params,
|
//
|
||||||
func(page *cloudwatch.ListMetricsOutput, lastPage bool) bool {
|
// var resp cloudwatch.ListMetricsOutput
|
||||||
metrics.M_Aws_CloudWatch_ListMetrics.Inc()
|
// err = svc.ListMetricsPages(params,
|
||||||
metrics, _ := awsutil.ValuesAtPath(page, "Metrics")
|
// func(page *cloudwatch.ListMetricsOutput, lastPage bool) bool {
|
||||||
for _, metric := range metrics {
|
// metrics.M_Aws_CloudWatch_ListMetrics.Inc(1)
|
||||||
resp.Metrics = append(resp.Metrics, metric.(*cloudwatch.Metric))
|
// metrics, _ := awsutil.ValuesAtPath(page, "Metrics")
|
||||||
}
|
// for _, metric := range metrics {
|
||||||
return !lastPage
|
// resp.Metrics = append(resp.Metrics, metric.(*cloudwatch.Metric))
|
||||||
})
|
// }
|
||||||
if err != nil {
|
// return !lastPage
|
||||||
return resp, err
|
// })
|
||||||
}
|
// if err != nil {
|
||||||
|
// return resp, err
|
||||||
return resp, nil
|
// }
|
||||||
}
|
//
|
||||||
|
// return resp, nil
|
||||||
var metricsCacheLock sync.Mutex
|
//}
|
||||||
|
//
|
||||||
func getMetricsForCustomMetrics(dsInfo *DatasourceInfo, getAllMetrics func(*DatasourceInfo) (cloudwatch.ListMetricsOutput, error)) ([]string, error) {
|
//var metricsCacheLock sync.Mutex
|
||||||
metricsCacheLock.Lock()
|
//
|
||||||
defer metricsCacheLock.Unlock()
|
//func getMetricsForCustomMetrics(dsInfo *DatasourceInfo, getAllMetrics func(*DatasourceInfo) (cloudwatch.ListMetricsOutput, error)) ([]string, error) {
|
||||||
|
// metricsCacheLock.Lock()
|
||||||
if _, ok := customMetricsMetricsMap[dsInfo.Profile]; !ok {
|
// defer metricsCacheLock.Unlock()
|
||||||
customMetricsMetricsMap[dsInfo.Profile] = make(map[string]map[string]*CustomMetricsCache)
|
//
|
||||||
}
|
// if _, ok := customMetricsMetricsMap[dsInfo.Profile]; !ok {
|
||||||
if _, ok := customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region]; !ok {
|
// customMetricsMetricsMap[dsInfo.Profile] = make(map[string]map[string]*CustomMetricsCache)
|
||||||
customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region] = make(map[string]*CustomMetricsCache)
|
// }
|
||||||
}
|
// if _, ok := customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region]; !ok {
|
||||||
if _, ok := customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace]; !ok {
|
// customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region] = make(map[string]*CustomMetricsCache)
|
||||||
customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace] = &CustomMetricsCache{}
|
// }
|
||||||
customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.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[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Expire.After(time.Now()) {
|
// }
|
||||||
return customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache, nil
|
//
|
||||||
}
|
// if customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Expire.After(time.Now()) {
|
||||||
result, err := getAllMetrics(dsInfo)
|
// return customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache, nil
|
||||||
if err != nil {
|
// }
|
||||||
return []string{}, err
|
// result, err := getAllMetrics(dsInfo)
|
||||||
}
|
// if err != nil {
|
||||||
customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache = make([]string, 0)
|
// return []string{}, err
|
||||||
customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Expire = time.Now().Add(5 * time.Minute)
|
// }
|
||||||
|
// customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache = make([]string, 0)
|
||||||
for _, metric := range result.Metrics {
|
// customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Expire = time.Now().Add(5 * time.Minute)
|
||||||
if isDuplicate(customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache, *metric.MetricName) {
|
//
|
||||||
continue
|
// for _, metric := range result.Metrics {
|
||||||
}
|
// if isDuplicate(customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache, *metric.MetricName) {
|
||||||
customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache = append(customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache, *metric.MetricName)
|
// continue
|
||||||
}
|
// }
|
||||||
|
// customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache = append(customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache, *metric.MetricName)
|
||||||
return customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache, nil
|
// }
|
||||||
}
|
//
|
||||||
|
// return customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache, nil
|
||||||
var dimensionsCacheLock sync.Mutex
|
//}
|
||||||
|
//
|
||||||
func getDimensionsForCustomMetrics(dsInfo *DatasourceInfo, getAllMetrics func(*DatasourceInfo) (cloudwatch.ListMetricsOutput, error)) ([]string, error) {
|
//var dimensionsCacheLock sync.Mutex
|
||||||
dimensionsCacheLock.Lock()
|
//
|
||||||
defer dimensionsCacheLock.Unlock()
|
//func getDimensionsForCustomMetrics(dsInfo *DatasourceInfo, getAllMetrics func(*DatasourceInfo) (cloudwatch.ListMetricsOutput, error)) ([]string, error) {
|
||||||
|
// dimensionsCacheLock.Lock()
|
||||||
if _, ok := customMetricsDimensionsMap[dsInfo.Profile]; !ok {
|
// defer dimensionsCacheLock.Unlock()
|
||||||
customMetricsDimensionsMap[dsInfo.Profile] = make(map[string]map[string]*CustomMetricsCache)
|
//
|
||||||
}
|
// if _, ok := customMetricsDimensionsMap[dsInfo.Profile]; !ok {
|
||||||
if _, ok := customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region]; !ok {
|
// customMetricsDimensionsMap[dsInfo.Profile] = make(map[string]map[string]*CustomMetricsCache)
|
||||||
customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region] = make(map[string]*CustomMetricsCache)
|
// }
|
||||||
}
|
// if _, ok := customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region]; !ok {
|
||||||
if _, ok := customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace]; !ok {
|
// customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region] = make(map[string]*CustomMetricsCache)
|
||||||
customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace] = &CustomMetricsCache{}
|
// }
|
||||||
customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.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[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Expire.After(time.Now()) {
|
// }
|
||||||
return customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache, nil
|
//
|
||||||
}
|
// if customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Expire.After(time.Now()) {
|
||||||
result, err := getAllMetrics(dsInfo)
|
// return customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache, nil
|
||||||
if err != nil {
|
// }
|
||||||
return []string{}, err
|
// result, err := getAllMetrics(dsInfo)
|
||||||
}
|
// if err != nil {
|
||||||
customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache = make([]string, 0)
|
// return []string{}, err
|
||||||
customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Expire = time.Now().Add(5 * time.Minute)
|
// }
|
||||||
|
// customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache = make([]string, 0)
|
||||||
for _, metric := range result.Metrics {
|
// customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Expire = time.Now().Add(5 * time.Minute)
|
||||||
for _, dimension := range metric.Dimensions {
|
//
|
||||||
if isDuplicate(customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache, *dimension.Name) {
|
// for _, metric := range result.Metrics {
|
||||||
continue
|
// for _, dimension := range metric.Dimensions {
|
||||||
}
|
// if isDuplicate(customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache, *dimension.Name) {
|
||||||
customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache = append(customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache, *dimension.Name)
|
// continue
|
||||||
}
|
// }
|
||||||
}
|
// customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache = append(customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache, *dimension.Name)
|
||||||
|
// }
|
||||||
return customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache, nil
|
// }
|
||||||
}
|
//
|
||||||
|
// return customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache, nil
|
||||||
func isDuplicate(nameList []string, target string) bool {
|
//}
|
||||||
for _, name := range nameList {
|
//
|
||||||
if name == target {
|
//func isDuplicate(nameList []string, target string) bool {
|
||||||
return true
|
// for _, name := range nameList {
|
||||||
}
|
// if name == target {
|
||||||
}
|
// return true
|
||||||
return false
|
// }
|
||||||
}
|
// }
|
||||||
|
// return false
|
||||||
func isCustomMetrics(namespace string) bool {
|
//}
|
||||||
return strings.Index(namespace, "AWS/") != 0
|
//
|
||||||
}
|
//func isCustomMetrics(namespace string) bool {
|
||||||
|
// return strings.Index(namespace, "AWS/") != 0
|
||||||
|
//}
|
||||||
|
@ -11,7 +11,7 @@ function (angular, _, moment, dateMath, kbn, templatingVariable, CloudWatchAnnot
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
/** @ngInject */
|
/** @ngInject */
|
||||||
function CloudWatchDatasource(instanceSettings, $q, backendSrv, templateSrv) {
|
function CloudWatchDatasource(instanceSettings, $q, backendSrv, templateSrv, timeSrv) {
|
||||||
this.type = 'cloudwatch';
|
this.type = 'cloudwatch';
|
||||||
this.name = instanceSettings.name;
|
this.name = instanceSettings.name;
|
||||||
this.supportMetrics = true;
|
this.supportMetrics = true;
|
||||||
@ -133,7 +133,21 @@ function (angular, _, moment, dateMath, kbn, templatingVariable, CloudWatchAnnot
|
|||||||
};
|
};
|
||||||
|
|
||||||
this.getRegions = function() {
|
this.getRegions = function() {
|
||||||
return this.awsRequest({action: '__GetRegions'});
|
var range = timeSrv.timeRange();
|
||||||
|
return backendSrv.post('/api/tsdb/query', {
|
||||||
|
from: range.from,
|
||||||
|
to: range.to,
|
||||||
|
queries: [
|
||||||
|
{
|
||||||
|
refId: 'metricFindQuery',
|
||||||
|
intervalMs: 1, // dummy
|
||||||
|
maxDataPoints: 1, // dummy
|
||||||
|
datasourceId: this.instanceSettings.id,
|
||||||
|
type: 'metricFindQuery',
|
||||||
|
subtype: 'regions'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
this.getNamespaces = function() {
|
this.getNamespaces = function() {
|
||||||
@ -200,6 +214,14 @@ function (angular, _, moment, dateMath, kbn, templatingVariable, CloudWatchAnnot
|
|||||||
var namespace;
|
var namespace;
|
||||||
var metricName;
|
var metricName;
|
||||||
|
|
||||||
|
var transformSuggestDataFromTable = function(suggestData) {
|
||||||
|
return _.map(suggestData.results['metricFindQuery'].tables[0].rows, function (v) {
|
||||||
|
return {
|
||||||
|
text: v[0],
|
||||||
|
value: v[1]
|
||||||
|
};
|
||||||
|
});
|
||||||
|
};
|
||||||
var transformSuggestData = function(suggestData) {
|
var transformSuggestData = function(suggestData) {
|
||||||
return _.map(suggestData, function(v) {
|
return _.map(suggestData, function(v) {
|
||||||
return { text: v };
|
return { text: v };
|
||||||
@ -208,7 +230,7 @@ function (angular, _, moment, dateMath, kbn, templatingVariable, CloudWatchAnnot
|
|||||||
|
|
||||||
var regionQuery = query.match(/^regions\(\)/);
|
var regionQuery = query.match(/^regions\(\)/);
|
||||||
if (regionQuery) {
|
if (regionQuery) {
|
||||||
return this.getRegions();
|
return this.getRegions().then(function (r) { return transformSuggestDataFromTable(r); });
|
||||||
}
|
}
|
||||||
|
|
||||||
var namespaceQuery = query.match(/^namespaces\(\)/);
|
var namespaceQuery = query.match(/^namespaces\(\)/);
|
||||||
|
Loading…
Reference in New Issue
Block a user