Skip to content

Commit

Permalink
[AWS] Add tags to events based on parsed identifier (#33472)
Browse files Browse the repository at this point in the history
(cherry picked from commit 50c6ae5)

# Conflicts:
#	x-pack/metricbeat/module/aws/cloudwatch/cloudwatch.go
#	x-pack/metricbeat/module/aws/cloudwatch/cloudwatch_test.go
  • Loading branch information
kaiyan-sheng authored and mergify[bot] committed Nov 1, 2022
1 parent 44c5573 commit 15a0cc4
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ https://github.com/elastic/beats/compare/v8.2.0\...main[Check the HEAD diff]

*Metricbeat*

- Fix GCP storage field naming {pull}32806[32806]
- in module/windows/perfmon, changed collection method of the second counter value required to create a displayable value {pull}32305[32305]
- Fix and improve AWS metric period calculation to avoid zero-length intervals {pull}32724[32724]
- Add missing cluster metadata to k8s module metricsets {pull}32979[32979] {pull}33032[33032]
- Change max query size for GetMetricData API to 500 and add RecentlyActive for ListMetrics API call {pull}33105[33105]
- Add GCP CloudSQL region filter {pull}32943[32943]
- Fix logstash cgroup mappings {pull}33131[33131]
- Remove unused `elasticsearch.node_stats.indices.bulk.avg_time.bytes` mapping {pull}33263[33263]
- Add tags to events based on parsed identifier. {pull}33472[33472]

*Packetbeat*

Expand Down
41 changes: 41 additions & 0 deletions x-pack/metricbeat/module/aws/cloudwatch/cloudwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,7 @@ func (m *MetricSet) createEvents(svcCloudwatch cloudwatch.GetMetricDataAPIClient
}

identifierValue := labels[identifierValueIdx]
<<<<<<< HEAD
if _, ok := events[identifierValue]; !ok {
// when tagsFilter is not empty but no entry in
// resourceTagMap for this identifier, do not initialize
Expand All @@ -586,6 +587,29 @@ func (m *MetricSet) createEvents(svcCloudwatch cloudwatch.GetMetricDataAPIClient

// add tags to event based on identifierValue
insertTags(events, identifierValue, resourceTagMap)
=======
uniqueIdentifierValue := *output.Label + fmt.Sprint("-", valI)

// add tags to event based on identifierValue
// Check if identifier includes dimensionSeparator (comma in this case),
// split the identifier and check for each sub-identifier.
// For example, identifier might be [storageType, s3BucketName].
// And tags are only store under s3BucketName in resourceTagMap.
subIdentifiers := strings.Split(identifierValue, dimensionSeparator)
for _, subIdentifier := range subIdentifiers {
if _, ok := events[uniqueIdentifierValue]; !ok {
// when tagsFilter is not empty but no entry in
// resourceTagMap for this identifier, do not initialize
// an event for this identifier.
if len(tagsFilter) != 0 && resourceTagMap[subIdentifier] == nil {
continue
}
events[uniqueIdentifierValue] = aws.InitEvent(regionName, m.AccountName, m.AccountID, output.Timestamps[valI])
}
events[uniqueIdentifierValue] = insertRootFields(events[uniqueIdentifierValue], metricDataResultValue, labels)
insertTags(events, uniqueIdentifierValue, subIdentifier, resourceTagMap)
}
>>>>>>> 50c6ae5f3a ([AWS] Add tags to events based on parsed identifier (#33472))
}
}
}
Expand Down Expand Up @@ -625,6 +649,7 @@ func compareAWSDimensions(dim1 []types.Dimension, dim2 []types.Dimension) bool {
return reflect.DeepEqual(dim1NameToValue, dim2NameToValue)
}

<<<<<<< HEAD
func insertTags(events map[string]mb.Event, identifier string, resourceTagMap map[string][]resourcegroupstaggingapitypes.Tag) {
// Check if identifier includes dimensionSeparator (comma in this case),
// split the identifier and check for each sub-identifier.
Expand All @@ -647,6 +672,22 @@ func insertTags(events map[string]mb.Event, identifier string, resourceTagMap ma
_, _ = events[identifier].RootFields.Put("aws.tags."+common.DeDot(*tag.Key), *tag.Value)
}
continue
=======
func insertTags(events map[string]mb.Event, uniqueIdentifierValue string, subIdentifier string, resourceTagMap map[string][]resourcegroupstaggingapitypes.Tag) {
tags := resourceTagMap[subIdentifier]
// some metric dimension values are arn format, eg: AWS/DDOS namespace metric
if len(tags) == 0 && strings.HasPrefix(subIdentifier, "arn:") {
resourceID, err := aws.FindShortIdentifierFromARN(subIdentifier)
if err == nil {
tags = resourceTagMap[resourceID]
}
}
if len(tags) != 0 {
// By default, replace dot "." using underscore "_" for tag keys.
// Note: tag values are not dedotted.
for _, tag := range tags {
_, _ = events[uniqueIdentifierValue].RootFields.Put("aws.tags."+common.DeDot(*tag.Key), *tag.Value)
>>>>>>> 50c6ae5f3a ([AWS] Add tags to events based on parsed identifier (#33472))
}
}
}
8 changes: 8 additions & 0 deletions x-pack/metricbeat/module/aws/cloudwatch/cloudwatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package cloudwatch
import (
"context"
"errors"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -1560,7 +1561,14 @@ func TestInsertTags(t *testing.T) {

for _, c := range cases {
t.Run(c.title, func(t *testing.T) {
<<<<<<< HEAD
insertTags(events, c.identifier, resourceTagMap)
=======
subIdentifiers := strings.Split(c.identifier, dimensionSeparator)
for _, subIdentifier := range subIdentifiers {
insertTags(events, c.identifier, subIdentifier, resourceTagMap)
}
>>>>>>> 50c6ae5f3a ([AWS] Add tags to events based on parsed identifier (#33472))
value, err := events[c.identifier].RootFields.GetValue(c.expectedTagKey)
assert.NoError(t, err)
assert.Equal(t, c.expectedTagValue, value)
Expand Down

0 comments on commit 15a0cc4

Please sign in to comment.