diff --git a/libbeat/processors/add_cloud_metadata/provider_aws_ec2.go b/libbeat/processors/add_cloud_metadata/provider_aws_ec2.go index 9918654728e..1f428372b86 100644 --- a/libbeat/processors/add_cloud_metadata/provider_aws_ec2.go +++ b/libbeat/processors/add_cloud_metadata/provider_aws_ec2.go @@ -22,13 +22,14 @@ import ( "fmt" "net/http" + "github.com/elastic/elastic-agent-libs/logp" + 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" @@ -80,7 +81,6 @@ 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) result.err = fmt.Errorf("failed loading AWS default configuration: %w", err) return } @@ -88,7 +88,6 @@ func fetchRawProviderMetadata( 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 } @@ -96,13 +95,19 @@ func fetchRawProviderMetadata( // 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) - } + } 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) @@ -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) { diff --git a/libbeat/processors/add_cloud_metadata/providers.go b/libbeat/processors/add_cloud_metadata/providers.go index 2b9f0d90646..55e68f75607 100644 --- a/libbeat/processors/add_cloud_metadata/providers.go +++ b/libbeat/processors/add_cloud_metadata/providers.go @@ -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 @@ -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 { @@ -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")