2019-11-14 03:59:41 -06:00
package cloudwatch
import (
2021-09-08 09:06:43 -05:00
"encoding/json"
"io/ioutil"
2022-04-04 08:44:19 -05:00
"path/filepath"
2021-11-30 03:53:31 -06:00
"strings"
2019-11-14 03:59:41 -06:00
"testing"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudwatch"
2020-10-02 12:40:15 -05:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2019-11-14 03:59:41 -06:00
)
2022-04-04 08:44:19 -05:00
func loadGetMetricDataOutputsFromFile ( filePath string ) ( [ ] * cloudwatch . GetMetricDataOutput , error ) {
2021-09-08 09:06:43 -05:00
var getMetricDataOutputs [ ] * cloudwatch . GetMetricDataOutput
2022-04-04 08:44:19 -05:00
cleanFilePath := filepath . Clean ( filePath )
jsonBody , err := ioutil . ReadFile ( cleanFilePath )
2021-09-08 09:06:43 -05:00
if err != nil {
return getMetricDataOutputs , err
}
err = json . Unmarshal ( jsonBody , & getMetricDataOutputs )
return getMetricDataOutputs , err
}
2019-11-14 03:59:41 -06:00
func TestCloudWatchResponseParser ( t * testing . T ) {
2021-09-08 09:06:43 -05:00
startTime := time . Now ( )
endTime := startTime . Add ( 2 * time . Hour )
2022-05-08 02:27:03 -05:00
t . Run ( "when aggregating multi-outputs response" , func ( t * testing . T ) {
getMetricDataOutputs , err := loadGetMetricDataOutputsFromFile ( "./test-data/multiple-outputs-query-a.json" )
2021-09-08 09:06:43 -05:00
require . NoError ( t , err )
aggregatedResponse := aggregateResponse ( getMetricDataOutputs )
2022-05-08 02:27:03 -05:00
idA := "a"
t . Run ( "should have two labels" , func ( t * testing . T ) {
assert . Len ( t , aggregatedResponse [ idA ] . Metrics , 2 )
2021-09-08 09:06:43 -05:00
} )
2022-05-08 02:27:03 -05:00
t . Run ( "should have points for label1 taken from both getMetricDataOutputs" , func ( t * testing . T ) {
2022-05-18 02:16:38 -05:00
require . NotNil ( t , * aggregatedResponse [ idA ] . Metrics [ 0 ] . Label )
require . Equal ( t , "label1" , * aggregatedResponse [ idA ] . Metrics [ 0 ] . Label )
assert . Len ( t , aggregatedResponse [ idA ] . Metrics [ 0 ] . Values , 10 )
2022-05-08 02:27:03 -05:00
} )
t . Run ( "should have statuscode 'Complete'" , func ( t * testing . T ) {
assert . Equal ( t , "Complete" , aggregatedResponse [ idA ] . StatusCode )
} )
t . Run ( "should have exceeded request limit" , func ( t * testing . T ) {
assert . True ( t , aggregatedResponse [ idA ] . ErrorCodes [ "MaxMetricsExceeded" ] )
} )
t . Run ( "should have exceeded query time range" , func ( t * testing . T ) {
assert . True ( t , aggregatedResponse [ idA ] . ErrorCodes [ "MaxQueryTimeRangeExceeded" ] )
} )
t . Run ( "should have exceeded max query results" , func ( t * testing . T ) {
assert . True ( t , aggregatedResponse [ idA ] . ErrorCodes [ "MaxQueryResultsExceeded" ] )
} )
t . Run ( "should have exceeded max matching results" , func ( t * testing . T ) {
assert . True ( t , aggregatedResponse [ idA ] . ErrorCodes [ "MaxMatchingResultsExceeded" ] )
} )
} )
t . Run ( "when aggregating multi-outputs response with PartialData and ArithmeticError" , func ( t * testing . T ) {
getMetricDataOutputs , err := loadGetMetricDataOutputsFromFile ( "./test-data/multiple-outputs-query-b.json" )
require . NoError ( t , err )
aggregatedResponse := aggregateResponse ( getMetricDataOutputs )
idB := "b"
t . Run ( "should have statuscode is 'PartialData'" , func ( t * testing . T ) {
assert . Equal ( t , "PartialData" , aggregatedResponse [ idB ] . StatusCode )
} )
t . Run ( "should have an arithmetic error and an error message" , func ( t * testing . T ) {
assert . True ( t , aggregatedResponse [ idB ] . HasArithmeticError )
assert . Equal ( t , "One or more data-points have been dropped due to non-numeric values (NaN, -Infinite, +Infinite)" , aggregatedResponse [ idB ] . ArithmeticErrorMessage )
2021-09-08 09:06:43 -05:00
} )
} )
2022-05-18 02:16:38 -05:00
t . Run ( "when aggregating multi-outputs response" , func ( t * testing . T ) {
getMetricDataOutputs , err := loadGetMetricDataOutputsFromFile ( "./test-data/single-output-multiple-metric-data-results.json" )
require . NoError ( t , err )
aggregatedResponse := aggregateResponse ( getMetricDataOutputs )
idA := "a"
t . Run ( "should have one label" , func ( t * testing . T ) {
assert . Len ( t , aggregatedResponse [ idA ] . Metrics , 1 )
} )
t . Run ( "should have points for label1 taken from both MetricDataResults" , func ( t * testing . T ) {
require . NotNil ( t , * aggregatedResponse [ idA ] . Metrics [ 0 ] . Label )
require . Equal ( t , "label1" , * aggregatedResponse [ idA ] . Metrics [ 0 ] . Label )
assert . Len ( t , aggregatedResponse [ idA ] . Metrics [ 0 ] . Values , 6 )
} )
t . Run ( "should have statuscode 'Complete'" , func ( t * testing . T ) {
assert . Equal ( t , "Complete" , aggregatedResponse [ idA ] . StatusCode )
} )
} )
2022-04-04 08:44:19 -05:00
t . Run ( "when aggregating response and error codes are in first GetMetricDataOutput" , func ( t * testing . T ) {
getMetricDataOutputs , err := loadGetMetricDataOutputsFromFile ( "./test-data/multiple-outputs2.json" )
require . NoError ( t , err )
aggregatedResponse := aggregateResponse ( getMetricDataOutputs )
t . Run ( "response for id a" , func ( t * testing . T ) {
idA := "a"
t . Run ( "should have exceeded request limit" , func ( t * testing . T ) {
assert . True ( t , aggregatedResponse [ idA ] . ErrorCodes [ "MaxMetricsExceeded" ] )
} )
t . Run ( "should have exceeded query time range" , func ( t * testing . T ) {
assert . True ( t , aggregatedResponse [ idA ] . ErrorCodes [ "MaxQueryTimeRangeExceeded" ] )
} )
t . Run ( "should have exceeded max query results" , func ( t * testing . T ) {
assert . True ( t , aggregatedResponse [ idA ] . ErrorCodes [ "MaxQueryResultsExceeded" ] )
} )
t . Run ( "should have exceeded max matching results" , func ( t * testing . T ) {
assert . True ( t , aggregatedResponse [ idA ] . ErrorCodes [ "MaxMatchingResultsExceeded" ] )
} )
} )
} )
2020-10-02 12:40:15 -05:00
t . Run ( "Expand dimension value using exact match" , func ( t * testing . T ) {
timestamp := time . Unix ( 0 , 0 )
2021-09-08 09:06:43 -05:00
response := & queryRowResponse {
2022-05-18 02:16:38 -05:00
Metrics : [ ] * cloudwatch . MetricDataResult {
{
2021-09-08 09:06:43 -05:00
Id : aws . String ( "id1" ) ,
Label : aws . String ( "lb1" ) ,
Timestamps : [ ] * time . Time {
aws . Time ( timestamp ) ,
aws . Time ( timestamp . Add ( 60 * time . Second ) ) ,
aws . Time ( timestamp . Add ( 180 * time . Second ) ) ,
} ,
Values : [ ] * float64 {
aws . Float64 ( 10 ) ,
aws . Float64 ( 20 ) ,
aws . Float64 ( 30 ) ,
} ,
StatusCode : aws . String ( "Complete" ) ,
2020-01-17 05:47:40 -06:00
} ,
2022-05-18 02:16:38 -05:00
{
2021-09-08 09:06:43 -05:00
Id : aws . String ( "id2" ) ,
Label : aws . String ( "lb2" ) ,
Timestamps : [ ] * time . Time {
aws . Time ( timestamp ) ,
aws . Time ( timestamp . Add ( 60 * time . Second ) ) ,
aws . Time ( timestamp . Add ( 180 * time . Second ) ) ,
} ,
Values : [ ] * float64 {
aws . Float64 ( 10 ) ,
aws . Float64 ( 20 ) ,
aws . Float64 ( 30 ) ,
} ,
StatusCode : aws . String ( "Complete" ) ,
2020-01-17 05:47:40 -06:00
} ,
2020-10-02 12:40:15 -05:00
} ,
}
2020-09-08 06:35:17 -05:00
2020-10-02 12:40:15 -05:00
query := & cloudWatchQuery {
RefId : "refId1" ,
Region : "us-east-1" ,
Namespace : "AWS/ApplicationELB" ,
MetricName : "TargetResponseTime" ,
Dimensions : map [ string ] [ ] string {
"LoadBalancer" : { "lb1" , "lb2" } ,
"TargetGroup" : { "tg" } ,
} ,
2021-11-30 03:53:31 -06:00
Statistic : "Average" ,
Period : 60 ,
Alias : "{{LoadBalancer}} Expanded" ,
MetricQueryType : MetricQueryTypeSearch ,
MetricEditorMode : MetricEditorModeBuilder ,
2020-10-02 12:40:15 -05:00
}
2022-05-05 06:59:23 -05:00
frames , err := buildDataFrames ( startTime , endTime , * response , query , false )
2020-10-02 12:40:15 -05:00
require . NoError ( t , err )
2020-01-20 11:51:32 -06:00
2020-10-06 06:45:58 -05:00
frame1 := frames [ 0 ]
assert . Equal ( t , "lb1 Expanded" , frame1 . Name )
assert . Equal ( t , "lb1" , frame1 . Fields [ 1 ] . Labels [ "LoadBalancer" ] )
2020-01-17 05:47:40 -06:00
2020-10-06 06:45:58 -05:00
frame2 := frames [ 1 ]
assert . Equal ( t , "lb2 Expanded" , frame2 . Name )
assert . Equal ( t , "lb2" , frame2 . Fields [ 1 ] . Labels [ "LoadBalancer" ] )
2020-10-02 12:40:15 -05:00
} )
2020-01-17 05:47:40 -06:00
2020-10-02 12:40:15 -05:00
t . Run ( "Expand dimension value using substring" , func ( t * testing . T ) {
timestamp := time . Unix ( 0 , 0 )
2021-09-08 09:06:43 -05:00
response := & queryRowResponse {
2022-05-18 02:16:38 -05:00
Metrics : [ ] * cloudwatch . MetricDataResult {
{
2021-09-08 09:06:43 -05:00
Id : aws . String ( "id1" ) ,
Label : aws . String ( "lb1 Sum" ) ,
Timestamps : [ ] * time . Time {
aws . Time ( timestamp ) ,
aws . Time ( timestamp . Add ( 60 * time . Second ) ) ,
aws . Time ( timestamp . Add ( 180 * time . Second ) ) ,
} ,
Values : [ ] * float64 {
aws . Float64 ( 10 ) ,
aws . Float64 ( 20 ) ,
aws . Float64 ( 30 ) ,
} ,
StatusCode : aws . String ( "Complete" ) ,
2020-01-17 05:47:40 -06:00
} ,
2022-05-18 02:16:38 -05:00
{
2021-09-08 09:06:43 -05:00
Id : aws . String ( "id2" ) ,
Label : aws . String ( "lb2 Average" ) ,
Timestamps : [ ] * time . Time {
aws . Time ( timestamp ) ,
aws . Time ( timestamp . Add ( 60 * time . Second ) ) ,
aws . Time ( timestamp . Add ( 180 * time . Second ) ) ,
} ,
Values : [ ] * float64 {
aws . Float64 ( 10 ) ,
aws . Float64 ( 20 ) ,
aws . Float64 ( 30 ) ,
} ,
StatusCode : aws . String ( "Complete" ) ,
2020-01-17 05:47:40 -06:00
} ,
2021-09-08 09:06:43 -05:00
} }
2020-09-08 06:35:17 -05:00
2020-10-02 12:40:15 -05:00
query := & cloudWatchQuery {
RefId : "refId1" ,
Region : "us-east-1" ,
Namespace : "AWS/ApplicationELB" ,
MetricName : "TargetResponseTime" ,
Dimensions : map [ string ] [ ] string {
"LoadBalancer" : { "lb1" , "lb2" } ,
"TargetGroup" : { "tg" } ,
} ,
2021-11-30 03:53:31 -06:00
Statistic : "Average" ,
Period : 60 ,
Alias : "{{LoadBalancer}} Expanded" ,
MetricQueryType : MetricQueryTypeSearch ,
MetricEditorMode : MetricEditorModeBuilder ,
2020-10-02 12:40:15 -05:00
}
2022-05-05 06:59:23 -05:00
frames , err := buildDataFrames ( startTime , endTime , * response , query , false )
2020-10-02 12:40:15 -05:00
require . NoError ( t , err )
2019-11-14 03:59:41 -06:00
2020-10-06 06:45:58 -05:00
frame1 := frames [ 0 ]
assert . Equal ( t , "lb1 Expanded" , frame1 . Name )
assert . Equal ( t , "lb1" , frame1 . Fields [ 1 ] . Labels [ "LoadBalancer" ] )
2020-03-10 15:14:58 -05:00
2020-10-06 06:45:58 -05:00
frame2 := frames [ 1 ]
assert . Equal ( t , "lb2 Expanded" , frame2 . Name )
assert . Equal ( t , "lb2" , frame2 . Fields [ 1 ] . Labels [ "LoadBalancer" ] )
2020-10-02 12:40:15 -05:00
} )
t . Run ( "Expand dimension value using wildcard" , func ( t * testing . T ) {
timestamp := time . Unix ( 0 , 0 )
2021-09-08 09:06:43 -05:00
response := & queryRowResponse {
2022-05-18 02:16:38 -05:00
Metrics : [ ] * cloudwatch . MetricDataResult {
{
2021-09-08 09:06:43 -05:00
Id : aws . String ( "lb3" ) ,
Label : aws . String ( "lb3" ) ,
Timestamps : [ ] * time . Time {
aws . Time ( timestamp ) ,
aws . Time ( timestamp . Add ( 60 * time . Second ) ) ,
aws . Time ( timestamp . Add ( 180 * time . Second ) ) ,
} ,
Values : [ ] * float64 {
aws . Float64 ( 10 ) ,
aws . Float64 ( 20 ) ,
aws . Float64 ( 30 ) ,
} ,
StatusCode : aws . String ( "Complete" ) ,
2020-10-02 12:40:15 -05:00
} ,
2022-05-18 02:16:38 -05:00
{
2021-09-08 09:06:43 -05:00
Id : aws . String ( "lb4" ) ,
Label : aws . String ( "lb4" ) ,
Timestamps : [ ] * time . Time {
aws . Time ( timestamp ) ,
aws . Time ( timestamp . Add ( 60 * time . Second ) ) ,
aws . Time ( timestamp . Add ( 180 * time . Second ) ) ,
} ,
Values : [ ] * float64 {
aws . Float64 ( 10 ) ,
aws . Float64 ( 20 ) ,
aws . Float64 ( 30 ) ,
} ,
StatusCode : aws . String ( "Complete" ) ,
2020-10-02 12:40:15 -05:00
} ,
} ,
}
2020-09-08 06:35:17 -05:00
2020-10-02 12:40:15 -05:00
query := & cloudWatchQuery {
RefId : "refId1" ,
Region : "us-east-1" ,
Namespace : "AWS/ApplicationELB" ,
MetricName : "TargetResponseTime" ,
Dimensions : map [ string ] [ ] string {
"LoadBalancer" : { "*" } ,
"TargetGroup" : { "tg" } ,
} ,
2021-11-30 03:53:31 -06:00
Statistic : "Average" ,
Period : 60 ,
Alias : "{{LoadBalancer}} Expanded" ,
MetricQueryType : MetricQueryTypeSearch ,
MetricEditorMode : MetricEditorModeBuilder ,
2020-10-02 12:40:15 -05:00
}
2022-05-05 06:59:23 -05:00
frames , err := buildDataFrames ( startTime , endTime , * response , query , false )
2020-10-02 12:40:15 -05:00
require . NoError ( t , err )
2020-10-06 06:45:58 -05:00
assert . Equal ( t , "lb3 Expanded" , frames [ 0 ] . Name )
assert . Equal ( t , "lb4 Expanded" , frames [ 1 ] . Name )
2020-10-02 12:40:15 -05:00
} )
2020-03-10 15:14:58 -05:00
2020-10-02 12:40:15 -05:00
t . Run ( "Expand dimension value when no values are returned and a multi-valued template variable is used" , func ( t * testing . T ) {
timestamp := time . Unix ( 0 , 0 )
2021-09-08 09:06:43 -05:00
response := & queryRowResponse {
2022-05-18 02:16:38 -05:00
Metrics : [ ] * cloudwatch . MetricDataResult {
{
2021-09-08 09:06:43 -05:00
Id : aws . String ( "lb3" ) ,
Label : aws . String ( "lb3" ) ,
Timestamps : [ ] * time . Time {
aws . Time ( timestamp ) ,
aws . Time ( timestamp . Add ( 60 * time . Second ) ) ,
aws . Time ( timestamp . Add ( 180 * time . Second ) ) ,
} ,
Values : [ ] * float64 { } ,
StatusCode : aws . String ( "Complete" ) ,
2020-03-10 15:14:58 -05:00
} ,
2020-10-02 12:40:15 -05:00
} ,
}
query := & cloudWatchQuery {
RefId : "refId1" ,
Region : "us-east-1" ,
Namespace : "AWS/ApplicationELB" ,
MetricName : "TargetResponseTime" ,
Dimensions : map [ string ] [ ] string {
"LoadBalancer" : { "lb1" , "lb2" } ,
} ,
2021-11-30 03:53:31 -06:00
Statistic : "Average" ,
Period : 60 ,
Alias : "{{LoadBalancer}} Expanded" ,
MetricQueryType : MetricQueryTypeSearch ,
MetricEditorMode : MetricEditorModeBuilder ,
2020-10-02 12:40:15 -05:00
}
2022-05-05 06:59:23 -05:00
frames , err := buildDataFrames ( startTime , endTime , * response , query , false )
2020-10-02 12:40:15 -05:00
require . NoError ( t , err )
2020-10-06 06:45:58 -05:00
assert . Len ( t , frames , 2 )
assert . Equal ( t , "lb1 Expanded" , frames [ 0 ] . Name )
assert . Equal ( t , "lb2 Expanded" , frames [ 1 ] . Name )
2020-10-02 12:40:15 -05:00
} )
t . Run ( "Expand dimension value when no values are returned and a multi-valued template variable and two single-valued dimensions are used" , func ( t * testing . T ) {
timestamp := time . Unix ( 0 , 0 )
2021-09-08 09:06:43 -05:00
response := & queryRowResponse {
2022-05-18 02:16:38 -05:00
Metrics : [ ] * cloudwatch . MetricDataResult {
{
2021-09-08 09:06:43 -05:00
Id : aws . String ( "lb3" ) ,
Label : aws . String ( "lb3" ) ,
Timestamps : [ ] * time . Time {
aws . Time ( timestamp ) ,
aws . Time ( timestamp . Add ( 60 * time . Second ) ) ,
aws . Time ( timestamp . Add ( 180 * time . Second ) ) ,
} ,
Values : [ ] * float64 { } ,
StatusCode : aws . String ( "Complete" ) ,
2020-03-10 15:14:58 -05:00
} ,
2020-10-02 12:40:15 -05:00
} ,
}
2020-09-08 06:35:17 -05:00
2020-10-02 12:40:15 -05:00
query := & cloudWatchQuery {
RefId : "refId1" ,
Region : "us-east-1" ,
Namespace : "AWS/ApplicationELB" ,
MetricName : "TargetResponseTime" ,
Dimensions : map [ string ] [ ] string {
"LoadBalancer" : { "lb1" , "lb2" } ,
"InstanceType" : { "micro" } ,
"Resource" : { "res" } ,
} ,
2021-11-30 03:53:31 -06:00
Statistic : "Average" ,
Period : 60 ,
Alias : "{{LoadBalancer}} Expanded {{InstanceType}} - {{Resource}}" ,
MetricQueryType : MetricQueryTypeSearch ,
MetricEditorMode : MetricEditorModeBuilder ,
2020-10-02 12:40:15 -05:00
}
2022-05-05 06:59:23 -05:00
frames , err := buildDataFrames ( startTime , endTime , * response , query , false )
2020-10-02 12:40:15 -05:00
require . NoError ( t , err )
2020-10-06 06:45:58 -05:00
assert . Len ( t , frames , 2 )
assert . Equal ( t , "lb1 Expanded micro - res" , frames [ 0 ] . Name )
assert . Equal ( t , "lb2 Expanded micro - res" , frames [ 1 ] . Name )
2020-10-02 12:40:15 -05:00
} )
2020-03-10 15:14:58 -05:00
2021-11-30 03:53:31 -06:00
t . Run ( "Should only expand certain fields when using SQL queries" , func ( t * testing . T ) {
timestamp := time . Unix ( 0 , 0 )
response := & queryRowResponse {
2022-05-18 02:16:38 -05:00
Metrics : [ ] * cloudwatch . MetricDataResult {
{
2021-11-30 03:53:31 -06:00
Id : aws . String ( "lb3" ) ,
Label : aws . String ( "lb3" ) ,
Timestamps : [ ] * time . Time {
aws . Time ( timestamp ) ,
} ,
Values : [ ] * float64 { aws . Float64 ( 23 ) } ,
StatusCode : aws . String ( "Complete" ) ,
} ,
} ,
}
query := & cloudWatchQuery {
RefId : "refId1" ,
Region : "us-east-1" ,
Namespace : "AWS/ApplicationELB" ,
MetricName : "TargetResponseTime" ,
Dimensions : map [ string ] [ ] string {
"LoadBalancer" : { "lb1" } ,
"InstanceType" : { "micro" } ,
"Resource" : { "res" } ,
} ,
Statistic : "Average" ,
Period : 60 ,
Alias : "{{LoadBalancer}} {{InstanceType}} {{metric}} {{namespace}} {{stat}} {{region}} {{period}}" ,
MetricQueryType : MetricQueryTypeQuery ,
MetricEditorMode : MetricEditorModeRaw ,
}
2022-05-05 06:59:23 -05:00
frames , err := buildDataFrames ( startTime , endTime , * response , query , false )
2021-11-30 03:53:31 -06:00
require . NoError ( t , err )
assert . False ( t , strings . Contains ( frames [ 0 ] . Name , "AWS/ApplicationELB" ) )
assert . False ( t , strings . Contains ( frames [ 0 ] . Name , "lb1" ) )
assert . False ( t , strings . Contains ( frames [ 0 ] . Name , "micro" ) )
assert . False ( t , strings . Contains ( frames [ 0 ] . Name , "AWS/ApplicationELB" ) )
assert . True ( t , strings . Contains ( frames [ 0 ] . Name , "us-east-1" ) )
assert . True ( t , strings . Contains ( frames [ 0 ] . Name , "60" ) )
} )
2020-10-02 12:40:15 -05:00
t . Run ( "Parse cloudwatch response" , func ( t * testing . T ) {
timestamp := time . Unix ( 0 , 0 )
2021-09-08 09:06:43 -05:00
response := & queryRowResponse {
2022-05-18 02:16:38 -05:00
Metrics : [ ] * cloudwatch . MetricDataResult {
{
2021-09-08 09:06:43 -05:00
Id : aws . String ( "id1" ) ,
Label : aws . String ( "lb" ) ,
Timestamps : [ ] * time . Time {
aws . Time ( timestamp ) ,
aws . Time ( timestamp . Add ( 60 * time . Second ) ) ,
aws . Time ( timestamp . Add ( 180 * time . Second ) ) ,
} ,
Values : [ ] * float64 {
aws . Float64 ( 10 ) ,
aws . Float64 ( 20 ) ,
aws . Float64 ( 30 ) ,
} ,
StatusCode : aws . String ( "Complete" ) ,
2019-11-14 03:59:41 -06:00
} ,
2020-10-02 12:40:15 -05:00
} ,
}
2020-09-08 06:35:17 -05:00
2020-10-02 12:40:15 -05:00
query := & cloudWatchQuery {
RefId : "refId1" ,
Region : "us-east-1" ,
Namespace : "AWS/ApplicationELB" ,
MetricName : "TargetResponseTime" ,
Dimensions : map [ string ] [ ] string {
"LoadBalancer" : { "lb" } ,
"TargetGroup" : { "tg" } ,
} ,
2021-11-30 03:53:31 -06:00
Statistic : "Average" ,
Period : 60 ,
Alias : "{{namespace}}_{{metric}}_{{stat}}" ,
MetricQueryType : MetricQueryTypeSearch ,
MetricEditorMode : MetricEditorModeBuilder ,
2020-10-02 12:40:15 -05:00
}
2022-05-05 06:59:23 -05:00
frames , err := buildDataFrames ( startTime , endTime , * response , query , false )
2020-10-02 12:40:15 -05:00
require . NoError ( t , err )
2020-10-06 06:45:58 -05:00
frame := frames [ 0 ]
assert . Equal ( t , "AWS/ApplicationELB_TargetResponseTime_Average" , frame . Name )
2020-11-03 04:24:26 -06:00
assert . Equal ( t , "Time" , frame . Fields [ 0 ] . Name )
2020-10-06 06:45:58 -05:00
assert . Equal ( t , "lb" , frame . Fields [ 1 ] . Labels [ "LoadBalancer" ] )
assert . Equal ( t , 10.0 , * frame . Fields [ 1 ] . At ( 0 ) . ( * float64 ) )
assert . Equal ( t , 20.0 , * frame . Fields [ 1 ] . At ( 1 ) . ( * float64 ) )
2022-02-10 03:17:45 -06:00
assert . Equal ( t , 30.0 , * frame . Fields [ 1 ] . At ( 2 ) . ( * float64 ) )
2020-11-03 04:24:26 -06:00
assert . Equal ( t , "Value" , frame . Fields [ 1 ] . Name )
assert . Equal ( t , "" , frame . Fields [ 1 ] . Config . DisplayName )
2019-11-14 03:59:41 -06:00
} )
2022-05-05 06:59:23 -05:00
t . Run ( "buildDataFrames should use response label as frame name when dynamic label is enabled" , func ( t * testing . T ) {
response := & queryRowResponse {
2022-05-18 02:16:38 -05:00
Metrics : [ ] * cloudwatch . MetricDataResult {
{
Label : aws . String ( "some response label" ) ,
2022-05-05 06:59:23 -05:00
Timestamps : [ ] * time . Time { } ,
Values : [ ] * float64 { aws . Float64 ( 10 ) } ,
StatusCode : aws . String ( "Complete" ) ,
} ,
} ,
}
frames , err := buildDataFrames ( startTime , endTime , * response , & cloudWatchQuery { } , true )
assert . NoError ( t , err )
require . Len ( t , frames , 1 )
assert . Equal ( t , "some response label" , frames [ 0 ] . Name )
} )
2019-11-14 03:59:41 -06:00
}