From 82e7eec15532c76da82de43dacfc44818ea71fa0 Mon Sep 17 00:00:00 2001 From: kaiyan-sheng Date: Tue, 7 May 2019 07:25:15 -0600 Subject: [PATCH] Use time.Duration directly in GetStartTimeEndTime function (#12033) * Remove convertPeriodToDuration and use duration directly in GetStartTimeEndTime * Pass period in time.Duration type --- x-pack/metricbeat/module/aws/aws.go | 27 ++++---------- x-pack/metricbeat/module/aws/aws_test.go | 36 ------------------- .../module/aws/cloudwatch/cloudwatch.go | 20 +++++------ x-pack/metricbeat/module/aws/ec2/ec2.go | 22 ++++++------ x-pack/metricbeat/module/aws/ec2/ec2_test.go | 2 +- .../aws/s3_daily_storage/s3_daily_storage.go | 20 +++++------ .../module/aws/s3_request/s3_request.go | 20 +++++------ x-pack/metricbeat/module/aws/sqs/sqs.go | 17 +++++---- x-pack/metricbeat/module/aws/utils.go | 13 +++---- x-pack/metricbeat/module/aws/utils_test.go | 8 ++--- 10 files changed, 60 insertions(+), 125 deletions(-) diff --git a/x-pack/metricbeat/module/aws/aws.go b/x-pack/metricbeat/module/aws/aws.go index 063a8c1437f..edf11051fc4 100644 --- a/x-pack/metricbeat/module/aws/aws.go +++ b/x-pack/metricbeat/module/aws/aws.go @@ -31,10 +31,9 @@ type Config struct { // MetricSet is the base metricset for all aws metricsets type MetricSet struct { mb.BaseMetricSet - RegionsList []string - DurationString string - PeriodInSec int - AwsConfig *awssdk.Config + RegionsList []string + Period time.Duration + AwsConfig *awssdk.Config } // ModuleName is the name of this module. @@ -77,16 +76,10 @@ func NewMetricSet(base mb.BaseMetricSet) (*MetricSet, error) { awsConfig.Region = config.DefaultRegion - durationString, periodSec := convertPeriodToDuration(config.Period) - if err != nil { - return nil, err - } - metricSet := MetricSet{ - BaseMetricSet: base, - DurationString: durationString, - PeriodInSec: periodSec, - AwsConfig: &awsConfig, + BaseMetricSet: base, + Period: config.Period, + AwsConfig: &awsConfig, } // Construct MetricSet with a full regions list @@ -120,14 +113,6 @@ func getRegions(svc ec2iface.EC2API) (completeRegionsList []string, err error) { return } -func convertPeriodToDuration(period time.Duration) (string, int) { - // Set starttime double the default frequency earlier than the endtime in order to make sure - // GetMetricDataRequest gets the latest data point for each metric. - duration := "-" + (period * 2).String() - numberPeriod := int(period.Seconds()) - return duration, numberPeriod -} - // StringInSlice checks if a string is already exists in list func StringInSlice(str string, list []string) bool { for _, v := range list { diff --git a/x-pack/metricbeat/module/aws/aws_test.go b/x-pack/metricbeat/module/aws/aws_test.go index 5f6c884f93f..665aef11825 100644 --- a/x-pack/metricbeat/module/aws/aws_test.go +++ b/x-pack/metricbeat/module/aws/aws_test.go @@ -9,7 +9,6 @@ package aws import ( "fmt" "testing" - "time" awssdk "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/ec2" @@ -48,38 +47,3 @@ func TestGetRegions(t *testing.T) { assert.Equal(t, 1, len(regionsList)) assert.Equal(t, regionName, regionsList[0]) } - -func TestConvertPeriodToDuration(t *testing.T) { - cases := []struct { - period time.Duration - expectedDuration string - expectedPeriodNumber int - }{ - { - period: time.Duration(300) * time.Second, - expectedDuration: "-10m0s", - expectedPeriodNumber: 300, - }, - { - period: time.Duration(10) * time.Minute, - expectedDuration: "-20m0s", - expectedPeriodNumber: 600, - }, - { - period: time.Duration(30) * time.Second, - expectedDuration: "-1m0s", - expectedPeriodNumber: 30, - }, - { - period: time.Duration(60) * time.Second, - expectedDuration: "-2m0s", - expectedPeriodNumber: 60, - }, - } - - for _, c := range cases { - duration, periodSec := convertPeriodToDuration(c.period) - assert.Equal(t, c.expectedDuration, duration) - assert.Equal(t, c.expectedPeriodNumber, periodSec) - } -} diff --git a/x-pack/metricbeat/module/aws/cloudwatch/cloudwatch.go b/x-pack/metricbeat/module/aws/cloudwatch/cloudwatch.go index 486b01e8cc3..73d52cfd6b3 100644 --- a/x-pack/metricbeat/module/aws/cloudwatch/cloudwatch.go +++ b/x-pack/metricbeat/module/aws/cloudwatch/cloudwatch.go @@ -91,10 +91,7 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) { // of an error set the Error field of mb.Event or simply call report.Error(). func (m *MetricSet) Fetch(report mb.ReporterV2) error { // Get startTime and endTime - startTime, endTime, err := aws.GetStartTimeEndTime(m.DurationString) - if err != nil { - return errors.Wrap(err, "error GetStartTimeEndTime") - } + startTime, endTime := aws.GetStartTimeEndTime(m.Period) // Get listMetrics and namespacesTotal from configuration listMetrics, namespacesTotal := readCloudwatchConfig(m.CloudwatchConfigs) @@ -102,7 +99,7 @@ func (m *MetricSet) Fetch(report mb.ReporterV2) error { awsConfig := m.MetricSet.AwsConfig.Copy() awsConfig.Region = regionName svcCloudwatch := cloudwatch.New(awsConfig) - err := createEvents(svcCloudwatch, listMetrics, regionName, m.PeriodInSec, startTime, endTime, report) + err := createEvents(svcCloudwatch, listMetrics, regionName, m.Period, startTime, endTime, report) if err != nil { return errors.Wrap(err, "createEvents failed") } @@ -124,7 +121,7 @@ func (m *MetricSet) Fetch(report mb.ReporterV2) error { continue } - err = createEvents(svcCloudwatch, listMetricsOutput, regionName, m.PeriodInSec, startTime, endTime, report) + err = createEvents(svcCloudwatch, listMetricsOutput, regionName, m.Period, startTime, endTime, report) if err != nil { return errors.Wrap(err, "createEvents failed for region "+regionName) } @@ -150,7 +147,7 @@ func readCloudwatchConfig(cloudwatchConfigs []Config) ([]cloudwatch.Metric, []st return listMetrics, namespacesTotal } -func constructMetricQueries(listMetricsOutput []cloudwatch.Metric, period int64) []cloudwatch.MetricDataQuery { +func constructMetricQueries(listMetricsOutput []cloudwatch.Metric, period time.Duration) []cloudwatch.MetricDataQuery { var metricDataQueries []cloudwatch.MetricDataQuery for i, listMetric := range listMetricsOutput { metricDataQuery := createMetricDataQuery(listMetric, i, period) @@ -181,15 +178,16 @@ func constructLabel(metric cloudwatch.Metric) string { return label } -func createMetricDataQuery(metric cloudwatch.Metric, index int, period int64) (metricDataQuery cloudwatch.MetricDataQuery) { +func createMetricDataQuery(metric cloudwatch.Metric, index int, period time.Duration) (metricDataQuery cloudwatch.MetricDataQuery) { statistic := "Average" id := "cw" + strconv.Itoa(index) label := constructLabel(metric) + periodInSec := int64(period.Seconds()) metricDataQuery = cloudwatch.MetricDataQuery{ Id: &id, MetricStat: &cloudwatch.MetricStat{ - Period: &period, + Period: &periodInSec, Stat: &statistic, Metric: &metric, }, @@ -267,7 +265,7 @@ func convertConfigToListMetrics(cloudwatchConfig Config, namespace string) cloud return listMetricsOutput } -func createEvents(svc cloudwatchiface.CloudWatchAPI, listMetricsTotal []cloudwatch.Metric, regionName string, period int, startTime time.Time, endTime time.Time, report mb.ReporterV2) error { +func createEvents(svc cloudwatchiface.CloudWatchAPI, listMetricsTotal []cloudwatch.Metric, regionName string, period time.Duration, startTime time.Time, endTime time.Time, report mb.ReporterV2) error { identifiers := getIdentifiers(listMetricsTotal) // Initialize events map per region, which stores one event per identifierValue events := map[string]mb.Event{} @@ -280,7 +278,7 @@ func createEvents(svc cloudwatchiface.CloudWatchAPI, listMetricsTotal []cloudwat var eventsNoIdentifier []mb.Event // Construct metricDataQueries - metricDataQueries := constructMetricQueries(listMetricsTotal, int64(period)) + metricDataQueries := constructMetricQueries(listMetricsTotal, period) if len(metricDataQueries) == 0 { return nil } diff --git a/x-pack/metricbeat/module/aws/ec2/ec2.go b/x-pack/metricbeat/module/aws/ec2/ec2.go index 13b280e9272..989fec8aac2 100644 --- a/x-pack/metricbeat/module/aws/ec2/ec2.go +++ b/x-pack/metricbeat/module/aws/ec2/ec2.go @@ -8,6 +8,7 @@ import ( "fmt" "strconv" "strings" + "time" "github.com/aws/aws-sdk-go-v2/service/cloudwatch" "github.com/aws/aws-sdk-go-v2/service/ec2" @@ -51,8 +52,8 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) { } // Check if period is set to be multiple of 60s or 300s - remainder300 := metricSet.PeriodInSec % 300 - remainder60 := metricSet.PeriodInSec % 60 + remainder300 := int(metricSet.Period.Seconds()) % 300 + remainder60 := int(metricSet.Period.Seconds()) % 60 if remainder300 != 0 || remainder60 != 0 { err := errors.New("period needs to be set to 60s (or a multiple of 60s) if detailed monitoring is " + "enabled for EC2 instances or set to 300s (or a multiple of 300s) if EC2 instances has basic monitoring. " + @@ -70,10 +71,7 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) { // of an error set the Error field of mb.Event or simply call report.Error(). func (m *MetricSet) Fetch(report mb.ReporterV2) error { // Get startTime and endTime - startTime, endTime, err := aws.GetStartTimeEndTime(m.DurationString) - if err != nil { - return errors.Wrap(err, "Error ParseDuration") - } + startTime, endTime := aws.GetStartTimeEndTime(m.Period) for _, regionName := range m.MetricSet.RegionsList { awsConfig := m.MetricSet.AwsConfig.Copy() @@ -102,7 +100,7 @@ func (m *MetricSet) Fetch(report mb.ReporterV2) error { var metricDataQueriesTotal []cloudwatch.MetricDataQuery for _, instanceID := range instanceIDs { - metricDataQueriesTotal = append(metricDataQueriesTotal, constructMetricQueries(listMetricsOutput, instanceID, m.PeriodInSec)...) + metricDataQueriesTotal = append(metricDataQueriesTotal, constructMetricQueries(listMetricsOutput, instanceID, m.Period)...) } var metricDataOutput []cloudwatch.MetricDataResult @@ -139,11 +137,11 @@ func (m *MetricSet) Fetch(report mb.ReporterV2) error { return nil } -func constructMetricQueries(listMetricsOutput []cloudwatch.Metric, instanceID string, periodInSec int) []cloudwatch.MetricDataQuery { +func constructMetricQueries(listMetricsOutput []cloudwatch.Metric, instanceID string, period time.Duration) []cloudwatch.MetricDataQuery { var metricDataQueries []cloudwatch.MetricDataQuery metricDataQueryEmpty := cloudwatch.MetricDataQuery{} for i, listMetric := range listMetricsOutput { - metricDataQuery := createMetricDataQuery(listMetric, instanceID, i, periodInSec) + metricDataQuery := createMetricDataQuery(listMetric, instanceID, i, period) if metricDataQuery == metricDataQueryEmpty { continue } @@ -260,9 +258,9 @@ func getInstancesPerRegion(svc ec2iface.EC2API) (instanceIDs []string, instances return } -func createMetricDataQuery(metric cloudwatch.Metric, instanceID string, index int, periodInSec int) (metricDataQuery cloudwatch.MetricDataQuery) { +func createMetricDataQuery(metric cloudwatch.Metric, instanceID string, index int, period time.Duration) (metricDataQuery cloudwatch.MetricDataQuery) { statistic := "Average" - period := int64(periodInSec) + periodInSeconds := int64(period.Seconds()) id := metricsetName + strconv.Itoa(index) metricDims := metric.Dimensions @@ -273,7 +271,7 @@ func createMetricDataQuery(metric cloudwatch.Metric, instanceID string, index in metricDataQuery = cloudwatch.MetricDataQuery{ Id: &id, MetricStat: &cloudwatch.MetricStat{ - Period: &period, + Period: &periodInSeconds, Stat: &statistic, Metric: &metric, }, diff --git a/x-pack/metricbeat/module/aws/ec2/ec2_test.go b/x-pack/metricbeat/module/aws/ec2/ec2_test.go index 802b957afc0..213851c6f2a 100644 --- a/x-pack/metricbeat/module/aws/ec2/ec2_test.go +++ b/x-pack/metricbeat/module/aws/ec2/ec2_test.go @@ -217,7 +217,7 @@ func TestConstructMetricQueries(t *testing.T) { } listMetricsOutput := []cloudwatch.Metric{listMetric} - metricDataQuery := constructMetricQueries(listMetricsOutput, instanceID, 300) + metricDataQuery := constructMetricQueries(listMetricsOutput, instanceID, 5*time.Minute) assert.Equal(t, 1, len(metricDataQuery)) assert.Equal(t, "i-123 CPUUtilization", *metricDataQuery[0].Label) assert.Equal(t, "Average", *metricDataQuery[0].MetricStat.Stat) diff --git a/x-pack/metricbeat/module/aws/s3_daily_storage/s3_daily_storage.go b/x-pack/metricbeat/module/aws/s3_daily_storage/s3_daily_storage.go index 3c3b8273b3a..29e613ed5e9 100644 --- a/x-pack/metricbeat/module/aws/s3_daily_storage/s3_daily_storage.go +++ b/x-pack/metricbeat/module/aws/s3_daily_storage/s3_daily_storage.go @@ -8,6 +8,7 @@ import ( "fmt" "strconv" "strings" + "time" "github.com/aws/aws-sdk-go-v2/service/cloudwatch" "github.com/pkg/errors" @@ -55,7 +56,7 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) { } // Check if period is set to be multiple of 86400s - remainder := metricSet.PeriodInSec % 86400 + remainder := int(metricSet.Period.Seconds()) % 86400 if remainder != 0 { err := errors.New("period needs to be set to 86400s (or a multiple of 86400s). " + "To avoid data missing or extra costs, please make sure period is set correctly " + @@ -74,10 +75,7 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) { func (m *MetricSet) Fetch(report mb.ReporterV2) error { namespace := "AWS/S3" // Get startTime and endTime - startTime, endTime, err := aws.GetStartTimeEndTime(m.DurationString) - if err != nil { - return errors.Wrap(err, "Error ParseDuration") - } + startTime, endTime := aws.GetStartTimeEndTime(m.Period) // GetMetricData for AWS S3 from Cloudwatch for _, regionName := range m.MetricSet.RegionsList { @@ -96,7 +94,7 @@ func (m *MetricSet) Fetch(report mb.ReporterV2) error { continue } - metricDataQueries := constructMetricQueries(listMetricsOutputs, m.PeriodInSec) + metricDataQueries := constructMetricQueries(listMetricsOutputs, m.Period) // Use metricDataQueries to make GetMetricData API calls metricDataOutputs, err := aws.GetMetricDataResults(metricDataQueries, svcCloudwatch, startTime, endTime) if err != nil { @@ -142,7 +140,7 @@ func getBucketNames(listMetricsOutputs []cloudwatch.Metric) (bucketNames []strin return } -func constructMetricQueries(listMetricsOutputs []cloudwatch.Metric, periodInSec int) []cloudwatch.MetricDataQuery { +func constructMetricQueries(listMetricsOutputs []cloudwatch.Metric, period time.Duration) []cloudwatch.MetricDataQuery { var metricDataQueries []cloudwatch.MetricDataQuery metricDataQueryEmpty := cloudwatch.MetricDataQuery{} metricNames := []string{"NumberOfObjects", "BucketSizeBytes"} @@ -151,7 +149,7 @@ func constructMetricQueries(listMetricsOutputs []cloudwatch.Metric, periodInSec continue } - metricDataQuery := createMetricDataQuery(listMetric, periodInSec, i) + metricDataQuery := createMetricDataQuery(listMetric, period, i) if metricDataQuery == metricDataQueryEmpty { continue } @@ -160,9 +158,9 @@ func constructMetricQueries(listMetricsOutputs []cloudwatch.Metric, periodInSec return metricDataQueries } -func createMetricDataQuery(metric cloudwatch.Metric, periodInSec int, index int) (metricDataQuery cloudwatch.MetricDataQuery) { +func createMetricDataQuery(metric cloudwatch.Metric, period time.Duration, index int) (metricDataQuery cloudwatch.MetricDataQuery) { statistic := "Average" - period := int64(periodInSec) + periodInSec := int64(period.Seconds()) id := "s3d" + strconv.Itoa(index) metricDims := metric.Dimensions bucketName := "" @@ -180,7 +178,7 @@ func createMetricDataQuery(metric cloudwatch.Metric, periodInSec int, index int) metricDataQuery = cloudwatch.MetricDataQuery{ Id: &id, MetricStat: &cloudwatch.MetricStat{ - Period: &period, + Period: &periodInSec, Stat: &statistic, Metric: &metric, }, diff --git a/x-pack/metricbeat/module/aws/s3_request/s3_request.go b/x-pack/metricbeat/module/aws/s3_request/s3_request.go index 30a4f491191..c81506ded91 100644 --- a/x-pack/metricbeat/module/aws/s3_request/s3_request.go +++ b/x-pack/metricbeat/module/aws/s3_request/s3_request.go @@ -8,6 +8,7 @@ import ( "fmt" "strconv" "strings" + "time" "github.com/aws/aws-sdk-go-v2/service/cloudwatch" "github.com/pkg/errors" @@ -54,7 +55,7 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) { } // Check if period is set to be multiple of 60s - remainder := metricSet.PeriodInSec % 60 + remainder := int(metricSet.Period.Seconds()) % 60 if remainder != 0 { err := errors.New("period needs to be set to 60s (or a multiple of 60s). " + "To avoid data missing or extra costs, please make sure period is set correctly " + @@ -73,10 +74,7 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) { func (m *MetricSet) Fetch(report mb.ReporterV2) error { namespace := "AWS/S3" // Get startTime and endTime - startTime, endTime, err := aws.GetStartTimeEndTime(m.DurationString) - if err != nil { - return errors.Wrap(err, "Error ParseDuration") - } + startTime, endTime := aws.GetStartTimeEndTime(m.Period) // GetMetricData for AWS S3 from Cloudwatch for _, regionName := range m.MetricSet.RegionsList { @@ -94,7 +92,7 @@ func (m *MetricSet) Fetch(report mb.ReporterV2) error { continue } - metricDataQueries := constructMetricQueries(listMetricsOutputs, m.PeriodInSec) + metricDataQueries := constructMetricQueries(listMetricsOutputs, m.Period) // This happens when S3 cloudwatch request metrics are not enabled. if len(metricDataQueries) == 0 { continue @@ -143,9 +141,9 @@ func getBucketNames(listMetricsOutputs []cloudwatch.Metric) (bucketNames []strin return } -func createMetricDataQuery(metric cloudwatch.Metric, periodInSec int, index int) (metricDataQuery cloudwatch.MetricDataQuery) { +func createMetricDataQuery(metric cloudwatch.Metric, period time.Duration, index int) (metricDataQuery cloudwatch.MetricDataQuery) { statistic := "Sum" - period := int64(periodInSec) + periodInSec := int64(period.Seconds()) id := "s3r" + strconv.Itoa(index) metricDims := metric.Dimensions bucketName := "" @@ -162,7 +160,7 @@ func createMetricDataQuery(metric cloudwatch.Metric, periodInSec int, index int) metricDataQuery = cloudwatch.MetricDataQuery{ Id: &id, MetricStat: &cloudwatch.MetricStat{ - Period: &period, + Period: &periodInSec, Stat: &statistic, Metric: &metric, }, @@ -171,7 +169,7 @@ func createMetricDataQuery(metric cloudwatch.Metric, periodInSec int, index int) return } -func constructMetricQueries(listMetricsOutputs []cloudwatch.Metric, periodInSec int) []cloudwatch.MetricDataQuery { +func constructMetricQueries(listMetricsOutputs []cloudwatch.Metric, period time.Duration) []cloudwatch.MetricDataQuery { var metricDataQueries []cloudwatch.MetricDataQuery metricDataQueryEmpty := cloudwatch.MetricDataQuery{} dailyMetricNames := []string{"NumberOfObjects", "BucketSizeBytes"} @@ -180,7 +178,7 @@ func constructMetricQueries(listMetricsOutputs []cloudwatch.Metric, periodInSec continue } - metricDataQuery := createMetricDataQuery(listMetric, periodInSec, i) + metricDataQuery := createMetricDataQuery(listMetric, period, i) if metricDataQuery == metricDataQueryEmpty { continue } diff --git a/x-pack/metricbeat/module/aws/sqs/sqs.go b/x-pack/metricbeat/module/aws/sqs/sqs.go index ffd6d560b01..d98984d4c3c 100644 --- a/x-pack/metricbeat/module/aws/sqs/sqs.go +++ b/x-pack/metricbeat/module/aws/sqs/sqs.go @@ -8,6 +8,7 @@ import ( "fmt" "strconv" "strings" + "time" "github.com/aws/aws-sdk-go-v2/service/cloudwatch" "github.com/aws/aws-sdk-go-v2/service/sqs" @@ -52,7 +53,7 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) { } // Check if period is set to be multiple of 300s - remainder := metricSet.PeriodInSec % 300 + remainder := int(metricSet.Period.Seconds()) % 300 if remainder != 0 { err := errors.New("period needs to be set to 300s (or a multiple of 300s). " + "To avoid data missing or extra costs, please make sure period is set correctly in config.yml") @@ -70,10 +71,7 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) { func (m *MetricSet) Fetch(report mb.ReporterV2) error { namespace := "AWS/SQS" // Get startTime and endTime - startTime, endTime, err := aws.GetStartTimeEndTime(m.DurationString) - if err != nil { - return errors.Wrap(err, "Error ParseDuration") - } + startTime, endTime := aws.GetStartTimeEndTime(m.Period) for _, regionName := range m.MetricSet.RegionsList { awsConfig := m.MetricSet.AwsConfig.Copy() @@ -104,7 +102,7 @@ func (m *MetricSet) Fetch(report mb.ReporterV2) error { } // Construct metricDataQueries - metricDataQueries := constructMetricQueries(listMetricsOutput, int64(m.PeriodInSec)) + metricDataQueries := constructMetricQueries(listMetricsOutput, m.Period) if len(metricDataQueries) == 0 { continue } @@ -141,7 +139,7 @@ func getQueueUrls(svc sqsiface.SQSAPI) ([]string, error) { return output.QueueUrls, nil } -func constructMetricQueries(listMetricsOutput []cloudwatch.Metric, period int64) []cloudwatch.MetricDataQuery { +func constructMetricQueries(listMetricsOutput []cloudwatch.Metric, period time.Duration) []cloudwatch.MetricDataQuery { var metricDataQueries []cloudwatch.MetricDataQuery for i, listMetric := range listMetricsOutput { metricDataQuery := createMetricDataQuery(listMetric, i, period) @@ -150,8 +148,9 @@ func constructMetricQueries(listMetricsOutput []cloudwatch.Metric, period int64) return metricDataQueries } -func createMetricDataQuery(metric cloudwatch.Metric, index int, period int64) (metricDataQuery cloudwatch.MetricDataQuery) { +func createMetricDataQuery(metric cloudwatch.Metric, index int, period time.Duration) (metricDataQuery cloudwatch.MetricDataQuery) { statistic := "Average" + periodInSec := int64(period.Seconds()) id := "sqs" + strconv.Itoa(index) metricDims := metric.Dimensions metricName := *metric.MetricName @@ -166,7 +165,7 @@ func createMetricDataQuery(metric cloudwatch.Metric, index int, period int64) (m metricDataQuery = cloudwatch.MetricDataQuery{ Id: &id, MetricStat: &cloudwatch.MetricStat{ - Period: &period, + Period: &periodInSec, Stat: &statistic, Metric: &metric, }, diff --git a/x-pack/metricbeat/module/aws/utils.go b/x-pack/metricbeat/module/aws/utils.go index 7ffab2a625f..ccaacdc193d 100644 --- a/x-pack/metricbeat/module/aws/utils.go +++ b/x-pack/metricbeat/module/aws/utils.go @@ -16,14 +16,11 @@ import ( ) // GetStartTimeEndTime function uses durationString to create startTime and endTime for queries. -func GetStartTimeEndTime(durationString string) (startTime time.Time, endTime time.Time, err error) { - endTime = time.Now() - duration, err := time.ParseDuration(durationString) - if err != nil { - return - } - startTime = endTime.Add(duration) - return startTime, endTime, nil +func GetStartTimeEndTime(period time.Duration) (time.Time, time.Time) { + endTime := time.Now() + // Set startTime double the period earlier than the endtime in order to + // make sure GetMetricDataRequest gets the latest data point for each metric. + return endTime.Add(period * -2), endTime } // GetListMetricsOutput function gets listMetrics results from cloudwatch per namespace for each region. diff --git a/x-pack/metricbeat/module/aws/utils_test.go b/x-pack/metricbeat/module/aws/utils_test.go index 3e539f4f122..03f53f71e4b 100644 --- a/x-pack/metricbeat/module/aws/utils_test.go +++ b/x-pack/metricbeat/module/aws/utils_test.go @@ -112,11 +112,10 @@ func TestGetListMetricsOutput(t *testing.T) { } func TestGetMetricDataPerRegion(t *testing.T) { - startTime, endTime, err := GetStartTimeEndTime("-10m") - assert.NoError(t, err) + startTime, endTime := GetStartTimeEndTime(10 * time.Minute) mockSvc := &MockCloudWatchClient{} - metricDataQueries := []cloudwatch.MetricDataQuery{} + var metricDataQueries []cloudwatch.MetricDataQuery getMetricDataOutput, err := getMetricDataPerRegion(metricDataQueries, nil, mockSvc, startTime, endTime) if err != nil { fmt.Println("failed getMetricDataPerRegion: ", err) @@ -142,8 +141,7 @@ func TestGetMetricDataPerRegion(t *testing.T) { } func TestGetMetricDataResults(t *testing.T) { - startTime, endTime, err := GetStartTimeEndTime("-10m") - assert.NoError(t, err) + startTime, endTime := GetStartTimeEndTime(10 * time.Minute) mockSvc := &MockCloudWatchClient{} metricInfo := cloudwatch.Metric{