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

[AWS] Collect metrics from linked accounts #35540

Merged
merged 11 commits into from May 26, 2023

Conversation

kaiyan-sheng
Copy link
Contributor

@kaiyan-sheng kaiyan-sheng commented May 22, 2023

What does this PR do?

This PR adds support for collecting metrics from linked accounts in AWS. IncludeLinkedAccounts is a new parameter offered in ListMetrics CloudWatch API for users to include metrics from source accounts when this API is called in a monitoring account. With this change, we also update the cloud.account.id to the account that the instance belongs to, either the monitoring account or one of the linked accounts.

Also I added dynamic label collection in this PR as well to collect the actual cloud.account.name for each metric. Dynamic label is supported as a part of the get-metric-data API call for CloudWatch. With adding ${PROP('AccountLabel')} as a part of the label for collecting cloudwatch metrics, we can get the label specified for the source account that owns the metric.

Checklist

  • My code follows the style guidelines of this project
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have made corresponding change to the default configuration files
  • I have added tests that prove my fix is effective or that my feature works
  • I have added an entry in CHANGELOG.next.asciidoc or CHANGELOG-developer.next.asciidoc.

Related issues

Use cases

Config 1: Collect metrics from a given namespace.

- module: aws
  period: 5m
  metricsets:
    - cloudwatch
  credential_profile_name: cross-account
  regions:
    - ap-northeast-1
  metrics:
    - namespace: AWS/EC2

Expected behavior: We should collect metrics from all instances both from the monitoring account and linked accounts.

Config 2: Collect a specific metric from a given instance.

- module: aws
  period: 5m
  metricsets:
    - cloudwatch
  credential_profile_name: cross-account
  regions:
    - ap-northeast-1
  metrics:
    - namespace: AWS/EC2
      name: [ "CPUUtilization" ]
      dimensions:
        - name: InstanceId
          value: i-041ee56b355341288
      statistic: [ "Average" ]

Expected behavior: We should find the instance from one of the linked accounts and collect the metric.

Config3: collect a specific metric from all instances both from the linked accounts and the monitoring account.

- module: aws
  period: 5m
  metricsets:
    - cloudwatch
  credential_profile_name: cross-account
  regions:
    - ap-northeast-1
  metrics:
    - namespace: AWS/EC2
      name: [ "CPUUtilization" ]
      statistic: [ "Average" ]

Expected behavior: We should see CPUUtilization metric for all instances from both the linked accounts and the monitoring acco
Screenshot 2023-05-24 at 3 23 34 PM
unt.

Sample Output

{
  "_index": ".ds-metricbeat-8.9.0-2023.05.24-000001",
  "_id": "MdCkT4gBdTd3f6iY6Eme",
  "_version": 1,
  "_score": 0,
  "_source": {
    "@timestamp": "2023-05-24T21:00:00.000Z",
    "metricset": {
      "name": "cloudwatch",
      "period": 300000
    },
    "service": {
      "type": "aws"
    },
    "cloud": {
      "region": "ap-northeast-1",
      "account": {
        "name": "Sxxxxxa",
        "id": "80xxxxxxxx66"
      },
      "provider": "aws"
    },
    "ecs": {
      "version": "8.0.0"
    },
    "host": {
      "hostname": "Kaiyans-MacBook-Pro.local",
      "architecture": "x86_64",
      "name": "Kaiyans-MacBook-Pro.local"
    },
    "aws": {
      "cloudwatch": {
        "namespace": "AWS/EC2"
      },
      "dimensions": {
        "InstanceId": "i-087df6035a52dc9fe"
      },
      "ec2": {
        "metrics": {
          "CPUUtilization": {
            "avg": 0.23333333333332956
          }
        }
      }
    },
    "event": {
      "dataset": "aws.cloudwatch",
      "module": "aws",
      "duration": 1811546083
    }
  }
}

@botelastic botelastic bot added the needs_team Indicates that the issue/PR needs a Team:* label label May 22, 2023
@mergify
Copy link
Contributor

mergify bot commented May 22, 2023

This pull request does not have a backport label.
If this is a bug or security fix, could you label this PR @kaiyan-sheng? 🙏.
For such, you'll need to label your PR with:

  • The upcoming major version of the Elastic Stack
  • The upcoming minor version of the Elastic Stack (if you're not pushing a breaking change)

To fixup this pull request, you need to add the backport labels for the needed
branches, such as:

  • backport-v8./d.0 is the label to automatically backport to the 8./d branch. /d is the digit

@elasticmachine
Copy link
Collaborator

elasticmachine commented May 23, 2023

💚 Build Succeeded

the below badges are clickable and redirect to their specific view in the CI or DOCS
Pipeline View Test View Changes Artifacts preview preview

Expand to view the summary

Build stats

  • Start Time: 2023-05-25T19:42:29.235+0000

  • Duration: 144 min 56 sec

Test stats 🧪

Test Results
Failed 0
Passed 26132
Skipped 1964
Total 28096

💚 Flaky test report

Tests succeeded.

🤖 GitHub comments

Expand to view the GitHub comments

To re-run your PR in the CI, just comment with:

  • /test : Re-trigger the build.

  • /package : Generate the packages and run the E2E tests.

  • /beats-tester : Run the installation tests with beats-tester.

  • run elasticsearch-ci/docs : Re-trigger the docs validation. (use unformatted text in the comment!)

@kaiyan-sheng kaiyan-sheng added the Team:Cloud-Monitoring Label for the Cloud Monitoring team label May 24, 2023
@botelastic botelastic bot removed the needs_team Indicates that the issue/PR needs a Team:* label label May 24, 2023
@kaiyan-sheng kaiyan-sheng marked this pull request as ready for review May 24, 2023 03:42
@kaiyan-sheng kaiyan-sheng requested review from a team as code owners May 24, 2023 03:42
@girodav girodav self-requested a review May 24, 2023 08:32
Copy link
Contributor

@tommyers-elastic tommyers-elastic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looking good kaiyan - thanks for the quick turnaround on this. couple of small questions. no blockers.

@@ -195,11 +199,16 @@ func (m *MetricSet) getCloudWatchBillingMetrics(
}
for valI, metricDataResultValue := range output.Values {
labels := strings.Split(*output.Label, labelSeparator)
event := mb.Event{}
if labels[accountIdIdx] != "" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

under what circumstances is this empty when we pass includeLinkedAccounts?


label := metricName + labelSeparator
label := metric.AccountID + labelSeparator + "${PROP('AccountLabel')}" + labelSeparator + metricName + labelSeparator
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit not related to this PR - it would be better if we could avoid the manual work of keeping this label in sync with the index constants used to parse it. one way would just be to wrap the label creation and parsing in a type; at least that way the indexing concerns don't bleed out into the code. let's not change it now, just a thought though.

x-pack/metricbeat/module/aws/utils_test.go Show resolved Hide resolved
@@ -338,7 +347,7 @@ func (m *MetricSet) addCostMetrics(metrics map[string]costexplorertypes.MetricVa
return event
}

func constructMetricQueries(listMetricsOutput []types.Metric, dataGranularity time.Duration) []types.MetricDataQuery {
func constructMetricQueries(listMetricsOutput []aws.MetricWithID, dataGranularity time.Duration) []types.MetricDataQuery {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit, not required for this PR

We should rename constructMetricQueries and createMetricDataQuery with something clearer (e.g createQueries vs createSingleQuery)

Copy link
Contributor

@girodav girodav left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this PR Kaiyan! I put some comments but none of the are deal breakers.

Copy link
Member

@cmacknz cmacknz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding codeowner approval for the go.mod changes. Did not look closely at the other parts of the PR.

@kaiyan-sheng
Copy link
Contributor Author

Thank you all for the review! I made another commit for some of the comments, and I will address the rest in a separate PR.

@kaiyan-sheng kaiyan-sheng added backport-v8.8.0 Automated backport with mergify and removed backport-v8.8.0 Automated backport with mergify labels May 25, 2023
@kaiyan-sheng kaiyan-sheng merged commit 217e658 into elastic:main May 26, 2023
116 checks passed
@kaiyan-sheng kaiyan-sheng deleted the include_linked_accounts branch May 26, 2023 03:53
@zmoog zmoog added backport-v7.17.0 Automated backport with mergify backport-v8.7.0 Automated backport with mergify backport-v8.8.0 Automated backport with mergify labels May 26, 2023
mergify bot pushed a commit that referenced this pull request May 26, 2023
mergify bot pushed a commit that referenced this pull request May 26, 2023
mergify bot pushed a commit that referenced this pull request May 26, 2023
(cherry picked from commit 217e658)

# Conflicts:
#	NOTICE.txt
#	go.mod
#	go.sum
#	metricbeat/docs/modules/aws.asciidoc
#	x-pack/metricbeat/module/aws/_meta/docs.asciidoc
#	x-pack/metricbeat/module/aws/billing/billing.go
#	x-pack/metricbeat/module/aws/cloudwatch/cloudwatch.go
#	x-pack/metricbeat/module/aws/cloudwatch/cloudwatch_test.go
#	x-pack/metricbeat/module/aws/utils.go
#	x-pack/metricbeat/module/aws/utils_test.go
zmoog pushed a commit that referenced this pull request May 26, 2023
(cherry picked from commit 217e658)

Co-authored-by: kaiyan-sheng <kaiyan.sheng@elastic.co>
zmoog pushed a commit that referenced this pull request May 30, 2023
kaiyan-sheng added a commit that referenced this pull request May 30, 2023
…5587)

* [AWS] Collect metrics from linked accounts (#35540)
(cherry picked from commit 217e658)

Co-authored-by: kaiyan-sheng <kaiyan.sheng@elastic.co>
Co-authored-by: Maurizio Branca <maurizio.branca@elastic.co>
@reakaleek reakaleek mentioned this pull request Jul 19, 2023
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-v7.17.0 Automated backport with mergify backport-v8.7.0 Automated backport with mergify backport-v8.8.0 Automated backport with mergify Team:Cloud-Monitoring Label for the Cloud Monitoring team
Projects
None yet
6 participants