Skip to content

Commit 2d3281d

Browse files
authored
Cherry-pick elastic#15844 to 7.5: [Metricbeat] Add dedot to aws ec2 metricset and cloudwatch metricset (elastic#15896)
* [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 (cherry picked from commit 65dbf15)
1 parent d3d6a44 commit 2d3281d

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

CHANGELOG.next.asciidoc

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
6666
- Fixed bug with `elasticsearch/cluster_stats` metricset not recording license expiration date correctly. {issue}14541[14541] {pull}14591[14591]
6767
- Log bulk failures from bulk API requests to monitoring cluster. {issue}14303[14303] {pull}14356[14356]
6868
- Fixed bug with `elasticsearch/cluster_stats` metricset not recording license ID in the correct field. {pull}14592[14592]
69+
- Add dedot for tags in ec2 metricset and cloudwatch metricset. {issue}15843[15843] {pull}15844[15844]
6970

7071
*Packetbeat*
7172

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
)
@@ -491,10 +492,11 @@ func (m *MetricSet) createEvents(svcCloudwatch cloudwatchiface.ClientAPI, svcRes
491492
events[identifierValue] = aws.InitEvent(regionName, m.AccountName, m.AccountID)
492493
}
493494
events[identifierValue] = insertRootFields(events[identifierValue], output.Values[timestampIdx], labels)
495+
496+
// By default, replace dot "." using under bar "_" for tag keys and values
494497
for _, tag := range tags {
495-
events[identifierValue].RootFields.Put("aws.tags."+*tag.Key, *tag.Value)
498+
events[identifierValue].RootFields.Put("aws.tags."+common.DeDot(*tag.Key), common.DeDot(*tag.Value))
496499
}
497-
498500
}
499501
}
500502
}

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)