Skip to content

Commit 65dbf15

Browse files
authored
[Metricbeat] Add dedot to aws ec2 metricset and cloudwatch metricset (elastic#15844)
* Add dedot to aws ec2 metricset and cloudwatch metricset * Remove dedot config parameter
1 parent f8d6d50 commit 65dbf15

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed

CHANGELOG.next.asciidoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
6767

6868
*Metricbeat*
6969

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

72-
7373
*Packetbeat*
7474

7575

x-pack/metricbeat/module/aws/cloudwatch/cloudwatch.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/aws/aws-sdk-go-v2/service/resourcegroupstaggingapi/resourcegroupstaggingapiiface"
1818
"github.com/pkg/errors"
1919

20+
"github.com/elastic/beats/libbeat/common"
2021
"github.com/elastic/beats/metricbeat/mb"
2122
"github.com/elastic/beats/x-pack/metricbeat/module/aws"
2223
)
@@ -514,10 +515,11 @@ func (m *MetricSet) createEvents(svcCloudwatch cloudwatchiface.ClientAPI, svcRes
514515
events[identifierValue] = aws.InitEvent(regionName, m.AccountName, m.AccountID)
515516
}
516517
events[identifierValue] = insertRootFields(events[identifierValue], output.Values[timestampIdx], labels)
518+
519+
// By default, replace dot "." using under bar "_" for tag keys and values
517520
for _, tag := range tags {
518-
events[identifierValue].RootFields.Put("aws.tags."+*tag.Key, *tag.Value)
521+
events[identifierValue].RootFields.Put("aws.tags."+common.DeDot(*tag.Key), common.DeDot(*tag.Value))
519522
}
520-
521523
}
522524
}
523525
}

x-pack/metricbeat/module/aws/ec2/ec2.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,9 @@ func (m *MetricSet) createCloudWatchEvents(getMetricDataResults []cloudwatch.Met
201201
}
202202
}
203203

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

208209
machineType, err := instanceOutput[instanceID].InstanceType.MarshalValue()

x-pack/metricbeat/module/aws/ec2/ec2_test.go

+18-1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,17 @@ func (m *MockEC2Client) DescribeInstancesRequest(input *ec2.DescribeInstancesInp
7373
privateDNSName := "ip-5-6-7-8.us-west-1.compute.internal"
7474
privateIP := "5.6.7.8"
7575

76+
tags := []ec2.Tag{
77+
{
78+
Key: awssdk.String("app.kubernetes.io/name"),
79+
Value: awssdk.String("foo"),
80+
},
81+
{
82+
Key: awssdk.String("helm.sh/chart"),
83+
Value: awssdk.String("foo-chart"),
84+
},
85+
}
86+
7687
instance := ec2.Instance{
7788
InstanceId: awssdk.String(instanceID),
7889
InstanceType: ec2.InstanceTypeT2Medium,
@@ -95,6 +106,7 @@ func (m *MockEC2Client) DescribeInstancesRequest(input *ec2.DescribeInstancesInp
95106
PublicIpAddress: &publicIP,
96107
PrivateDnsName: &privateDNSName,
97108
PrivateIpAddress: &privateIP,
109+
Tags: tags,
98110
}
99111

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

129-
func TestCreateCloudWatchEvents(t *testing.T) {
141+
func TestCreateCloudWatchEventsDedotTags(t *testing.T) {
130142
expectedEvent := mb.Event{
131143
RootFields: common.MapStr{
132144
"cloud": common.MapStr{
@@ -156,6 +168,10 @@ func TestCreateCloudWatchEvents(t *testing.T) {
156168
"ip": "5.6.7.8",
157169
},
158170
},
171+
"tags": common.MapStr{
172+
"app_kubernetes_io/name": "foo",
173+
"helm_sh/chart": "foo-chart",
174+
},
159175
},
160176
}
161177
svcEC2Mock := &MockEC2Client{}
@@ -203,6 +219,7 @@ func TestCreateCloudWatchEvents(t *testing.T) {
203219
assert.Equal(t, expectedEvent.RootFields, events[instanceID].RootFields)
204220
assert.Equal(t, expectedEvent.MetricSetFields["cpu"], events[instanceID].MetricSetFields["cpu"])
205221
assert.Equal(t, expectedEvent.MetricSetFields["instance"], events[instanceID].MetricSetFields["instance"])
222+
assert.Equal(t, expectedEvent.MetricSetFields["tags"], events[instanceID].ModuleFields["tags"])
206223
}
207224

208225
func TestConstructMetricQueries(t *testing.T) {

0 commit comments

Comments
 (0)