From 5ee94b1cdbf6b8a20789d086a5e21290d00c25f7 Mon Sep 17 00:00:00 2001 From: constanca-m Date: Thu, 12 Oct 2023 16:38:27 +0200 Subject: [PATCH 1/4] Remove logger. --- .../processors/add_cloud_metadata/provider_aws_ec2.go | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/libbeat/processors/add_cloud_metadata/provider_aws_ec2.go b/libbeat/processors/add_cloud_metadata/provider_aws_ec2.go index 9918654728e3..a3207491d0ac 100644 --- a/libbeat/processors/add_cloud_metadata/provider_aws_ec2.go +++ b/libbeat/processors/add_cloud_metadata/provider_aws_ec2.go @@ -28,7 +28,6 @@ import ( "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" @@ -75,12 +74,9 @@ func fetchRawProviderMetadata( client http.Client, result *result, ) { - logger := logp.NewLogger("add_cloud_metadata") - // 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 +84,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 } @@ -97,10 +92,7 @@ func fetchRawProviderMetadata( awsRegion := instanceIdentity.InstanceIdentityDocument.Region awsConfig.Region = awsRegion - clusterName, err := fetchEC2ClusterNameTag(awsConfig, instanceIdentity.InstanceIdentityDocument.InstanceID) - if err != nil { - logger.Warnf("error fetching cluster name metadata: %s.", err) - } + clusterName, _ := fetchEC2ClusterNameTag(awsConfig, instanceIdentity.InstanceIdentityDocument.InstanceID) accountID := instanceIdentity.InstanceIdentityDocument.AccountID From 2970b8a54950f8492daf020f6b3947ff049211a4 Mon Sep 17 00:00:00 2001 From: constanca-m Date: Fri, 27 Oct 2023 14:21:36 +0200 Subject: [PATCH 2/4] Add error message. --- libbeat/processors/add_cloud_metadata/providers.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/libbeat/processors/add_cloud_metadata/providers.go b/libbeat/processors/add_cloud_metadata/providers.go index 2b9f0d906464..55e68f756071 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") From 6b5aa7a53d1763341be02f7d3af5bc114fca19e6 Mon Sep 17 00:00:00 2001 From: constanca-m Date: Wed, 1 Nov 2023 09:14:04 +0100 Subject: [PATCH 3/4] Add log message --- .../add_cloud_metadata/provider_aws_ec2.go | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/libbeat/processors/add_cloud_metadata/provider_aws_ec2.go b/libbeat/processors/add_cloud_metadata/provider_aws_ec2.go index a3207491d0ac..b6c5f3ad7bcf 100644 --- a/libbeat/processors/add_cloud_metadata/provider_aws_ec2.go +++ b/libbeat/processors/add_cloud_metadata/provider_aws_ec2.go @@ -20,6 +20,7 @@ package add_cloud_metadata import ( "context" "fmt" + "github.com/elastic/elastic-agent-libs/logp" "net/http" awssdk "github.com/aws/aws-sdk-go-v2/aws" @@ -74,6 +75,8 @@ func fetchRawProviderMetadata( client http.Client, result *result, ) { + logger := logp.NewLogger("add_cloud_metadata") + // LoadDefaultConfig loads the EC2 role credentials awsConfig, err := awscfg.LoadDefaultConfig(context.TODO(), awscfg.WithHTTPClient(&client)) if err != nil { @@ -91,10 +94,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, _ := fetchEC2ClusterNameTag(awsConfig, instanceIdentity.InstanceIdentityDocument.InstanceID) + 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) @@ -103,14 +115,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) { From 9a0013580e05726d4a0c2d1d9fd7da881c9c2db6 Mon Sep 17 00:00:00 2001 From: constanca-m Date: Wed, 1 Nov 2023 09:33:19 +0100 Subject: [PATCH 4/4] Add log message --- libbeat/processors/add_cloud_metadata/provider_aws_ec2.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libbeat/processors/add_cloud_metadata/provider_aws_ec2.go b/libbeat/processors/add_cloud_metadata/provider_aws_ec2.go index b6c5f3ad7bcf..1f428372b869 100644 --- a/libbeat/processors/add_cloud_metadata/provider_aws_ec2.go +++ b/libbeat/processors/add_cloud_metadata/provider_aws_ec2.go @@ -20,9 +20,10 @@ package add_cloud_metadata import ( "context" "fmt" - "github.com/elastic/elastic-agent-libs/logp" "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"