metric_find_query.go 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. package cloudwatch
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "reflect"
  7. "sort"
  8. "strings"
  9. "sync"
  10. "time"
  11. "github.com/aws/aws-sdk-go/aws"
  12. "github.com/aws/aws-sdk-go/aws/awsutil"
  13. "github.com/aws/aws-sdk-go/aws/session"
  14. "github.com/aws/aws-sdk-go/service/cloudwatch"
  15. "github.com/aws/aws-sdk-go/service/ec2"
  16. "github.com/grafana/grafana/pkg/components/simplejson"
  17. "github.com/grafana/grafana/pkg/metrics"
  18. "github.com/grafana/grafana/pkg/tsdb"
  19. )
  20. var metricsMap map[string][]string
  21. var dimensionsMap map[string][]string
  22. type suggestData struct {
  23. Text string
  24. Value string
  25. }
  26. type CustomMetricsCache struct {
  27. Expire time.Time
  28. Cache []string
  29. }
  30. var customMetricsMetricsMap map[string]map[string]map[string]*CustomMetricsCache
  31. var customMetricsDimensionsMap map[string]map[string]map[string]*CustomMetricsCache
  32. func init() {
  33. metricsMap = map[string][]string{
  34. "AWS/AmazonMQ": {"CpuUtilization", "HeapUsage", "NetworkIn", "NetworkOut", "TotalMessageCount", "ConsumerCount", "EnqueueCount", "EnqueueTime", "ExpiredCount", "InflightCount", "DispatchCount", "DequeueCount", "MemoryUsage", "ProducerCount", "QueueSize"},
  35. "AWS/ApiGateway": {"4XXError", "5XXError", "CacheHitCount", "CacheMissCount", "Count", "IntegrationLatency", "Latency"},
  36. "AWS/ApplicationELB": {"ActiveConnectionCount", "ClientTLSNegotiationErrorCount", "HealthyHostCount", "HTTPCode_ELB_4XX_Count", "HTTPCode_ELB_5XX_Count", "HTTPCode_Target_2XX_Count", "HTTPCode_Target_3XX_Count", "HTTPCode_Target_4XX_Count", "HTTPCode_Target_5XX_Count", "IPv6ProcessedBytes", "IPv6RequestCount", "NewConnectionCount", "ProcessedBytes", "RejectedConnectionCount", "RequestCount", "RequestCountPerTarget", "TargetConnectionErrorCount", "TargetResponseTime", "TargetTLSNegotiationErrorCount", "UnHealthyHostCount"},
  37. "AWS/AutoScaling": {"GroupMinSize", "GroupMaxSize", "GroupDesiredCapacity", "GroupInServiceInstances", "GroupPendingInstances", "GroupStandbyInstances", "GroupTerminatingInstances", "GroupTotalInstances"},
  38. "AWS/Billing": {"EstimatedCharges"},
  39. "AWS/CloudFront": {"Requests", "BytesDownloaded", "BytesUploaded", "TotalErrorRate", "4xxErrorRate", "5xxErrorRate"},
  40. "AWS/CloudSearch": {"SuccessfulRequests", "SearchableDocuments", "IndexUtilization", "Partitions"},
  41. "AWS/DMS": {"FreeableMemory", "WriteIOPS", "ReadIOPS", "WriteThroughput", "ReadThroughput", "WriteLatency", "ReadLatency", "SwapUsage", "NetworkTransmitThroughput", "NetworkReceiveThroughput", "FullLoadThroughputBandwidthSource", "FullLoadThroughputBandwidthTarget", "FullLoadThroughputRowsSource", "FullLoadThroughputRowsTarget", "CDCIncomingChanges", "CDCChangesMemorySource", "CDCChangesMemoryTarget", "CDCChangesDiskSource", "CDCChangesDiskTarget", "CDCThroughputBandwidthTarget", "CDCThroughputRowsSource", "CDCThroughputRowsTarget", "CDCLatencySource", "CDCLatencyTarget"},
  42. "AWS/DynamoDB": {"ConditionalCheckFailedRequests", "ConsumedReadCapacityUnits", "ConsumedWriteCapacityUnits", "OnlineIndexConsumedWriteCapacity", "OnlineIndexPercentageProgress", "OnlineIndexThrottleEvents", "ProvisionedReadCapacityUnits", "ProvisionedWriteCapacityUnits", "ReadThrottleEvents", "ReturnedBytes", "ReturnedItemCount", "ReturnedRecordsCount", "SuccessfulRequestLatency", "SystemErrors", "TimeToLiveDeletedItemCount", "ThrottledRequests", "UserErrors", "WriteThrottleEvents"},
  43. "AWS/EBS": {"VolumeReadBytes", "VolumeWriteBytes", "VolumeReadOps", "VolumeWriteOps", "VolumeTotalReadTime", "VolumeTotalWriteTime", "VolumeIdleTime", "VolumeQueueLength", "VolumeThroughputPercentage", "VolumeConsumedReadWriteOps", "BurstBalance"},
  44. "AWS/EC2": {"CPUCreditUsage", "CPUCreditBalance", "CPUUtilization", "DiskReadOps", "DiskWriteOps", "DiskReadBytes", "DiskWriteBytes", "NetworkIn", "NetworkOut", "NetworkPacketsIn", "NetworkPacketsOut", "StatusCheckFailed", "StatusCheckFailed_Instance", "StatusCheckFailed_System"},
  45. "AWS/EC2Spot": {"AvailableInstancePoolsCount", "BidsSubmittedForCapacity", "EligibleInstancePoolCount", "FulfilledCapacity", "MaxPercentCapacityAllocation", "PendingCapacity", "PercentCapacityAllocation", "TargetCapacity", "TerminatingCapacity"},
  46. "AWS/ECS": {"CPUReservation", "MemoryReservation", "CPUUtilization", "MemoryUtilization"},
  47. "AWS/EFS": {"BurstCreditBalance", "ClientConnections", "DataReadIOBytes", "DataWriteIOBytes", "MetadataIOBytes", "TotalIOBytes", "PermittedThroughput", "PercentIOLimit"},
  48. "AWS/ELB": {"HealthyHostCount", "UnHealthyHostCount", "RequestCount", "Latency", "HTTPCode_ELB_4XX", "HTTPCode_ELB_5XX", "HTTPCode_Backend_2XX", "HTTPCode_Backend_3XX", "HTTPCode_Backend_4XX", "HTTPCode_Backend_5XX", "BackendConnectionErrors", "SurgeQueueLength", "SpilloverCount", "EstimatedALBActiveConnectionCount", "EstimatedALBConsumedLCUs", "EstimatedALBNewConnectionCount", "EstimatedProcessedBytes"},
  49. "AWS/ElastiCache": {
  50. "CPUUtilization", "FreeableMemory", "NetworkBytesIn", "NetworkBytesOut", "SwapUsage",
  51. "BytesUsedForCacheItems", "BytesReadIntoMemcached", "BytesWrittenOutFromMemcached", "CasBadval", "CasHits", "CasMisses", "CmdFlush", "CmdGet", "CmdSet", "CurrConnections", "CurrItems", "DecrHits", "DecrMisses", "DeleteHits", "DeleteMisses", "Evictions", "GetHits", "GetMisses", "IncrHits", "IncrMisses", "Reclaimed",
  52. "BytesUsedForHash", "CmdConfigGet", "CmdConfigSet", "CmdTouch", "CurrConfig", "EvictedUnfetched", "ExpiredUnfetched", "SlabsMoved", "TouchHits", "TouchMisses",
  53. "NewConnections", "NewItems", "UnusedMemory",
  54. "BytesUsedForCache", "CacheHits", "CacheMisses", "CurrConnections", "Evictions", "HyperLogLogBasedCmds", "NewConnections", "Reclaimed", "ReplicationBytes", "ReplicationLag", "SaveInProgress",
  55. "CurrItems", "GetTypeCmds", "HashBasedCmds", "KeyBasedCmds", "ListBasedCmds", "SetBasedCmds", "SetTypeCmds", "SortedSetBasedCmds", "StringBasedCmds",
  56. },
  57. "AWS/ElasticBeanstalk": {
  58. "EnvironmentHealth",
  59. "ApplicationLatencyP10", "ApplicationLatencyP50", "ApplicationLatencyP75", "ApplicationLatencyP85", "ApplicationLatencyP90", "ApplicationLatencyP95", "ApplicationLatencyP99", "ApplicationLatencyP99.9",
  60. "ApplicationRequests2xx", "ApplicationRequests3xx", "ApplicationRequests4xx", "ApplicationRequests5xx", "ApplicationRequestsTotal",
  61. "CPUIdle", "CPUIowait", "CPUIrq", "CPUNice", "CPUSoftirq", "CPUSystem", "CPUUser",
  62. "InstanceHealth", "InstancesDegraded", "InstancesInfo", "InstancesNoData", "InstancesOk", "InstancesPending", "InstancesSevere", "InstancesUnknown", "InstancesWarning",
  63. "LoadAverage1min", "LoadAverage5min",
  64. "RootFilesystemUtil",
  65. },
  66. "AWS/ElasticMapReduce": {"IsIdle", "JobsRunning", "JobsFailed",
  67. "MapTasksRunning", "MapTasksRemaining", "MapSlotsOpen", "RemainingMapTasksPerSlot", "ReduceTasksRunning", "ReduceTasksRemaining", "ReduceSlotsOpen",
  68. "CoreNodesRunning", "CoreNodesPending", "LiveDataNodes", "TaskNodesRunning", "TaskNodesPending", "LiveTaskTrackers",
  69. "S3BytesWritten", "S3BytesRead", "HDFSUtilization", "HDFSBytesRead", "HDFSBytesWritten", "MissingBlocks", "TotalLoad",
  70. "BackupFailed", "MostRecentBackupDuration", "TimeSinceLastSuccessfulBackup",
  71. "IsIdle", "ContainerAllocated", "ContainerReserved", "ContainerPending", "AppsCompleted", "AppsFailed", "AppsKilled", "AppsPending", "AppsRunning", "AppsSubmitted",
  72. "CoreNodesRunning", "CoreNodesPending", "LiveDataNodes", "MRTotalNodes", "MRActiveNodes", "MRLostNodes", "MRUnhealthyNodes", "MRDecommissionedNodes", "MRRebootedNodes",
  73. "S3BytesWritten", "S3BytesRead", "HDFSUtilization", "HDFSBytesRead", "HDFSBytesWritten", "MissingBlocks", "CorruptBlocks", "TotalLoad", "MemoryTotalMB", "MemoryReservedMB", "MemoryAvailableMB", "MemoryAllocatedMB", "PendingDeletionBlocks", "UnderReplicatedBlocks", "DfsPendingReplicationBlocks", "CapacityRemainingGB",
  74. "HbaseBackupFailed", "MostRecentBackupDuration", "TimeSinceLastSuccessfulBackup"},
  75. "AWS/ES": {"ClusterStatus.green", "ClusterStatus.yellow", "ClusterStatus.red", "ClusterUsedSpace", "Nodes", "SearchableDocuments", "DeletedDocuments", "CPUCreditBalance", "CPUUtilization", "FreeStorageSpace", "JVMMemoryPressure", "AutomatedSnapshotFailure", "MasterCPUCreditBalance", "MasterCPUUtilization", "MasterFreeStorageSpace", "MasterJVMMemoryPressure", "ReadLatency", "WriteLatency", "ReadThroughput", "WriteThroughput", "DiskQueueDepth", "ReadIOPS", "WriteIOPS"},
  76. "AWS/Events": {"Invocations", "FailedInvocations", "TriggeredRules", "MatchedEvents", "ThrottledRules"},
  77. "AWS/Firehose": {"DeliveryToElasticsearch.Bytes", "DeliveryToElasticsearch.Records", "DeliveryToElasticsearch.Success", "DeliveryToRedshift.Bytes", "DeliveryToRedshift.Records", "DeliveryToRedshift.Success", "DeliveryToS3.Bytes", "DeliveryToS3.DataFreshness", "DeliveryToS3.Records", "DeliveryToS3.Success", "IncomingBytes", "IncomingRecords", "DescribeDeliveryStream.Latency", "DescribeDeliveryStream.Requests", "ListDeliveryStreams.Latency", "ListDeliveryStreams.Requests", "PutRecord.Bytes", "PutRecord.Latency", "PutRecord.Requests", "PutRecordBatch.Bytes", "PutRecordBatch.Latency", "PutRecordBatch.Records", "PutRecordBatch.Requests", "UpdateDeliveryStream.Latency", "UpdateDeliveryStream.Requests"},
  78. "AWS/IoT": {"PublishIn.Success", "PublishOut.Success", "Subscribe.Success", "Ping.Success", "Connect.Success", "GetThingShadow.Accepted"},
  79. "AWS/Kinesis": {"GetRecords.Bytes", "GetRecords.IteratorAge", "GetRecords.IteratorAgeMilliseconds", "GetRecords.Latency", "GetRecords.Records", "GetRecords.Success", "IncomingBytes", "IncomingRecords", "PutRecord.Bytes", "PutRecord.Latency", "PutRecord.Success", "PutRecords.Bytes", "PutRecords.Latency", "PutRecords.Records", "PutRecords.Success", "ReadProvisionedThroughputExceeded", "WriteProvisionedThroughputExceeded", "IteratorAgeMilliseconds", "OutgoingBytes", "OutgoingRecords"},
  80. "AWS/KinesisAnalytics": {"Bytes", "MillisBehindLatest", "Records", "Success"},
  81. "AWS/Lambda": {"Invocations", "Errors", "Duration", "Throttles", "IteratorAge"},
  82. "AWS/Logs": {"IncomingBytes", "IncomingLogEvents", "ForwardedBytes", "ForwardedLogEvents", "DeliveryErrors", "DeliveryThrottling"},
  83. "AWS/ML": {"PredictCount", "PredictFailureCount"},
  84. "AWS/NATGateway": {"PacketsOutToDestination", "PacketsOutToSource", "PacketsInFromSource", "PacketsInFromDestination", "BytesOutToDestination", "BytesOutToSource", "BytesInFromSource", "BytesInFromDestination", "ErrorPortAllocation", "ActiveConnectionCount", "ConnectionAttemptCount", "ConnectionEstablishedCount", "IdleTimeoutCount", "PacketsDropCount"},
  85. "AWS/NetworkELB": {"ActiveFlowCount", "ConsumedLCUs", "HealthyHostCount", "NewFlowCount", "ProcessedBytes", "TCP_Client_Reset_Count", "TCP_ELB_Reset_Count", "TCP_Target_Reset_Count", "UnHealthyHostCount"},
  86. "AWS/OpsWorks": {"cpu_idle", "cpu_nice", "cpu_system", "cpu_user", "cpu_waitio", "load_1", "load_5", "load_15", "memory_buffers", "memory_cached", "memory_free", "memory_swap", "memory_total", "memory_used", "procs"},
  87. "AWS/Redshift": {"CPUUtilization", "DatabaseConnections", "HealthStatus", "MaintenanceMode", "NetworkReceiveThroughput", "NetworkTransmitThroughput", "PercentageDiskSpaceUsed", "ReadIOPS", "ReadLatency", "ReadThroughput", "WriteIOPS", "WriteLatency", "WriteThroughput"},
  88. "AWS/RDS": {"ActiveTransactions", "AuroraBinlogReplicaLag", "AuroraReplicaLag", "AuroraReplicaLagMaximum", "AuroraReplicaLagMinimum", "BinLogDiskUsage", "BlockedTransactions", "BufferCacheHitRatio", "CommitLatency", "CommitThroughput", "BinLogDiskUsage", "CPUCreditBalance", "CPUCreditUsage", "CPUUtilization", "DatabaseConnections", "DDLLatency", "DDLThroughput", "Deadlocks", "DeleteLatency", "DeleteThroughput", "DiskQueueDepth", "DMLLatency", "DMLThroughput", "EngineUptime", "FailedSqlStatements", "FreeableMemory", "FreeLocalStorage", "FreeStorageSpace", "InsertLatency", "InsertThroughput", "LoginFailures", "NetworkReceiveThroughput", "NetworkTransmitThroughput", "NetworkThroughput", "Queries", "ReadIOPS", "ReadLatency", "ReadThroughput", "ReplicaLag", "ResultSetCacheHitRatio", "SelectLatency", "SelectThroughput", "SwapUsage", "TotalConnections", "UpdateLatency", "UpdateThroughput", "VolumeBytesUsed", "VolumeReadIOPS", "VolumeWriteIOPS", "WriteIOPS", "WriteLatency", "WriteThroughput"},
  89. "AWS/Route53": {"ChildHealthCheckHealthyCount", "HealthCheckStatus", "HealthCheckPercentageHealthy", "ConnectionTime", "SSLHandshakeTime", "TimeToFirstByte"},
  90. "AWS/S3": {"BucketSizeBytes", "NumberOfObjects", "AllRequests", "GetRequests", "PutRequests", "DeleteRequests", "HeadRequests", "PostRequests", "ListRequests", "BytesDownloaded", "BytesUploaded", "4xxErrors", "5xxErrors", "FirstByteLatency", "TotalRequestLatency"},
  91. "AWS/SES": {"Bounce", "Complaint", "Delivery", "Reject", "Send"},
  92. "AWS/SNS": {"NumberOfMessagesPublished", "PublishSize", "NumberOfNotificationsDelivered", "NumberOfNotificationsFailed"},
  93. "AWS/SQS": {"NumberOfMessagesSent", "SentMessageSize", "NumberOfMessagesReceived", "NumberOfEmptyReceives", "NumberOfMessagesDeleted", "ApproximateAgeOfOldestMessage", "ApproximateNumberOfMessagesDelayed", "ApproximateNumberOfMessagesVisible", "ApproximateNumberOfMessagesNotVisible"},
  94. "AWS/States": {"ExecutionTime", "ExecutionThrottled", "ExecutionsAborted", "ExecutionsFailed", "ExecutionsStarted", "ExecutionsSucceeded", "ExecutionsTimedOut", "ActivityRunTime", "ActivityScheduleTime", "ActivityTime", "ActivitiesFailed", "ActivitiesHeartbeatTimedOut", "ActivitiesScheduled", "ActivitiesScheduled", "ActivitiesSucceeded", "ActivitiesTimedOut", "LambdaFunctionRunTime", "LambdaFunctionScheduleTime", "LambdaFunctionTime", "LambdaFunctionsFailed", "LambdaFunctionsHeartbeatTimedOut", "LambdaFunctionsScheduled", "LambdaFunctionsStarted", "LambdaFunctionsSucceeded", "LambdaFunctionsTimedOut"},
  95. "AWS/StorageGateway": {"CacheHitPercent", "CachePercentUsed", "CachePercentDirty", "CloudBytesDownloaded", "CloudDownloadLatency", "CloudBytesUploaded", "UploadBufferFree", "UploadBufferPercentUsed", "UploadBufferUsed", "QueuedWrites", "ReadBytes", "ReadTime", "TotalCacheSize", "WriteBytes", "WriteTime", "TimeSinceLastRecoveryPoint", "WorkingStorageFree", "WorkingStoragePercentUsed", "WorkingStorageUsed",
  96. "CacheHitPercent", "CachePercentUsed", "CachePercentDirty", "ReadBytes", "ReadTime", "WriteBytes", "WriteTime", "QueuedWrites"},
  97. "AWS/SWF": {"DecisionTaskScheduleToStartTime", "DecisionTaskStartToCloseTime", "DecisionTasksCompleted", "StartedDecisionTasksTimedOutOnClose", "WorkflowStartToCloseTime", "WorkflowsCanceled", "WorkflowsCompleted", "WorkflowsContinuedAsNew", "WorkflowsFailed", "WorkflowsTerminated", "WorkflowsTimedOut",
  98. "ActivityTaskScheduleToCloseTime", "ActivityTaskScheduleToStartTime", "ActivityTaskStartToCloseTime", "ActivityTasksCanceled", "ActivityTasksCompleted", "ActivityTasksFailed", "ScheduledActivityTasksTimedOutOnClose", "ScheduledActivityTasksTimedOutOnStart", "StartedActivityTasksTimedOutOnClose", "StartedActivityTasksTimedOutOnHeartbeat"},
  99. "AWS/VPN": {"TunnelState", "TunnelDataIn", "TunnelDataOut"},
  100. "Rekognition": {"SuccessfulRequestCount", "ThrottledCount", "ResponseTime", "DetectedFaceCount", "DetectedLabelCount", "ServerErrorCount", "UserErrorCount"},
  101. "WAF": {"AllowedRequests", "BlockedRequests", "CountedRequests"},
  102. "AWS/WorkSpaces": {"Available", "Unhealthy", "ConnectionAttempt", "ConnectionSuccess", "ConnectionFailure", "SessionLaunchTime", "InSessionLatency", "SessionDisconnect"},
  103. "KMS": {"SecondsUntilKeyMaterialExpiration"},
  104. }
  105. dimensionsMap = map[string][]string{
  106. "AWS/AmazonMQ": {"Broker", "Topic", "Queue"},
  107. "AWS/ApiGateway": {"ApiName", "Method", "Resource", "Stage"},
  108. "AWS/ApplicationELB": {"LoadBalancer", "TargetGroup", "AvailabilityZone"},
  109. "AWS/AutoScaling": {"AutoScalingGroupName"},
  110. "AWS/Billing": {"ServiceName", "LinkedAccount", "Currency"},
  111. "AWS/CloudFront": {"DistributionId", "Region"},
  112. "AWS/CloudSearch": {},
  113. "AWS/DMS": {"ReplicationInstanceIdentifier", "ReplicationTaskIdentifier"},
  114. "AWS/DynamoDB": {"TableName", "GlobalSecondaryIndexName", "Operation", "StreamLabel"},
  115. "AWS/EBS": {"VolumeId"},
  116. "AWS/EC2": {"AutoScalingGroupName", "ImageId", "InstanceId", "InstanceType"},
  117. "AWS/EC2Spot": {"AvailabilityZone", "FleetRequestId", "InstanceType"},
  118. "AWS/ECS": {"ClusterName", "ServiceName"},
  119. "AWS/EFS": {"FileSystemId"},
  120. "AWS/ELB": {"LoadBalancerName", "AvailabilityZone"},
  121. "AWS/ElastiCache": {"CacheClusterId", "CacheNodeId"},
  122. "AWS/ElasticBeanstalk": {"EnvironmentName", "InstanceId"},
  123. "AWS/ElasticMapReduce": {"ClusterId", "JobFlowId", "JobId"},
  124. "AWS/ES": {"ClientId", "DomainName"},
  125. "AWS/Events": {"RuleName"},
  126. "AWS/Firehose": {"DeliveryStreamName"},
  127. "AWS/IoT": {"Protocol"},
  128. "AWS/Kinesis": {"StreamName", "ShardId"},
  129. "AWS/KinesisAnalytics": {"Flow", "Id", "Application"},
  130. "AWS/Lambda": {"FunctionName", "Resource", "Version", "Alias"},
  131. "AWS/Logs": {"LogGroupName", "DestinationType", "FilterName"},
  132. "AWS/ML": {"MLModelId", "RequestMode"},
  133. "AWS/NATGateway": {"NatGatewayId"},
  134. "AWS/NetworkELB": {"LoadBalancer", "TargetGroup", "AvailabilityZone"},
  135. "AWS/OpsWorks": {"StackId", "LayerId", "InstanceId"},
  136. "AWS/Redshift": {"NodeID", "ClusterIdentifier"},
  137. "AWS/RDS": {"DBInstanceIdentifier", "DBClusterIdentifier", "DbClusterIdentifier", "DatabaseClass", "EngineName", "Role"},
  138. "AWS/Route53": {"HealthCheckId", "Region"},
  139. "AWS/S3": {"BucketName", "StorageType", "FilterId"},
  140. "AWS/SES": {},
  141. "AWS/SNS": {"Application", "Platform", "TopicName"},
  142. "AWS/SQS": {"QueueName"},
  143. "AWS/States": {"StateMachineArn", "ActivityArn", "LambdaFunctionArn"},
  144. "AWS/StorageGateway": {"GatewayId", "GatewayName", "VolumeId"},
  145. "AWS/SWF": {"Domain", "WorkflowTypeName", "WorkflowTypeVersion", "ActivityTypeName", "ActivityTypeVersion"},
  146. "AWS/VPN": {"VpnId", "TunnelIpAddress"},
  147. "Rekognition": {},
  148. "WAF": {"Rule", "WebACL"},
  149. "AWS/WorkSpaces": {"DirectoryId", "WorkspaceId"},
  150. "KMS": {"KeyId"},
  151. }
  152. customMetricsMetricsMap = make(map[string]map[string]map[string]*CustomMetricsCache)
  153. customMetricsDimensionsMap = make(map[string]map[string]map[string]*CustomMetricsCache)
  154. }
  155. func (e *CloudWatchExecutor) executeMetricFindQuery(ctx context.Context, queryContext *tsdb.TsdbQuery) (*tsdb.Response, error) {
  156. result := &tsdb.Response{
  157. Results: make(map[string]*tsdb.QueryResult),
  158. }
  159. firstQuery := queryContext.Queries[0]
  160. queryResult := &tsdb.QueryResult{Meta: simplejson.New(), RefId: firstQuery.RefId}
  161. parameters := firstQuery.Model
  162. subType := firstQuery.Model.Get("subtype").MustString()
  163. var data []suggestData
  164. var err error
  165. switch subType {
  166. case "regions":
  167. data, err = e.handleGetRegions(ctx, parameters, queryContext)
  168. break
  169. case "namespaces":
  170. data, err = e.handleGetNamespaces(ctx, parameters, queryContext)
  171. break
  172. case "metrics":
  173. data, err = e.handleGetMetrics(ctx, parameters, queryContext)
  174. break
  175. case "dimension_keys":
  176. data, err = e.handleGetDimensions(ctx, parameters, queryContext)
  177. break
  178. case "dimension_values":
  179. data, err = e.handleGetDimensionValues(ctx, parameters, queryContext)
  180. break
  181. case "ebs_volume_ids":
  182. data, err = e.handleGetEbsVolumeIds(ctx, parameters, queryContext)
  183. break
  184. case "ec2_instance_attribute":
  185. data, err = e.handleGetEc2InstanceAttribute(ctx, parameters, queryContext)
  186. break
  187. }
  188. transformToTable(data, queryResult)
  189. result.Results[firstQuery.RefId] = queryResult
  190. return result, err
  191. }
  192. func transformToTable(data []suggestData, result *tsdb.QueryResult) {
  193. table := &tsdb.Table{
  194. Columns: make([]tsdb.TableColumn, 2),
  195. Rows: make([]tsdb.RowValues, 0),
  196. }
  197. table.Columns[0].Text = "text"
  198. table.Columns[1].Text = "value"
  199. for _, r := range data {
  200. values := make([]interface{}, 2)
  201. values[0] = r.Text
  202. values[1] = r.Value
  203. table.Rows = append(table.Rows, values)
  204. }
  205. result.Tables = append(result.Tables, table)
  206. result.Meta.Set("rowCount", len(data))
  207. }
  208. func parseMultiSelectValue(input string) []string {
  209. trimmedInput := strings.TrimSpace(input)
  210. if strings.HasPrefix(trimmedInput, "{") {
  211. values := strings.Split(strings.TrimRight(strings.TrimLeft(trimmedInput, "{"), "}"), ",")
  212. trimValues := make([]string, len(values))
  213. for i, v := range values {
  214. trimValues[i] = strings.TrimSpace(v)
  215. }
  216. return trimValues
  217. } else {
  218. return []string{trimmedInput}
  219. }
  220. }
  221. // Whenever this list is updated, frontend list should also be updated.
  222. // Please update the region list in public/app/plugins/datasource/cloudwatch/partials/config.html
  223. func (e *CloudWatchExecutor) handleGetRegions(ctx context.Context, parameters *simplejson.Json, queryContext *tsdb.TsdbQuery) ([]suggestData, error) {
  224. regions := []string{
  225. "ap-northeast-1", "ap-northeast-2", "ap-southeast-1", "ap-southeast-2", "ap-south-1", "ca-central-1", "cn-north-1",
  226. "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",
  227. }
  228. result := make([]suggestData, 0)
  229. for _, region := range regions {
  230. result = append(result, suggestData{Text: region, Value: region})
  231. }
  232. return result, nil
  233. }
  234. func (e *CloudWatchExecutor) handleGetNamespaces(ctx context.Context, parameters *simplejson.Json, queryContext *tsdb.TsdbQuery) ([]suggestData, error) {
  235. keys := []string{}
  236. for key := range metricsMap {
  237. keys = append(keys, key)
  238. }
  239. customNamespaces := e.DataSource.JsonData.Get("customMetricsNamespaces").MustString()
  240. if customNamespaces != "" {
  241. keys = append(keys, strings.Split(customNamespaces, ",")...)
  242. }
  243. sort.Sort(sort.StringSlice(keys))
  244. result := make([]suggestData, 0)
  245. for _, key := range keys {
  246. result = append(result, suggestData{Text: key, Value: key})
  247. }
  248. return result, nil
  249. }
  250. func (e *CloudWatchExecutor) handleGetMetrics(ctx context.Context, parameters *simplejson.Json, queryContext *tsdb.TsdbQuery) ([]suggestData, error) {
  251. region := parameters.Get("region").MustString()
  252. namespace := parameters.Get("namespace").MustString()
  253. var namespaceMetrics []string
  254. if !isCustomMetrics(namespace) {
  255. var exists bool
  256. if namespaceMetrics, exists = metricsMap[namespace]; !exists {
  257. return nil, errors.New("Unable to find namespace " + namespace)
  258. }
  259. } else {
  260. var err error
  261. dsInfo := e.getDsInfo(region)
  262. dsInfo.Namespace = namespace
  263. if namespaceMetrics, err = getMetricsForCustomMetrics(dsInfo, getAllMetrics); err != nil {
  264. return nil, errors.New("Unable to call AWS API")
  265. }
  266. }
  267. sort.Sort(sort.StringSlice(namespaceMetrics))
  268. result := make([]suggestData, 0)
  269. for _, name := range namespaceMetrics {
  270. result = append(result, suggestData{Text: name, Value: name})
  271. }
  272. return result, nil
  273. }
  274. func (e *CloudWatchExecutor) handleGetDimensions(ctx context.Context, parameters *simplejson.Json, queryContext *tsdb.TsdbQuery) ([]suggestData, error) {
  275. region := parameters.Get("region").MustString()
  276. namespace := parameters.Get("namespace").MustString()
  277. var dimensionValues []string
  278. if !isCustomMetrics(namespace) {
  279. var exists bool
  280. if dimensionValues, exists = dimensionsMap[namespace]; !exists {
  281. return nil, errors.New("Unable to find dimension " + namespace)
  282. }
  283. } else {
  284. var err error
  285. dsInfo := e.getDsInfo(region)
  286. dsInfo.Namespace = namespace
  287. if dimensionValues, err = getDimensionsForCustomMetrics(dsInfo, getAllMetrics); err != nil {
  288. return nil, errors.New("Unable to call AWS API")
  289. }
  290. }
  291. sort.Sort(sort.StringSlice(dimensionValues))
  292. result := make([]suggestData, 0)
  293. for _, name := range dimensionValues {
  294. result = append(result, suggestData{Text: name, Value: name})
  295. }
  296. return result, nil
  297. }
  298. func (e *CloudWatchExecutor) handleGetDimensionValues(ctx context.Context, parameters *simplejson.Json, queryContext *tsdb.TsdbQuery) ([]suggestData, error) {
  299. region := parameters.Get("region").MustString()
  300. namespace := parameters.Get("namespace").MustString()
  301. metricName := parameters.Get("metricName").MustString()
  302. dimensionKey := parameters.Get("dimensionKey").MustString()
  303. dimensionsJson := parameters.Get("dimensions").MustMap()
  304. var dimensions []*cloudwatch.DimensionFilter
  305. for k, v := range dimensionsJson {
  306. if vv, ok := v.(string); ok {
  307. dimensions = append(dimensions, &cloudwatch.DimensionFilter{
  308. Name: aws.String(k),
  309. Value: aws.String(vv),
  310. })
  311. }
  312. }
  313. metrics, err := e.cloudwatchListMetrics(region, namespace, metricName, dimensions)
  314. if err != nil {
  315. return nil, err
  316. }
  317. result := make([]suggestData, 0)
  318. dupCheck := make(map[string]bool)
  319. for _, metric := range metrics.Metrics {
  320. for _, dim := range metric.Dimensions {
  321. if *dim.Name == dimensionKey {
  322. if _, exists := dupCheck[*dim.Value]; exists {
  323. continue
  324. }
  325. dupCheck[*dim.Value] = true
  326. result = append(result, suggestData{Text: *dim.Value, Value: *dim.Value})
  327. }
  328. }
  329. }
  330. sort.Slice(result, func(i, j int) bool {
  331. return result[i].Text < result[j].Text
  332. })
  333. return result, nil
  334. }
  335. func (e *CloudWatchExecutor) ensureClientSession(region string) error {
  336. if e.ec2Svc == nil {
  337. dsInfo := e.getDsInfo(region)
  338. cfg, err := e.getAwsConfig(dsInfo)
  339. if err != nil {
  340. return fmt.Errorf("Failed to call ec2:getAwsConfig, %v", err)
  341. }
  342. sess, err := session.NewSession(cfg)
  343. if err != nil {
  344. return fmt.Errorf("Failed to call ec2:NewSession, %v", err)
  345. }
  346. e.ec2Svc = ec2.New(sess, cfg)
  347. }
  348. return nil
  349. }
  350. func (e *CloudWatchExecutor) handleGetEbsVolumeIds(ctx context.Context, parameters *simplejson.Json, queryContext *tsdb.TsdbQuery) ([]suggestData, error) {
  351. region := parameters.Get("region").MustString()
  352. instanceId := parameters.Get("instanceId").MustString()
  353. err := e.ensureClientSession(region)
  354. if err != nil {
  355. return nil, err
  356. }
  357. instanceIds := aws.StringSlice(parseMultiSelectValue(instanceId))
  358. instances, err := e.ec2DescribeInstances(region, nil, instanceIds)
  359. if err != nil {
  360. return nil, err
  361. }
  362. result := make([]suggestData, 0)
  363. for _, reservation := range instances.Reservations {
  364. for _, instance := range reservation.Instances {
  365. for _, mapping := range instance.BlockDeviceMappings {
  366. result = append(result, suggestData{Text: *mapping.Ebs.VolumeId, Value: *mapping.Ebs.VolumeId})
  367. }
  368. }
  369. }
  370. return result, nil
  371. }
  372. func (e *CloudWatchExecutor) handleGetEc2InstanceAttribute(ctx context.Context, parameters *simplejson.Json, queryContext *tsdb.TsdbQuery) ([]suggestData, error) {
  373. region := parameters.Get("region").MustString()
  374. attributeName := parameters.Get("attributeName").MustString()
  375. filterJson := parameters.Get("filters").MustMap()
  376. var filters []*ec2.Filter
  377. for k, v := range filterJson {
  378. if vv, ok := v.([]interface{}); ok {
  379. var vvvvv []*string
  380. for _, vvv := range vv {
  381. if vvvv, ok := vvv.(string); ok {
  382. vvvvv = append(vvvvv, &vvvv)
  383. }
  384. }
  385. filters = append(filters, &ec2.Filter{
  386. Name: aws.String(k),
  387. Values: vvvvv,
  388. })
  389. }
  390. }
  391. err := e.ensureClientSession(region)
  392. if err != nil {
  393. return nil, err
  394. }
  395. instances, err := e.ec2DescribeInstances(region, filters, nil)
  396. if err != nil {
  397. return nil, err
  398. }
  399. result := make([]suggestData, 0)
  400. dupCheck := make(map[string]bool)
  401. for _, reservation := range instances.Reservations {
  402. for _, instance := range reservation.Instances {
  403. tags := make(map[string]string)
  404. for _, tag := range instance.Tags {
  405. tags[*tag.Key] = *tag.Value
  406. }
  407. var data string
  408. if strings.Index(attributeName, "Tags.") == 0 {
  409. tagName := attributeName[5:]
  410. data = tags[tagName]
  411. } else {
  412. attributePath := strings.Split(attributeName, ".")
  413. v := reflect.ValueOf(instance)
  414. for _, key := range attributePath {
  415. if v.Kind() == reflect.Ptr {
  416. v = v.Elem()
  417. }
  418. if v.Kind() != reflect.Struct {
  419. return nil, errors.New("invalid attribute path")
  420. }
  421. v = v.FieldByName(key)
  422. }
  423. if attr, ok := v.Interface().(*string); ok {
  424. data = *attr
  425. } else {
  426. return nil, errors.New("invalid attribute path")
  427. }
  428. }
  429. if _, exists := dupCheck[data]; exists {
  430. continue
  431. }
  432. dupCheck[data] = true
  433. result = append(result, suggestData{Text: data, Value: data})
  434. }
  435. }
  436. sort.Slice(result, func(i, j int) bool {
  437. return result[i].Text < result[j].Text
  438. })
  439. return result, nil
  440. }
  441. func (e *CloudWatchExecutor) cloudwatchListMetrics(region string, namespace string, metricName string, dimensions []*cloudwatch.DimensionFilter) (*cloudwatch.ListMetricsOutput, error) {
  442. svc, err := e.getClient(region)
  443. if err != nil {
  444. return nil, err
  445. }
  446. params := &cloudwatch.ListMetricsInput{
  447. Namespace: aws.String(namespace),
  448. MetricName: aws.String(metricName),
  449. Dimensions: dimensions,
  450. }
  451. var resp cloudwatch.ListMetricsOutput
  452. err = svc.ListMetricsPages(params,
  453. func(page *cloudwatch.ListMetricsOutput, lastPage bool) bool {
  454. metrics.M_Aws_CloudWatch_ListMetrics.Inc()
  455. metrics, _ := awsutil.ValuesAtPath(page, "Metrics")
  456. for _, metric := range metrics {
  457. resp.Metrics = append(resp.Metrics, metric.(*cloudwatch.Metric))
  458. }
  459. return !lastPage
  460. })
  461. if err != nil {
  462. return nil, fmt.Errorf("Failed to call cloudwatch:ListMetrics, %v", err)
  463. }
  464. return &resp, nil
  465. }
  466. func (e *CloudWatchExecutor) ec2DescribeInstances(region string, filters []*ec2.Filter, instanceIds []*string) (*ec2.DescribeInstancesOutput, error) {
  467. params := &ec2.DescribeInstancesInput{
  468. Filters: filters,
  469. InstanceIds: instanceIds,
  470. }
  471. var resp ec2.DescribeInstancesOutput
  472. err := e.ec2Svc.DescribeInstancesPages(params,
  473. func(page *ec2.DescribeInstancesOutput, lastPage bool) bool {
  474. reservations, _ := awsutil.ValuesAtPath(page, "Reservations")
  475. for _, reservation := range reservations {
  476. resp.Reservations = append(resp.Reservations, reservation.(*ec2.Reservation))
  477. }
  478. return !lastPage
  479. })
  480. if err != nil {
  481. return nil, errors.New("Failed to call ec2:DescribeInstances")
  482. }
  483. return &resp, nil
  484. }
  485. func getAllMetrics(cwData *DatasourceInfo) (cloudwatch.ListMetricsOutput, error) {
  486. creds, err := GetCredentials(cwData)
  487. if err != nil {
  488. return cloudwatch.ListMetricsOutput{}, err
  489. }
  490. cfg := &aws.Config{
  491. Region: aws.String(cwData.Region),
  492. Credentials: creds,
  493. }
  494. sess, err := session.NewSession(cfg)
  495. if err != nil {
  496. return cloudwatch.ListMetricsOutput{}, err
  497. }
  498. svc := cloudwatch.New(sess, cfg)
  499. params := &cloudwatch.ListMetricsInput{
  500. Namespace: aws.String(cwData.Namespace),
  501. }
  502. var resp cloudwatch.ListMetricsOutput
  503. err = svc.ListMetricsPages(params,
  504. func(page *cloudwatch.ListMetricsOutput, lastPage bool) bool {
  505. metrics.M_Aws_CloudWatch_ListMetrics.Inc()
  506. metrics, _ := awsutil.ValuesAtPath(page, "Metrics")
  507. for _, metric := range metrics {
  508. resp.Metrics = append(resp.Metrics, metric.(*cloudwatch.Metric))
  509. }
  510. return !lastPage
  511. })
  512. if err != nil {
  513. return resp, err
  514. }
  515. return resp, nil
  516. }
  517. var metricsCacheLock sync.Mutex
  518. func getMetricsForCustomMetrics(dsInfo *DatasourceInfo, getAllMetrics func(*DatasourceInfo) (cloudwatch.ListMetricsOutput, error)) ([]string, error) {
  519. metricsCacheLock.Lock()
  520. defer metricsCacheLock.Unlock()
  521. if _, ok := customMetricsMetricsMap[dsInfo.Profile]; !ok {
  522. customMetricsMetricsMap[dsInfo.Profile] = make(map[string]map[string]*CustomMetricsCache)
  523. }
  524. if _, ok := customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region]; !ok {
  525. customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region] = make(map[string]*CustomMetricsCache)
  526. }
  527. if _, ok := customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace]; !ok {
  528. customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace] = &CustomMetricsCache{}
  529. customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache = make([]string, 0)
  530. }
  531. if customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Expire.After(time.Now()) {
  532. return customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache, nil
  533. }
  534. result, err := getAllMetrics(dsInfo)
  535. if err != nil {
  536. return []string{}, err
  537. }
  538. customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache = make([]string, 0)
  539. customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Expire = time.Now().Add(5 * time.Minute)
  540. for _, metric := range result.Metrics {
  541. if isDuplicate(customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache, *metric.MetricName) {
  542. continue
  543. }
  544. customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache = append(customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache, *metric.MetricName)
  545. }
  546. return customMetricsMetricsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache, nil
  547. }
  548. var dimensionsCacheLock sync.Mutex
  549. func getDimensionsForCustomMetrics(dsInfo *DatasourceInfo, getAllMetrics func(*DatasourceInfo) (cloudwatch.ListMetricsOutput, error)) ([]string, error) {
  550. dimensionsCacheLock.Lock()
  551. defer dimensionsCacheLock.Unlock()
  552. if _, ok := customMetricsDimensionsMap[dsInfo.Profile]; !ok {
  553. customMetricsDimensionsMap[dsInfo.Profile] = make(map[string]map[string]*CustomMetricsCache)
  554. }
  555. if _, ok := customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region]; !ok {
  556. customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region] = make(map[string]*CustomMetricsCache)
  557. }
  558. if _, ok := customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace]; !ok {
  559. customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace] = &CustomMetricsCache{}
  560. customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache = make([]string, 0)
  561. }
  562. if customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Expire.After(time.Now()) {
  563. return customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache, nil
  564. }
  565. result, err := getAllMetrics(dsInfo)
  566. if err != nil {
  567. return []string{}, err
  568. }
  569. customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache = make([]string, 0)
  570. customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Expire = time.Now().Add(5 * time.Minute)
  571. for _, metric := range result.Metrics {
  572. for _, dimension := range metric.Dimensions {
  573. if isDuplicate(customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache, *dimension.Name) {
  574. continue
  575. }
  576. customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache = append(customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache, *dimension.Name)
  577. }
  578. }
  579. return customMetricsDimensionsMap[dsInfo.Profile][dsInfo.Region][dsInfo.Namespace].Cache, nil
  580. }
  581. func isDuplicate(nameList []string, target string) bool {
  582. for _, name := range nameList {
  583. if name == target {
  584. return true
  585. }
  586. }
  587. return false
  588. }
  589. func isCustomMetrics(namespace string) bool {
  590. return strings.Index(namespace, "AWS/") != 0
  591. }