Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Metricbeat] Add dedot to aws ec2 metricset and cloudwatch metricset #15844

Merged
merged 4 commits into from
Jan 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d

*Metricbeat*

- Add dedot for tags in ec2 metricset and cloudwatch metricset. {issue}15843[15843] {pull}15844[15844]
- Use RFC3339 format for timestamps collected using the SQL module. {pull}15847[15847]


*Packetbeat*


Expand Down
6 changes: 4 additions & 2 deletions x-pack/metricbeat/module/aws/cloudwatch/cloudwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/aws/aws-sdk-go-v2/service/resourcegroupstaggingapi/resourcegroupstaggingapiiface"
"github.com/pkg/errors"

"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/metricbeat/mb"
"github.com/elastic/beats/x-pack/metricbeat/module/aws"
)
Expand Down Expand Up @@ -514,10 +515,11 @@ func (m *MetricSet) createEvents(svcCloudwatch cloudwatchiface.ClientAPI, svcRes
events[identifierValue] = aws.InitEvent(regionName, m.AccountName, m.AccountID)
}
events[identifierValue] = insertRootFields(events[identifierValue], output.Values[timestampIdx], labels)

// By default, replace dot "." using under bar "_" for tag keys and values
for _, tag := range tags {
events[identifierValue].RootFields.Put("aws.tags."+*tag.Key, *tag.Value)
events[identifierValue].RootFields.Put("aws.tags."+common.DeDot(*tag.Key), common.DeDot(*tag.Value))
}

}
}
}
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 @@ -201,8 +201,9 @@ func (m *MetricSet) createCloudWatchEvents(getMetricDataResults []cloudwatch.Met
}
}

// By default, replace dot "." using under bar "_" for tag keys and values
for _, tag := range tags {
events[instanceID].ModuleFields.Put("tags."+*tag.Key, *tag.Value)
events[instanceID].ModuleFields.Put("tags."+common.DeDot(*tag.Key), common.DeDot(*tag.Value))
}

machineType, err := instanceOutput[instanceID].InstanceType.MarshalValue()
Expand Down
19 changes: 18 additions & 1 deletion x-pack/metricbeat/module/aws/ec2/ec2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ func (m *MockEC2Client) DescribeInstancesRequest(input *ec2.DescribeInstancesInp
privateDNSName := "ip-5-6-7-8.us-west-1.compute.internal"
privateIP := "5.6.7.8"

tags := []ec2.Tag{
{
Key: awssdk.String("app.kubernetes.io/name"),
Value: awssdk.String("foo"),
},
{
Key: awssdk.String("helm.sh/chart"),
Value: awssdk.String("foo-chart"),
},
}

instance := ec2.Instance{
InstanceId: awssdk.String(instanceID),
InstanceType: ec2.InstanceTypeT2Medium,
Expand All @@ -95,6 +106,7 @@ func (m *MockEC2Client) DescribeInstancesRequest(input *ec2.DescribeInstancesInp
PublicIpAddress: &publicIP,
PrivateDnsName: &privateDNSName,
PrivateIpAddress: &privateIP,
Tags: tags,
}

httpReq, _ := http.NewRequest("", "", nil)
Expand Down Expand Up @@ -126,7 +138,7 @@ func TestGetInstanceIDs(t *testing.T) {
assert.Equal(t, awssdk.String("us-west-1a"), instancesOutputs[instanceID].Placement.AvailabilityZone)
}

func TestCreateCloudWatchEvents(t *testing.T) {
func TestCreateCloudWatchEventsDedotTags(t *testing.T) {
expectedEvent := mb.Event{
RootFields: common.MapStr{
"cloud": common.MapStr{
Expand Down Expand Up @@ -156,6 +168,10 @@ func TestCreateCloudWatchEvents(t *testing.T) {
"ip": "5.6.7.8",
},
},
"tags": common.MapStr{
"app_kubernetes_io/name": "foo",
"helm_sh/chart": "foo-chart",
},
},
}
svcEC2Mock := &MockEC2Client{}
Expand Down Expand Up @@ -203,6 +219,7 @@ func TestCreateCloudWatchEvents(t *testing.T) {
assert.Equal(t, expectedEvent.RootFields, events[instanceID].RootFields)
assert.Equal(t, expectedEvent.MetricSetFields["cpu"], events[instanceID].MetricSetFields["cpu"])
assert.Equal(t, expectedEvent.MetricSetFields["instance"], events[instanceID].MetricSetFields["instance"])
assert.Equal(t, expectedEvent.MetricSetFields["tags"], events[instanceID].ModuleFields["tags"])
}

func TestConstructMetricQueries(t *testing.T) {
Expand Down