Skip to content

Commit

Permalink
[add_cloud_metadata] Remove logger for AWS/EC2 (#36829)
Browse files Browse the repository at this point in the history
* Remove logger.

* Add error message.

* Add log message

(cherry picked from commit d66a000)

# Conflicts:
#	libbeat/processors/add_cloud_metadata/provider_aws_ec2.go
  • Loading branch information
constanca-m authored and mergify[bot] committed Nov 20, 2023
1 parent 97ceb28 commit 86c5e2c
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 2 deletions.
94 changes: 94 additions & 0 deletions libbeat/processors/add_cloud_metadata/provider_aws_ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,25 @@ import (
"net"
"net/http"

<<<<<<< HEAD

Check failure on line 27 in libbeat/processors/add_cloud_metadata/provider_aws_ec2.go

View workflow job for this annotation

GitHub Actions / lint (windows)

missing import path (typecheck)

Check failure on line 27 in libbeat/processors/add_cloud_metadata/provider_aws_ec2.go

View workflow job for this annotation

GitHub Actions / lint (linux)

missing import path (typecheck)

Check failure on line 27 in libbeat/processors/add_cloud_metadata/provider_aws_ec2.go

View workflow job for this annotation

GitHub Actions / lint (darwin)

missing import path (typecheck)
"github.com/elastic/beats/v7/libbeat/common"
s "github.com/elastic/beats/v7/libbeat/common/schema"
c "github.com/elastic/beats/v7/libbeat/common/schema/mapstriface"
"github.com/elastic/beats/v7/libbeat/common/transport/tlscommon"
"github.com/elastic/beats/v7/libbeat/logp"
=======

Check failure on line 33 in libbeat/processors/add_cloud_metadata/provider_aws_ec2.go

View workflow job for this annotation

GitHub Actions / lint (windows)

missing import path (typecheck)

Check failure on line 33 in libbeat/processors/add_cloud_metadata/provider_aws_ec2.go

View workflow job for this annotation

GitHub Actions / lint (linux)

missing import path (typecheck)

Check failure on line 33 in libbeat/processors/add_cloud_metadata/provider_aws_ec2.go

View workflow job for this annotation

GitHub Actions / lint (darwin)

missing import path (typecheck)
"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/mapstr"

conf "github.com/elastic/elastic-agent-libs/config"
>>>>>>> d66a0001ac ([add_cloud_metadata] Remove logger for AWS/EC2 (#36829))

Check failure on line 45 in libbeat/processors/add_cloud_metadata/provider_aws_ec2.go

View workflow job for this annotation

GitHub Actions / lint (windows)

missing import path (typecheck)

Check failure on line 45 in libbeat/processors/add_cloud_metadata/provider_aws_ec2.go

View workflow job for this annotation

GitHub Actions / lint (linux)

missing import path (typecheck)

Check failure on line 45 in libbeat/processors/add_cloud_metadata/provider_aws_ec2.go

View workflow job for this annotation

GitHub Actions / lint (darwin)

missing import path (typecheck)
)

const (
Expand Down Expand Up @@ -138,3 +152,83 @@ var ec2MetadataFetcher = provider{
return fetcher, err
},
}
<<<<<<< HEAD

Check failure on line 155 in libbeat/processors/add_cloud_metadata/provider_aws_ec2.go

View workflow job for this annotation

GitHub Actions / lint (windows)

expected declaration, found '<<' (typecheck)

Check failure on line 155 in libbeat/processors/add_cloud_metadata/provider_aws_ec2.go

View workflow job for this annotation

GitHub Actions / lint (linux)

expected declaration, found '<<' (typecheck)

Check failure on line 155 in libbeat/processors/add_cloud_metadata/provider_aws_ec2.go

View workflow job for this annotation

GitHub Actions / lint (darwin)

expected declaration, found '<<' (typecheck)
=======

// fetchRaw queries raw metadata from a hosting provider's metadata service.
func fetchRawProviderMetadata(
ctx context.Context,
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 {
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 {
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)
} 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)

_, _ = 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)
_, _ = result.metadata.Put("region", awsRegion)
_, _ = result.metadata.Put("availability_zone", instanceIdentity.InstanceIdentityDocument.AvailabilityZone)
_, _ = result.metadata.Put("account.id", accountID)
_, _ = result.metadata.Put("image.id", instanceIdentity.InstanceIdentityDocument.ImageID)

}

func fetchEC2ClusterNameTag(awsConfig awssdk.Config, instanceID string) (string, error) {
svc := NewEC2Client(awsConfig)
input := &ec2.DescribeTagsInput{
Filters: []types.Filter{
{
Name: awssdk.String("resource-id"),
Values: []string{
instanceID,
},
},
{
Name: awssdk.String("key"),
Values: []string{
"eks:cluster-name",
},
},
},
}

tagsResult, err := svc.DescribeTags(context.TODO(), input)
if err != nil {
return "", fmt.Errorf("error fetching EC2 Tags: %w", err)
}
if len(tagsResult.Tags) == 1 {
return *tagsResult.Tags[0].Value, nil
}
return "", nil
}
>>>>>>> d66a0001ac ([add_cloud_metadata] Remove logger for AWS/EC2 (#36829))

Check failure on line 234 in libbeat/processors/add_cloud_metadata/provider_aws_ec2.go

View workflow job for this annotation

GitHub Actions / lint (windows)

illegal character U+0023 '#' (typecheck)

Check failure on line 234 in libbeat/processors/add_cloud_metadata/provider_aws_ec2.go

View workflow job for this annotation

GitHub Actions / lint (linux)

illegal character U+0023 '#' (typecheck)

Check failure on line 234 in libbeat/processors/add_cloud_metadata/provider_aws_ec2.go

View workflow job for this annotation

GitHub Actions / lint (darwin)

illegal character U+0023 '#' (typecheck)
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 @@ -100,7 +100,7 @@ func setupFetchers(providers map[string]provider, c *common.Config) ([]metadataF
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 @@ -122,7 +122,7 @@ func setupFetchers(providers map[string]provider, c *common.Config) ([]metadataF
}

// 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 @@ -168,6 +168,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

0 comments on commit 86c5e2c

Please sign in to comment.