2016-03-26 06:16:23 +08:00
|
|
|
package cloudwatch
|
|
|
|
|
|
|
|
|
|
import (
|
2021-10-22 05:32:10 +08:00
|
|
|
"context"
|
2021-02-27 02:58:28 +08:00
|
|
|
"net/http"
|
2022-06-22 04:50:06 +08:00
|
|
|
"net/url"
|
2016-03-26 06:16:23 +08:00
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
|
2021-10-22 05:32:10 +08:00
|
|
|
"github.com/aws/aws-sdk-go-v2/aws"
|
|
|
|
|
cwClient "github.com/aws/aws-sdk-go-v2/service/cloudwatch"
|
|
|
|
|
"github.com/aws/aws-sdk-go-v2/service/cloudwatch/types"
|
2019-04-23 08:36:46 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
2020-08-08 01:31:55 +08:00
|
|
|
"github.com/influxdata/telegraf/config"
|
2019-04-23 08:36:46 +08:00
|
|
|
"github.com/influxdata/telegraf/filter"
|
2022-08-17 01:04:30 +08:00
|
|
|
internalaws "github.com/influxdata/telegraf/plugins/common/aws"
|
2021-02-27 02:58:28 +08:00
|
|
|
"github.com/influxdata/telegraf/plugins/common/proxy"
|
2016-03-26 06:16:23 +08:00
|
|
|
"github.com/influxdata/telegraf/testutil"
|
|
|
|
|
)
|
|
|
|
|
|
2016-12-13 22:13:53 +08:00
|
|
|
type mockGatherCloudWatchClient struct{}
|
2016-03-26 06:16:23 +08:00
|
|
|
|
2022-11-09 01:41:17 +08:00
|
|
|
func (m *mockGatherCloudWatchClient) ListMetrics(
|
|
|
|
|
_ context.Context,
|
|
|
|
|
params *cwClient.ListMetricsInput,
|
|
|
|
|
_ ...func(*cwClient.Options),
|
|
|
|
|
) (*cwClient.ListMetricsOutput, error) {
|
2021-04-28 09:41:52 +08:00
|
|
|
return &cwClient.ListMetricsOutput{
|
2021-10-22 05:32:10 +08:00
|
|
|
Metrics: []types.Metric{
|
2018-10-20 04:32:54 +08:00
|
|
|
{
|
2019-04-23 08:36:46 +08:00
|
|
|
Namespace: params.Namespace,
|
|
|
|
|
MetricName: aws.String("Latency"),
|
2021-10-22 05:32:10 +08:00
|
|
|
Dimensions: []types.Dimension{
|
2019-04-23 08:36:46 +08:00
|
|
|
{
|
|
|
|
|
Name: aws.String("LoadBalancerName"),
|
|
|
|
|
Value: aws.String("p-example"),
|
|
|
|
|
},
|
|
|
|
|
},
|
2016-03-26 06:16:23 +08:00
|
|
|
},
|
|
|
|
|
},
|
2019-04-23 08:36:46 +08:00
|
|
|
}, nil
|
|
|
|
|
}
|
2016-03-26 06:16:23 +08:00
|
|
|
|
2022-11-09 01:41:17 +08:00
|
|
|
func (m *mockGatherCloudWatchClient) GetMetricData(
|
|
|
|
|
_ context.Context,
|
|
|
|
|
params *cwClient.GetMetricDataInput,
|
|
|
|
|
_ ...func(*cwClient.Options),
|
|
|
|
|
) (*cwClient.GetMetricDataOutput, error) {
|
2021-04-28 09:41:52 +08:00
|
|
|
return &cwClient.GetMetricDataOutput{
|
2021-10-22 05:32:10 +08:00
|
|
|
MetricDataResults: []types.MetricDataResult{
|
2019-04-23 08:36:46 +08:00
|
|
|
{
|
|
|
|
|
Id: aws.String("minimum_0_0"),
|
|
|
|
|
Label: aws.String("latency_minimum"),
|
2021-10-22 05:32:10 +08:00
|
|
|
StatusCode: types.StatusCodeComplete,
|
|
|
|
|
Timestamps: []time.Time{
|
|
|
|
|
*params.EndTime,
|
2019-04-23 08:36:46 +08:00
|
|
|
},
|
2021-10-22 05:32:10 +08:00
|
|
|
Values: []float64{0.1},
|
2019-04-23 08:36:46 +08:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Id: aws.String("maximum_0_0"),
|
|
|
|
|
Label: aws.String("latency_maximum"),
|
2021-10-22 05:32:10 +08:00
|
|
|
StatusCode: types.StatusCodeComplete,
|
|
|
|
|
Timestamps: []time.Time{
|
|
|
|
|
*params.EndTime,
|
2019-04-23 08:36:46 +08:00
|
|
|
},
|
2021-10-22 05:32:10 +08:00
|
|
|
Values: []float64{0.3},
|
2019-04-23 08:36:46 +08:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Id: aws.String("average_0_0"),
|
|
|
|
|
Label: aws.String("latency_average"),
|
2021-10-22 05:32:10 +08:00
|
|
|
StatusCode: types.StatusCodeComplete,
|
|
|
|
|
Timestamps: []time.Time{
|
|
|
|
|
*params.EndTime,
|
2019-04-23 08:36:46 +08:00
|
|
|
},
|
2021-10-22 05:32:10 +08:00
|
|
|
Values: []float64{0.2},
|
2019-04-23 08:36:46 +08:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Id: aws.String("sum_0_0"),
|
|
|
|
|
Label: aws.String("latency_sum"),
|
2021-10-22 05:32:10 +08:00
|
|
|
StatusCode: types.StatusCodeComplete,
|
|
|
|
|
Timestamps: []time.Time{
|
|
|
|
|
*params.EndTime,
|
2019-04-23 08:36:46 +08:00
|
|
|
},
|
2021-10-22 05:32:10 +08:00
|
|
|
Values: []float64{123},
|
2019-04-23 08:36:46 +08:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Id: aws.String("sample_count_0_0"),
|
|
|
|
|
Label: aws.String("latency_sample_count"),
|
2021-10-22 05:32:10 +08:00
|
|
|
StatusCode: types.StatusCodeComplete,
|
|
|
|
|
Timestamps: []time.Time{
|
|
|
|
|
*params.EndTime,
|
2019-04-23 08:36:46 +08:00
|
|
|
},
|
2021-10-22 05:32:10 +08:00
|
|
|
Values: []float64{100},
|
2019-04-23 08:36:46 +08:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}, nil
|
2016-03-26 06:16:23 +08:00
|
|
|
}
|
|
|
|
|
|
2019-04-23 08:36:46 +08:00
|
|
|
func TestSnakeCase(t *testing.T) {
|
2021-04-28 09:41:52 +08:00
|
|
|
require.Equal(t, "cluster_name", snakeCase("Cluster Name"))
|
|
|
|
|
require.Equal(t, "broker_id", snakeCase("Broker ID"))
|
2016-03-26 06:16:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestGather(t *testing.T) {
|
|
|
|
|
duration, _ := time.ParseDuration("1m")
|
2020-08-08 01:31:55 +08:00
|
|
|
internalDuration := config.Duration(duration)
|
2016-03-26 06:16:23 +08:00
|
|
|
c := &CloudWatch{
|
2021-08-04 05:29:26 +08:00
|
|
|
CredentialConfig: internalaws.CredentialConfig{
|
|
|
|
|
Region: "us-east-1",
|
|
|
|
|
},
|
2016-03-26 06:16:23 +08:00
|
|
|
Namespace: "AWS/ELB",
|
|
|
|
|
Delay: internalDuration,
|
|
|
|
|
Period: internalDuration,
|
2017-03-16 06:20:18 +08:00
|
|
|
RateLimit: 200,
|
2022-08-02 03:09:25 +08:00
|
|
|
BatchSize: 500,
|
2022-11-07 22:43:20 +08:00
|
|
|
Log: testutil.Logger{},
|
2016-03-26 06:16:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
|
|
2021-08-11 05:47:23 +08:00
|
|
|
require.NoError(t, c.Init())
|
|
|
|
|
c.client = &mockGatherCloudWatchClient{}
|
2021-04-28 09:41:52 +08:00
|
|
|
require.NoError(t, acc.GatherError(c.Gather))
|
2016-03-26 06:16:23 +08:00
|
|
|
|
|
|
|
|
fields := map[string]interface{}{}
|
|
|
|
|
fields["latency_minimum"] = 0.1
|
|
|
|
|
fields["latency_maximum"] = 0.3
|
|
|
|
|
fields["latency_average"] = 0.2
|
|
|
|
|
fields["latency_sum"] = 123.0
|
|
|
|
|
fields["latency_sample_count"] = 100.0
|
|
|
|
|
|
|
|
|
|
tags := map[string]string{}
|
|
|
|
|
tags["region"] = "us-east-1"
|
|
|
|
|
tags["load_balancer_name"] = "p-example"
|
|
|
|
|
|
2021-04-28 09:41:52 +08:00
|
|
|
require.True(t, acc.HasMeasurement("cloudwatch_aws_elb"))
|
2016-03-26 06:16:23 +08:00
|
|
|
acc.AssertContainsTaggedFields(t, "cloudwatch_aws_elb", fields, tags)
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-11 05:47:23 +08:00
|
|
|
func TestGather_MultipleNamespaces(t *testing.T) {
|
|
|
|
|
duration, _ := time.ParseDuration("1m")
|
|
|
|
|
internalDuration := config.Duration(duration)
|
|
|
|
|
c := &CloudWatch{
|
|
|
|
|
Namespaces: []string{"AWS/ELB", "AWS/EC2"},
|
|
|
|
|
Delay: internalDuration,
|
|
|
|
|
Period: internalDuration,
|
|
|
|
|
RateLimit: 200,
|
2022-08-02 03:09:25 +08:00
|
|
|
BatchSize: 500,
|
2022-11-07 22:43:20 +08:00
|
|
|
Log: testutil.Logger{},
|
2021-08-11 05:47:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
|
|
|
|
|
|
require.NoError(t, c.Init())
|
|
|
|
|
c.client = &mockGatherCloudWatchClient{}
|
|
|
|
|
require.NoError(t, acc.GatherError(c.Gather))
|
|
|
|
|
|
|
|
|
|
require.True(t, acc.HasMeasurement("cloudwatch_aws_elb"))
|
|
|
|
|
require.True(t, acc.HasMeasurement("cloudwatch_aws_ec2"))
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-13 22:13:53 +08:00
|
|
|
type mockSelectMetricsCloudWatchClient struct{}
|
|
|
|
|
|
2022-11-09 01:41:17 +08:00
|
|
|
func (m *mockSelectMetricsCloudWatchClient) ListMetrics(
|
|
|
|
|
_ context.Context,
|
|
|
|
|
params *cwClient.ListMetricsInput,
|
|
|
|
|
_ ...func(*cwClient.Options),
|
|
|
|
|
) (*cwClient.ListMetricsOutput, error) {
|
2021-10-22 05:32:10 +08:00
|
|
|
metrics := []types.Metric{}
|
2016-12-13 22:13:53 +08:00
|
|
|
// 4 metrics are available
|
|
|
|
|
metricNames := []string{"Latency", "RequestCount", "HealthyHostCount", "UnHealthyHostCount"}
|
|
|
|
|
// for 3 ELBs
|
|
|
|
|
loadBalancers := []string{"lb-1", "lb-2", "lb-3"}
|
|
|
|
|
// in 2 AZs
|
|
|
|
|
availabilityZones := []string{"us-east-1a", "us-east-1b"}
|
|
|
|
|
for _, m := range metricNames {
|
|
|
|
|
for _, lb := range loadBalancers {
|
|
|
|
|
// For each metric/ELB pair, we get an aggregate value across all AZs.
|
2021-10-22 05:32:10 +08:00
|
|
|
metrics = append(metrics, types.Metric{
|
2016-12-13 22:13:53 +08:00
|
|
|
Namespace: aws.String("AWS/ELB"),
|
|
|
|
|
MetricName: aws.String(m),
|
2021-10-22 05:32:10 +08:00
|
|
|
Dimensions: []types.Dimension{
|
2018-10-20 04:32:54 +08:00
|
|
|
{
|
2016-12-13 22:13:53 +08:00
|
|
|
Name: aws.String("LoadBalancerName"),
|
|
|
|
|
Value: aws.String(lb),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
for _, az := range availabilityZones {
|
|
|
|
|
// We get a metric for each metric/ELB/AZ triplet.
|
2021-10-22 05:32:10 +08:00
|
|
|
metrics = append(metrics, types.Metric{
|
2016-12-13 22:13:53 +08:00
|
|
|
Namespace: aws.String("AWS/ELB"),
|
|
|
|
|
MetricName: aws.String(m),
|
2021-10-22 05:32:10 +08:00
|
|
|
Dimensions: []types.Dimension{
|
2018-10-20 04:32:54 +08:00
|
|
|
{
|
2016-12-13 22:13:53 +08:00
|
|
|
Name: aws.String("LoadBalancerName"),
|
|
|
|
|
Value: aws.String(lb),
|
|
|
|
|
},
|
2018-10-20 04:32:54 +08:00
|
|
|
{
|
2016-12-13 22:13:53 +08:00
|
|
|
Name: aws.String("AvailabilityZone"),
|
|
|
|
|
Value: aws.String(az),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-28 09:41:52 +08:00
|
|
|
result := &cwClient.ListMetricsOutput{
|
2016-12-13 22:13:53 +08:00
|
|
|
Metrics: metrics,
|
|
|
|
|
}
|
|
|
|
|
return result, nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-09 01:41:17 +08:00
|
|
|
func (m *mockSelectMetricsCloudWatchClient) GetMetricData(
|
|
|
|
|
_ context.Context,
|
|
|
|
|
params *cwClient.GetMetricDataInput,
|
|
|
|
|
_ ...func(*cwClient.Options),
|
|
|
|
|
) (*cwClient.GetMetricDataOutput, error) {
|
2016-12-13 22:13:53 +08:00
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestSelectMetrics(t *testing.T) {
|
|
|
|
|
duration, _ := time.ParseDuration("1m")
|
2020-08-08 01:31:55 +08:00
|
|
|
internalDuration := config.Duration(duration)
|
2016-12-13 22:13:53 +08:00
|
|
|
c := &CloudWatch{
|
2021-08-04 05:29:26 +08:00
|
|
|
CredentialConfig: internalaws.CredentialConfig{
|
|
|
|
|
Region: "us-east-1",
|
|
|
|
|
},
|
2016-12-13 22:13:53 +08:00
|
|
|
Namespace: "AWS/ELB",
|
|
|
|
|
Delay: internalDuration,
|
|
|
|
|
Period: internalDuration,
|
2017-03-16 06:20:18 +08:00
|
|
|
RateLimit: 200,
|
2022-08-02 03:09:25 +08:00
|
|
|
BatchSize: 500,
|
2016-12-13 22:13:53 +08:00
|
|
|
Metrics: []*Metric{
|
2018-10-20 04:32:54 +08:00
|
|
|
{
|
2016-12-13 22:13:53 +08:00
|
|
|
MetricNames: []string{"Latency", "RequestCount"},
|
|
|
|
|
Dimensions: []*Dimension{
|
2018-10-20 04:32:54 +08:00
|
|
|
{
|
2016-12-13 22:13:53 +08:00
|
|
|
Name: "LoadBalancerName",
|
2021-04-21 05:29:58 +08:00
|
|
|
Value: "lb*",
|
2016-12-13 22:13:53 +08:00
|
|
|
},
|
2018-10-20 04:32:54 +08:00
|
|
|
{
|
2016-12-13 22:13:53 +08:00
|
|
|
Name: "AvailabilityZone",
|
2021-04-21 05:29:58 +08:00
|
|
|
Value: "us-east*",
|
2016-12-13 22:13:53 +08:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2022-11-07 22:43:20 +08:00
|
|
|
Log: testutil.Logger{},
|
2016-12-13 22:13:53 +08:00
|
|
|
}
|
2021-08-11 05:47:23 +08:00
|
|
|
require.NoError(t, c.Init())
|
2016-12-13 22:13:53 +08:00
|
|
|
c.client = &mockSelectMetricsCloudWatchClient{}
|
2019-04-23 08:36:46 +08:00
|
|
|
filtered, err := getFilteredMetrics(c)
|
2016-12-13 22:13:53 +08:00
|
|
|
// We've asked for 2 (out of 4) metrics, over all 3 load balancers in all 2
|
|
|
|
|
// AZs. We should get 12 metrics.
|
2021-04-28 09:41:52 +08:00
|
|
|
require.Equal(t, 12, len(filtered[0].metrics))
|
|
|
|
|
require.NoError(t, err)
|
2016-12-13 22:13:53 +08:00
|
|
|
}
|
|
|
|
|
|
2016-03-26 06:16:23 +08:00
|
|
|
func TestGenerateStatisticsInputParams(t *testing.T) {
|
2021-10-22 05:32:10 +08:00
|
|
|
d := types.Dimension{
|
2016-03-26 06:16:23 +08:00
|
|
|
Name: aws.String("LoadBalancerName"),
|
|
|
|
|
Value: aws.String("p-example"),
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-11 05:47:23 +08:00
|
|
|
namespace := "AWS/ELB"
|
2021-10-22 05:32:10 +08:00
|
|
|
m := types.Metric{
|
2016-03-26 06:16:23 +08:00
|
|
|
MetricName: aws.String("Latency"),
|
2021-10-22 05:32:10 +08:00
|
|
|
Dimensions: []types.Dimension{d},
|
|
|
|
|
Namespace: aws.String(namespace),
|
2016-03-26 06:16:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
duration, _ := time.ParseDuration("1m")
|
2020-08-08 01:31:55 +08:00
|
|
|
internalDuration := config.Duration(duration)
|
2016-03-26 06:16:23 +08:00
|
|
|
|
|
|
|
|
c := &CloudWatch{
|
2021-08-11 05:47:23 +08:00
|
|
|
Namespaces: []string{namespace},
|
|
|
|
|
Delay: internalDuration,
|
|
|
|
|
Period: internalDuration,
|
2022-08-02 03:09:25 +08:00
|
|
|
BatchSize: 500,
|
2022-11-07 22:43:20 +08:00
|
|
|
Log: testutil.Logger{},
|
2016-03-26 06:16:23 +08:00
|
|
|
}
|
|
|
|
|
|
2021-04-23 05:08:03 +08:00
|
|
|
require.NoError(t, c.initializeCloudWatch())
|
2016-03-26 06:16:23 +08:00
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
|
|
2018-09-12 05:59:39 +08:00
|
|
|
c.updateWindow(now)
|
|
|
|
|
|
2019-04-23 08:36:46 +08:00
|
|
|
statFilter, _ := filter.NewIncludeExcludeFilter(nil, nil)
|
2021-10-22 05:32:10 +08:00
|
|
|
queries := c.getDataQueries([]filteredMetric{{metrics: []types.Metric{m}, statFilter: statFilter}})
|
2021-08-11 05:47:23 +08:00
|
|
|
params := c.getDataInputs(queries[namespace])
|
2019-04-23 08:36:46 +08:00
|
|
|
|
2021-04-28 09:41:52 +08:00
|
|
|
require.EqualValues(t, *params.EndTime, now.Add(-time.Duration(c.Delay)))
|
|
|
|
|
require.EqualValues(t, *params.StartTime, now.Add(-time.Duration(c.Period)).Add(-time.Duration(c.Delay)))
|
2019-04-23 08:36:46 +08:00
|
|
|
require.Len(t, params.MetricDataQueries, 5)
|
2021-04-28 09:41:52 +08:00
|
|
|
require.Len(t, params.MetricDataQueries[0].MetricStat.Metric.Dimensions, 1)
|
|
|
|
|
require.EqualValues(t, *params.MetricDataQueries[0].MetricStat.Period, 60)
|
2019-04-23 08:36:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestGenerateStatisticsInputParamsFiltered(t *testing.T) {
|
2021-10-22 05:32:10 +08:00
|
|
|
d := types.Dimension{
|
2019-04-23 08:36:46 +08:00
|
|
|
Name: aws.String("LoadBalancerName"),
|
|
|
|
|
Value: aws.String("p-example"),
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-11 05:47:23 +08:00
|
|
|
namespace := "AWS/ELB"
|
2021-10-22 05:32:10 +08:00
|
|
|
m := types.Metric{
|
2019-04-23 08:36:46 +08:00
|
|
|
MetricName: aws.String("Latency"),
|
2021-10-22 05:32:10 +08:00
|
|
|
Dimensions: []types.Dimension{d},
|
|
|
|
|
Namespace: aws.String(namespace),
|
2019-04-23 08:36:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
duration, _ := time.ParseDuration("1m")
|
2020-08-08 01:31:55 +08:00
|
|
|
internalDuration := config.Duration(duration)
|
2019-04-23 08:36:46 +08:00
|
|
|
|
|
|
|
|
c := &CloudWatch{
|
2021-08-11 05:47:23 +08:00
|
|
|
Namespaces: []string{namespace},
|
|
|
|
|
Delay: internalDuration,
|
|
|
|
|
Period: internalDuration,
|
2022-08-02 03:09:25 +08:00
|
|
|
BatchSize: 500,
|
2022-11-07 22:43:20 +08:00
|
|
|
Log: testutil.Logger{},
|
2019-04-23 08:36:46 +08:00
|
|
|
}
|
|
|
|
|
|
2021-04-23 05:08:03 +08:00
|
|
|
require.NoError(t, c.initializeCloudWatch())
|
2019-04-23 08:36:46 +08:00
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
|
|
c.updateWindow(now)
|
|
|
|
|
|
|
|
|
|
statFilter, _ := filter.NewIncludeExcludeFilter([]string{"average", "sample_count"}, nil)
|
2021-10-22 05:32:10 +08:00
|
|
|
queries := c.getDataQueries([]filteredMetric{{metrics: []types.Metric{m}, statFilter: statFilter}})
|
2021-08-11 05:47:23 +08:00
|
|
|
params := c.getDataInputs(queries[namespace])
|
2016-03-26 06:16:23 +08:00
|
|
|
|
2021-04-28 09:41:52 +08:00
|
|
|
require.EqualValues(t, *params.EndTime, now.Add(-time.Duration(c.Delay)))
|
|
|
|
|
require.EqualValues(t, *params.StartTime, now.Add(-time.Duration(c.Period)).Add(-time.Duration(c.Delay)))
|
2019-04-23 08:36:46 +08:00
|
|
|
require.Len(t, params.MetricDataQueries, 2)
|
2021-04-28 09:41:52 +08:00
|
|
|
require.Len(t, params.MetricDataQueries[0].MetricStat.Metric.Dimensions, 1)
|
|
|
|
|
require.EqualValues(t, *params.MetricDataQueries[0].MetricStat.Period, 60)
|
2016-03-26 06:16:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestMetricsCacheTimeout(t *testing.T) {
|
2019-04-23 08:36:46 +08:00
|
|
|
cache := &metricCache{
|
|
|
|
|
metrics: []filteredMetric{},
|
|
|
|
|
built: time.Now(),
|
|
|
|
|
ttl: time.Minute,
|
2016-03-26 06:16:23 +08:00
|
|
|
}
|
|
|
|
|
|
2021-04-28 09:41:52 +08:00
|
|
|
require.True(t, cache.isValid())
|
2019-04-23 08:36:46 +08:00
|
|
|
cache.built = time.Now().Add(-time.Minute)
|
2021-04-28 09:41:52 +08:00
|
|
|
require.False(t, cache.isValid())
|
2016-03-26 06:16:23 +08:00
|
|
|
}
|
2018-09-12 05:59:39 +08:00
|
|
|
|
|
|
|
|
func TestUpdateWindow(t *testing.T) {
|
|
|
|
|
duration, _ := time.ParseDuration("1m")
|
2020-08-08 01:31:55 +08:00
|
|
|
internalDuration := config.Duration(duration)
|
2018-09-12 05:59:39 +08:00
|
|
|
|
|
|
|
|
c := &CloudWatch{
|
|
|
|
|
Namespace: "AWS/ELB",
|
|
|
|
|
Delay: internalDuration,
|
|
|
|
|
Period: internalDuration,
|
2022-08-02 03:09:25 +08:00
|
|
|
BatchSize: 500,
|
2022-11-07 22:43:20 +08:00
|
|
|
Log: testutil.Logger{},
|
2018-09-12 05:59:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
|
|
2021-04-28 09:41:52 +08:00
|
|
|
require.True(t, c.windowEnd.IsZero())
|
|
|
|
|
require.True(t, c.windowStart.IsZero())
|
2018-09-12 05:59:39 +08:00
|
|
|
|
|
|
|
|
c.updateWindow(now)
|
|
|
|
|
|
|
|
|
|
newStartTime := c.windowEnd
|
|
|
|
|
|
|
|
|
|
// initial window just has a single period
|
2021-04-28 09:41:52 +08:00
|
|
|
require.EqualValues(t, c.windowEnd, now.Add(-time.Duration(c.Delay)))
|
|
|
|
|
require.EqualValues(t, c.windowStart, now.Add(-time.Duration(c.Delay)).Add(-time.Duration(c.Period)))
|
2018-09-12 05:59:39 +08:00
|
|
|
|
|
|
|
|
now = time.Now()
|
|
|
|
|
c.updateWindow(now)
|
|
|
|
|
|
|
|
|
|
// subsequent window uses previous end time as start time
|
2021-04-28 09:41:52 +08:00
|
|
|
require.EqualValues(t, c.windowEnd, now.Add(-time.Duration(c.Delay)))
|
|
|
|
|
require.EqualValues(t, c.windowStart, newStartTime)
|
2018-09-12 05:59:39 +08:00
|
|
|
}
|
2021-02-27 02:58:28 +08:00
|
|
|
|
|
|
|
|
func TestProxyFunction(t *testing.T) {
|
|
|
|
|
c := &CloudWatch{
|
2022-06-22 04:50:06 +08:00
|
|
|
HTTPProxy: proxy.HTTPProxy{
|
|
|
|
|
HTTPProxyURL: "http://www.penguins.com",
|
|
|
|
|
},
|
2022-08-02 03:09:25 +08:00
|
|
|
BatchSize: 500,
|
2022-11-07 22:43:20 +08:00
|
|
|
Log: testutil.Logger{},
|
2021-02-27 02:58:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
proxyFunction, err := c.HTTPProxy.Proxy()
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
2022-06-22 04:50:06 +08:00
|
|
|
u, err := url.Parse("https://monitoring.us-west-1.amazonaws.com/")
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
proxyResult, err := proxyFunction(&http.Request{URL: u})
|
2021-02-27 02:58:28 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.Equal(t, "www.penguins.com", proxyResult.Host)
|
|
|
|
|
}
|
2021-08-11 05:47:23 +08:00
|
|
|
|
|
|
|
|
func TestCombineNamespaces(t *testing.T) {
|
2022-08-02 03:09:25 +08:00
|
|
|
c := &CloudWatch{
|
|
|
|
|
Namespace: "AWS/ELB",
|
|
|
|
|
Namespaces: []string{"AWS/EC2", "AWS/Billing"},
|
|
|
|
|
BatchSize: 500,
|
2022-11-07 22:43:20 +08:00
|
|
|
Log: testutil.Logger{},
|
2022-08-02 03:09:25 +08:00
|
|
|
}
|
2021-08-11 05:47:23 +08:00
|
|
|
|
|
|
|
|
require.NoError(t, c.Init())
|
|
|
|
|
require.Equal(t, []string{"AWS/EC2", "AWS/Billing", "AWS/ELB"}, c.Namespaces)
|
|
|
|
|
}
|