Skip to content

Commit

Permalink
[Metricbeat] Add latency config option into aws module (#20875)
Browse files Browse the repository at this point in the history
* Add latency config option into aws module
  • Loading branch information
kaiyan-sheng authored Oct 5, 2020
1 parent 6fdb4ba commit 7400f43
Show file tree
Hide file tree
Showing 15 changed files with 57 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Add billing data collection from Cost Explorer into aws billing metricset. {pull}20527[20527] {issue}20103[20103]
- Migrate `compute_vm` metricset to a light one, map `cloud.instance.id` field. {pull}20889[20889]
- Request prometheus endpoints to be gzipped by default {pull}20766[20766]
- Add latency config parameter into aws module. {pull}20875[20875]
- Release all kubernetes `state` metricsets as GA {pull}20901[20901]
- Add billing metricset into googlecloud module. {pull}20812[20812] {issue}20738[20738]
- Move `compute_vm_scaleset` to light metricset. {pull}21038[21038] {issue}20985[20985]
Expand Down
11 changes: 11 additions & 0 deletions metricbeat/docs/modules/aws.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,27 @@ module. Please see <<aws-api-requests,AWS API requests>> for more details.
[float]
== Module-specific configuration notes

* *AWS Credentials*

The `aws` module requires AWS credentials configuration in order to make AWS API calls.
Users can either use `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY` and/or
`AWS_SESSION_TOKEN`, or use shared AWS credentials file.
Please see <<aws-credentials-config,AWS credentials options>> for more details.

* *regions*

This module also accepts optional configuration `regions` to specify which
AWS regions to query metrics from. If the `regions` parameter is not set in the
config file, then by default, the `aws` module will query metrics from all available
AWS regions.

* *latency*

Some AWS services send monitoring metrics to CloudWatch with a latency to
process larger than Metricbeat collection period. This case, please specify a
`latency` parameter so collection start time and end time will be shifted by the
given latency amount.

The aws module comes with a predefined dashboard. For example:

image::./images/metricbeat-aws-overview.png[]
Expand Down
4 changes: 4 additions & 0 deletions x-pack/metricbeat/module/aws/_meta/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,8 @@
period: 24h
metricsets:
- s3_daily_storage
- module: aws
period: 1m
latency: 5m
metricsets:
- s3_request
11 changes: 11 additions & 0 deletions x-pack/metricbeat/module/aws/_meta/docs.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,27 @@ module. Please see <<aws-api-requests,AWS API requests>> for more details.
[float]
== Module-specific configuration notes

* *AWS Credentials*

The `aws` module requires AWS credentials configuration in order to make AWS API calls.
Users can either use `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY` and/or
`AWS_SESSION_TOKEN`, or use shared AWS credentials file.
Please see <<aws-credentials-config,AWS credentials options>> for more details.

* *regions*

This module also accepts optional configuration `regions` to specify which
AWS regions to query metrics from. If the `regions` parameter is not set in the
config file, then by default, the `aws` module will query metrics from all available
AWS regions.

* *latency*

Some AWS services send monitoring metrics to CloudWatch with a latency to
process larger than Metricbeat collection period. This case, please specify a
`latency` parameter so collection start time and end time will be shifted by the
given latency amount.

The aws module comes with a predefined dashboard. For example:

image::./images/metricbeat-aws-overview.png[]
Expand Down
3 changes: 3 additions & 0 deletions x-pack/metricbeat/module/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
type Config struct {
Period time.Duration `config:"period" validate:"nonzero,required"`
Regions []string `config:"regions"`
Latency time.Duration `config:"latency"`
AWSConfig awscommon.ConfigAWS `config:",inline"`
TagsFilter []Tag `config:"tags_filter"`
}
Expand All @@ -37,6 +38,7 @@ type MetricSet struct {
RegionsList []string
Endpoint string
Period time.Duration
Latency time.Duration
AwsConfig *awssdk.Config
AccountName string
AccountID string
Expand Down Expand Up @@ -87,6 +89,7 @@ func NewMetricSet(base mb.BaseMetricSet) (*MetricSet, error) {
metricSet := MetricSet{
BaseMetricSet: base,
Period: config.Period,
Latency: config.Latency,
AwsConfig: &awsConfig,
TagsFilter: config.TagsFilter,
}
Expand Down
3 changes: 2 additions & 1 deletion x-pack/metricbeat/module/aws/cloudwatch/cloudwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ 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 := aws.GetStartTimeEndTime(m.Period)
startTime, endTime := aws.GetStartTimeEndTime(m.Period, m.Latency)
m.Logger().Debugf("startTime = %s, endTime = %s", startTime, endTime)

// Check statistic method in config
err := m.checkStatistics()
Expand Down
6 changes: 3 additions & 3 deletions x-pack/metricbeat/module/aws/cloudwatch/cloudwatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1357,7 +1357,7 @@ func TestCreateEventsWithIdentifier(t *testing.T) {
Value: "test-ec2",
},
}
startTime, endTime := aws.GetStartTimeEndTime(m.MetricSet.Period)
startTime, endTime := aws.GetStartTimeEndTime(m.MetricSet.Period, m.MetricSet.Latency)

events, err := m.createEvents(mockCloudwatchSvc, mockTaggingSvc, listMetricWithStatsTotal, resourceTypeTagFilters, regionName, startTime, endTime)
assert.NoError(t, err)
Expand Down Expand Up @@ -1399,7 +1399,7 @@ func TestCreateEventsWithoutIdentifier(t *testing.T) {
}

resourceTypeTagFilters := map[string][]aws.Tag{}
startTime, endTime := aws.GetStartTimeEndTime(m.MetricSet.Period)
startTime, endTime := aws.GetStartTimeEndTime(m.MetricSet.Period, m.MetricSet.Latency)

events, err := m.createEvents(mockCloudwatchSvc, mockTaggingSvc, listMetricWithStatsTotal, resourceTypeTagFilters, regionName, startTime, endTime)
assert.NoError(t, err)
Expand Down Expand Up @@ -1447,7 +1447,7 @@ func TestCreateEventsWithTagsFilter(t *testing.T) {
Value: "foo",
},
}
startTime, endTime := aws.GetStartTimeEndTime(m.MetricSet.Period)
startTime, endTime := aws.GetStartTimeEndTime(m.MetricSet.Period, m.MetricSet.Latency)

events, err := m.createEvents(mockCloudwatchSvc, mockTaggingSvc, listMetricWithStatsTotal, resourceTypeTagFilters, regionName, startTime, endTime)
assert.NoError(t, err)
Expand Down
3 changes: 2 additions & 1 deletion x-pack/metricbeat/module/aws/ec2/ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ 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 := aws.GetStartTimeEndTime(m.Period)
startTime, endTime := aws.GetStartTimeEndTime(m.Period, m.Latency)
m.Logger().Debugf("startTime = %s, endTime = %s", startTime, endTime)

for _, regionName := range m.MetricSet.RegionsList {
awsConfig := m.MetricSet.AwsConfig.Copy()
Expand Down
3 changes: 2 additions & 1 deletion x-pack/metricbeat/module/aws/rds/rds.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ 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 := aws.GetStartTimeEndTime(m.Period)
startTime, endTime := aws.GetStartTimeEndTime(m.Period, m.Latency)
m.Logger().Debugf("startTime = %s, endTime = %s", startTime, endTime)

for _, regionName := range m.MetricSet.RegionsList {
awsConfig := m.MetricSet.AwsConfig.Copy()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ 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 := aws.GetStartTimeEndTime(m.Period)
startTime, endTime := aws.GetStartTimeEndTime(m.Period, m.Latency)
m.Logger().Debugf("startTime = %s, endTime = %s", startTime, endTime)

// GetMetricData for AWS S3 from Cloudwatch
for _, regionName := range m.MetricSet.RegionsList {
Expand Down
3 changes: 2 additions & 1 deletion x-pack/metricbeat/module/aws/s3_request/s3_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ 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 := aws.GetStartTimeEndTime(m.Period)
startTime, endTime := aws.GetStartTimeEndTime(m.Period, m.Latency)
m.Logger().Debugf("startTime = %s, endTime = %s", startTime, endTime)

// GetMetricData for AWS S3 from Cloudwatch
for _, regionName := range m.MetricSet.RegionsList {
Expand Down
3 changes: 2 additions & 1 deletion x-pack/metricbeat/module/aws/sqs/sqs.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ 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 := aws.GetStartTimeEndTime(m.Period)
startTime, endTime := aws.GetStartTimeEndTime(m.Period, m.Latency)
m.Logger().Debugf("startTime = %s, endTime = %s", startTime, endTime)

for _, regionName := range m.MetricSet.RegionsList {
awsConfig := m.MetricSet.AwsConfig.Copy()
Expand Down
7 changes: 6 additions & 1 deletion x-pack/metricbeat/module/aws/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@ import (
)

// GetStartTimeEndTime function uses durationString to create startTime and endTime for queries.
func GetStartTimeEndTime(period time.Duration) (time.Time, time.Time) {
func GetStartTimeEndTime(period time.Duration, latency time.Duration) (time.Time, time.Time) {
endTime := time.Now()
if latency != 0 {
// add latency if config is not 0
endTime = endTime.Add(latency * -1)
}

// 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
Expand Down
4 changes: 2 additions & 2 deletions x-pack/metricbeat/module/aws/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func TestGetListMetricsOutputWithWildcard(t *testing.T) {
}

func TestGetMetricDataPerRegion(t *testing.T) {
startTime, endTime := GetStartTimeEndTime(10 * time.Minute)
startTime, endTime := GetStartTimeEndTime(10*time.Minute, 0)

mockSvc := &MockCloudWatchClient{}
var metricDataQueries []cloudwatch.MetricDataQuery
Expand Down Expand Up @@ -205,7 +205,7 @@ func TestGetMetricDataPerRegion(t *testing.T) {
}

func TestGetMetricDataResults(t *testing.T) {
startTime, endTime := GetStartTimeEndTime(10 * time.Minute)
startTime, endTime := GetStartTimeEndTime(10*time.Minute, 0)

mockSvc := &MockCloudWatchClient{}
metricInfo := cloudwatch.Metric{
Expand Down
4 changes: 4 additions & 0 deletions x-pack/metricbeat/modules.d/aws.yml.disabled
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,8 @@
period: 24h
metricsets:
- s3_daily_storage
- module: aws
period: 1m
latency: 5m
metricsets:
- s3_request

0 comments on commit 7400f43

Please sign in to comment.