func getLastPoint(cloudWatch *cloudwatch.CloudWatch, dimensions *[]cloudwatch.Dimension, metricName string) (float64, error) { now := time.Now() response, err := cloudWatch.GetMetricStatistics(&cloudwatch.GetMetricStatisticsRequest{ Dimensions: *dimensions, StartTime: now.Add(time.Duration(180) * time.Second * -1), // 3 mins (to fetch at least 1 data-point) EndTime: now, MetricName: metricName, Period: 60, Statistics: []string{"Average"}, Namespace: "AWS/ElastiCache", }) if err != nil { return 0, err } datapoints := response.GetMetricStatisticsResult.Datapoints if len(datapoints) == 0 { return 0, errors.New("fetched no datapoints") } latest := time.Unix(0, 0) var latestVal float64 for _, dp := range datapoints { if dp.Timestamp.Before(latest) { continue } latest = dp.Timestamp latestVal = dp.Average } return latestVal, nil }
func getLastPointAverage(cw *cloudwatch.CloudWatch, dimension *cloudwatch.Dimension, metricName string) (float64, error) { namespace := "AWS/EC2" now := time.Now() prev := now.Add(time.Duration(600) * time.Second * -1) // 10 min (to fetch at least 1 data-point) request := &cloudwatch.GetMetricStatisticsRequest{ Dimensions: []cloudwatch.Dimension{*dimension}, EndTime: now, StartTime: prev, MetricName: metricName, Period: 60, Statistics: []string{"Average"}, Namespace: namespace, } response, err := cw.GetMetricStatistics(request) if err != nil { return 0, err } datapoints := response.GetMetricStatisticsResult.Datapoints if len(datapoints) == 0 { return 0, errors.New("fetched no datapoints") } latest := time.Unix(0, 0) var latestVal float64 for _, dp := range datapoints { if dp.Timestamp.Before(latest) { continue } latest = dp.Timestamp latestVal = dp.Average } return latestVal, nil }