Skip to content

Commit

Permalink
[processor/resourcedetection] Add fail_on_missing_metadata to EC2 det…
Browse files Browse the repository at this point in the history
…ector (#37453)

<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->
#### Description
Add `fail_on_missing_metadata` option on EC2 detector

If the EC2 metadata endpoint is unavailable, the EC2 detector by default
ignores the error.
By setting `fail_on_missing_metadata` to true on the detector, the user
will now trigger an error explicitly,
which will stop the collector from starting.

<!-- Issue number (e.g. #1234) or full URL to issue, if applicable. -->
#### Link to tracking issue
Relates to #35936
  • Loading branch information
atoulme authored Feb 3, 2025
1 parent 2a5371b commit b71d233
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 26 deletions.
30 changes: 30 additions & 0 deletions .chloggen/ec2_fail_on_missing_metadata.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: resourcedetectionprocessor

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add `fail_on_missing_metadata` option on EC2 detector

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [35936]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext: |
If the EC2 metadata endpoint is unavailable, the EC2 detector by default ignores the error.
By setting `fail_on_missing_metadata` to true on the detector, the user will now trigger an error explicitly,
which will stop the collector from starting.
# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
11 changes: 11 additions & 0 deletions processor/resourcedetectionprocessor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ In some cases, you might need to change the behavior of the AWS metadata client
By default, the client retries 3 times with a max backoff delay of 20s.

We offer a limited set of options to override those defaults specifically, such that you can set the client to retry 10 times, for up to 5 minutes, for example:

```yaml
processors:
resourcedetection/ec2:
Expand All @@ -262,6 +263,16 @@ processors:
max_backoff: 5m
```

The EC2 detector will report an error in logs if the EC2 metadata endpoint is unavailable. You can configure the detector to instead fail with this flag:

```yaml
processors:
resourcedetection/ec2:
detectors: ["ec2"]
ec2:
fail_on_missing_metadata: true
```

### Amazon ECS

Queries the [Task Metadata Endpoint](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-metadata-endpoint.html) (TMDE) to record information about the current ECS Task. Only TMDE V4 and V3 are supported.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ import (
type Config struct {
// Tags is a list of regex's to match ec2 instance tag keys that users want
// to add as resource attributes to processed data
Tags []string `mapstructure:"tags"`
ResourceAttributes metadata.ResourceAttributesConfig `mapstructure:"resource_attributes"`
MaxAttempts int `mapstructure:"max_attempts"`
MaxBackoff time.Duration `mapstructure:"max_backoff"`
Tags []string `mapstructure:"tags"`
ResourceAttributes metadata.ResourceAttributesConfig `mapstructure:"resource_attributes"`
MaxAttempts int `mapstructure:"max_attempts"`
MaxBackoff time.Duration `mapstructure:"max_backoff"`
FailOnMissingMetadata bool `mapstructure:"fail_on_missing_metadata"`
}

func CreateDefaultConfig() Config {
Expand Down
25 changes: 15 additions & 10 deletions processor/resourcedetectionprocessor/internal/aws/ec2/ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,12 @@ func (e *ec2ClientBuilder) buildClient(ctx context.Context, region string, clien
}

type Detector struct {
metadataProvider ec2provider.Provider
tagKeyRegexes []*regexp.Regexp
logger *zap.Logger
rb *metadata.ResourceBuilder
ec2ClientBuilder ec2ifaceBuilder
metadataProvider ec2provider.Provider
tagKeyRegexes []*regexp.Regexp
logger *zap.Logger
rb *metadata.ResourceBuilder
ec2ClientBuilder ec2ifaceBuilder
failOnMissingMetadata bool
}

func NewDetector(set processor.Settings, dcfg internal.DetectorConfig) (internal.Detector, error) {
Expand All @@ -76,17 +77,21 @@ func NewDetector(set processor.Settings, dcfg internal.DetectorConfig) (internal
}

return &Detector{
metadataProvider: ec2provider.NewProvider(awsConfig),
tagKeyRegexes: tagKeyRegexes,
logger: set.Logger,
rb: metadata.NewResourceBuilder(cfg.ResourceAttributes),
ec2ClientBuilder: &ec2ClientBuilder{},
metadataProvider: ec2provider.NewProvider(awsConfig),
tagKeyRegexes: tagKeyRegexes,
logger: set.Logger,
rb: metadata.NewResourceBuilder(cfg.ResourceAttributes),
ec2ClientBuilder: &ec2ClientBuilder{},
failOnMissingMetadata: cfg.FailOnMissingMetadata,
}, nil
}

func (d *Detector) Detect(ctx context.Context) (resource pcommon.Resource, schemaURL string, err error) {
if _, err = d.metadataProvider.InstanceID(ctx); err != nil {
d.logger.Debug("EC2 metadata unavailable", zap.Error(err))
if d.failOnMissingMetadata {
return pcommon.NewResource(), "", err
}
return pcommon.NewResource(), "", nil
}

Expand Down
38 changes: 26 additions & 12 deletions processor/resourcedetectionprocessor/internal/aws/ec2/ec2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,14 @@ func TestDetector_Detect(t *testing.T) {
ctx context.Context
}
tests := []struct {
name string
fields fields
tagKeyRegexes []*regexp.Regexp
args args
want pcommon.Resource
wantErr bool
tagsProvider ec2ifaceBuilder
name string
fields fields
tagKeyRegexes []*regexp.Regexp
args args
want pcommon.Resource
wantErr bool
tagsProvider ec2ifaceBuilder
failOnMissingMetadata bool
}{
{
name: "success",
Expand Down Expand Up @@ -267,6 +268,18 @@ func TestDetector_Detect(t *testing.T) {
want: pcommon.NewResource(),
wantErr: false,
},
{
name: "endpoint not available, with fail_on_missing_metadata",
fields: fields{metadataProvider: &mockMetadata{
retIDDoc: imds.InstanceIdentityDocument{},
retErrIDDoc: errors.New("should not be called"),
isAvailable: false,
}},
args: args{ctx: context.Background()},
want: pcommon.NewResource(),
wantErr: true,
failOnMissingMetadata: true,
},
{
name: "get fails",
fields: fields{metadataProvider: &mockMetadata{
Expand Down Expand Up @@ -294,11 +307,12 @@ func TestDetector_Detect(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := &Detector{
metadataProvider: tt.fields.metadataProvider,
logger: zap.NewNop(),
rb: metadata.NewResourceBuilder(metadata.DefaultResourceAttributesConfig()),
tagKeyRegexes: tt.tagKeyRegexes,
ec2ClientBuilder: tt.tagsProvider,
metadataProvider: tt.fields.metadataProvider,
logger: zap.NewNop(),
rb: metadata.NewResourceBuilder(metadata.DefaultResourceAttributesConfig()),
tagKeyRegexes: tt.tagKeyRegexes,
ec2ClientBuilder: tt.tagsProvider,
failOnMissingMetadata: tt.failOnMissingMetadata,
}
got, _, err := d.Detect(tt.args.ctx)

Expand Down

0 comments on commit b71d233

Please sign in to comment.