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

[processor/resourcedetection] Add fail_on_missing_metadata to EC2 detector #37453

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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: []
10 changes: 10 additions & 0 deletions processor/resourcedetectionprocessor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,16 @@ If you are using a proxy server on your EC2 instance, it's important that you ex

If the instance is part of AWS ParallelCluster and the detector is failing to connect to the metadata server, check the iptable and make sure the chain `PARALLELCLUSTER_IMDS` contains a rule that allows OTEL user to access `169.254.169.254/32`

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 @@ -11,8 +11,9 @@ 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"`
Tags []string `mapstructure:"tags"`
ResourceAttributes metadata.ResourceAttributesConfig `mapstructure:"resource_attributes"`
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 @@ -50,11 +50,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 @@ -69,17 +70,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
Loading