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

[add_cloud_metadata] Remove logger for AWS/EC2 #36829

Merged
merged 5 commits into from
Nov 20, 2023
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
23 changes: 10 additions & 13 deletions libbeat/processors/add_cloud_metadata/provider_aws_ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ import (
"fmt"
"net/http"

"github.com/elastic/elastic-agent-libs/logp"
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: I dont think we need to move this import around

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We need to move it around because of the errors of one of the tests. They force it to be sorted through the tool goimport


awssdk "github.com/aws/aws-sdk-go-v2/aws"
awscfg "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/feature/ec2/imds"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"

"github.com/elastic/elastic-agent-libs/logp"
"github.com/elastic/elastic-agent-libs/mapstr"

conf "github.com/elastic/elastic-agent-libs/config"
Expand Down Expand Up @@ -80,29 +81,33 @@ func fetchRawProviderMetadata(
// LoadDefaultConfig loads the EC2 role credentials
awsConfig, err := awscfg.LoadDefaultConfig(context.TODO(), awscfg.WithHTTPClient(&client))
if err != nil {
logger.Warnf("error loading AWS default configuration: %s.", err)
constanca-m marked this conversation as resolved.
Show resolved Hide resolved
result.err = fmt.Errorf("failed loading AWS default configuration: %w", err)
return
}
awsClient := NewIMDSClient(awsConfig)

instanceIdentity, err := awsClient.GetInstanceIdentityDocument(context.TODO(), &imds.GetInstanceIdentityDocumentInput{})
if err != nil {
logger.Warnf("error fetching EC2 Identity Document: %s.", err)
result.err = fmt.Errorf("failed fetching EC2 Identity Document: %w", err)
return
}

// AWS Region must be set to be able to get EC2 Tags
awsRegion := instanceIdentity.InstanceIdentityDocument.Region
awsConfig.Region = awsRegion
accountID := instanceIdentity.InstanceIdentityDocument.AccountID

clusterName, err := fetchEC2ClusterNameTag(awsConfig, instanceIdentity.InstanceIdentityDocument.InstanceID)
if err != nil {
logger.Warnf("error fetching cluster name metadata: %s.", err)
constanca-m marked this conversation as resolved.
Show resolved Hide resolved
}
} else if clusterName != "" {
// for AWS cluster ID is used cluster ARN: arn:partition:service:region:account-id:resource-type/resource-id, example:
// arn:aws:eks:us-east-2:627286350134:cluster/cluster-name
clusterARN := fmt.Sprintf("arn:aws:eks:%s:%s:cluster/%v", awsRegion, accountID, clusterName)

accountID := instanceIdentity.InstanceIdentityDocument.AccountID
_, _ = result.metadata.Put("orchestrator.cluster.id", clusterARN)
_, _ = result.metadata.Put("orchestrator.cluster.name", clusterName)
}

_, _ = result.metadata.Put("instance.id", instanceIdentity.InstanceIdentityDocument.InstanceID)
_, _ = result.metadata.Put("machine.type", instanceIdentity.InstanceIdentityDocument.InstanceType)
Expand All @@ -111,14 +116,6 @@ func fetchRawProviderMetadata(
_, _ = result.metadata.Put("account.id", accountID)
_, _ = result.metadata.Put("image.id", instanceIdentity.InstanceIdentityDocument.ImageID)

// for AWS cluster ID is used cluster ARN: arn:partition:service:region:account-id:resource-type/resource-id, example:
// arn:aws:eks:us-east-2:627286350134:cluster/cluster-name
if clusterName != "" {
clusterARN := fmt.Sprintf("arn:aws:eks:%s:%s:cluster/%v", awsRegion, accountID, clusterName)

_, _ = result.metadata.Put("orchestrator.cluster.id", clusterARN)
_, _ = result.metadata.Put("orchestrator.cluster.name", clusterName)
}
}

func fetchEC2ClusterNameTag(awsConfig awssdk.Config, instanceID string) (string, error) {
Expand Down
6 changes: 4 additions & 2 deletions libbeat/processors/add_cloud_metadata/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func setupFetchers(providers map[string]provider, c *conf.C) ([]metadataFetcher,
mf := make([]metadataFetcher, 0, len(providers))
visited := map[string]bool{}

// Iterate over all providers and create an unique meta-data fetcher per provider type.
// Iterate over all providers and create a unique meta-data fetcher per provider type.
// Some providers might appear twice in the set of providers to support aliases on provider names.
// For example aws and ec2 both use the same provider.
// The loop tracks already seen providers in the `visited` set, to ensure that we do not create
Expand All @@ -123,7 +123,7 @@ func setupFetchers(providers map[string]provider, c *conf.C) ([]metadataFetcher,
}

// fetchMetadata attempts to fetch metadata in parallel from each of the
// hosting providers supported by this processor. It wait for the results to
// hosting providers supported by this processor. It will wait for the results to
// be returned or for a timeout to occur then returns the first result that
// completed in time.
func (p *addCloudMetadata) fetchMetadata() *result {
Expand Down Expand Up @@ -169,6 +169,8 @@ func (p *addCloudMetadata) fetchMetadata() *result {
// Bail out on first success.
if result.err == nil && result.metadata != nil {
return &result
} else if result.err != nil {
p.logger.Errorf("add_cloud_metadata: received error %v", result.err)
}
case <-ctx.Done():
p.logger.Debugf("add_cloud_metadata: timed-out waiting for all responses")
Expand Down
Loading