Skip to content

Commit 452dad4

Browse files
committed
[processor/resourcedetection] fail to start the processor if the ec2 detector fails to start because network is unreachable
1 parent 23117ab commit 452dad4

File tree

4 files changed

+53
-17
lines changed

4 files changed

+53
-17
lines changed

.chloggen/detect_network_error.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: bug_fix
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: resourcedetectionprocessor
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Return an error when trying to start the processor with the ec2 detector while the network is not reachable
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [35936]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: []

processor/resourcedetectionprocessor/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/aws/aws-sdk-go-v2/config v1.29.1
1111
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.24
1212
github.com/aws/aws-sdk-go-v2/service/ec2 v1.200.0
13+
github.com/aws/smithy-go v1.22.1
1314
github.com/google/go-cmp v0.6.0
1415
github.com/hashicorp/consul/api v1.31.0
1516
github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/ecsutil v0.118.0
@@ -55,7 +56,6 @@ require (
5556
github.com/aws/aws-sdk-go-v2/service/sso v1.24.11 // indirect
5657
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.28.10 // indirect
5758
github.com/aws/aws-sdk-go-v2/service/sts v1.33.9 // indirect
58-
github.com/aws/smithy-go v1.22.1 // indirect
5959
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
6060
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
6161
github.com/distribution/reference v0.5.0 // indirect

processor/resourcedetectionprocessor/internal/aws/ec2/ec2.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package ec2 // import "github.com/open-telemetry/opentelemetry-collector-contrib
55

66
import (
77
"context"
8+
"errors"
89
"fmt"
910
"net/http"
1011
"regexp"
@@ -13,6 +14,7 @@ import (
1314
"github.com/aws/aws-sdk-go-v2/config"
1415
"github.com/aws/aws-sdk-go-v2/service/ec2"
1516
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
17+
smithyhttp "github.com/aws/smithy-go/transport/http"
1618
"go.opentelemetry.io/collector/pdata/pcommon"
1719
"go.opentelemetry.io/collector/processor"
1820
conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
@@ -79,6 +81,11 @@ func NewDetector(set processor.Settings, dcfg internal.DetectorConfig) (internal
7981

8082
func (d *Detector) Detect(ctx context.Context) (resource pcommon.Resource, schemaURL string, err error) {
8183
if _, err = d.metadataProvider.InstanceID(ctx); err != nil {
84+
var requestSendError *smithyhttp.RequestSendError
85+
if errors.As(err, &requestSendError) {
86+
d.logger.Warn("EC2 metadata endpoint unreachable", zap.Error(err))
87+
return pcommon.NewResource(), "", err
88+
}
8289
d.logger.Debug("EC2 metadata unavailable", zap.Error(err))
8390
return pcommon.NewResource(), "", nil
8491
}

processor/resourcedetectionprocessor/internal/aws/ec2/ec2_test.go

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/aws/aws-sdk-go-v2/feature/ec2/imds"
1515
"github.com/aws/aws-sdk-go-v2/service/ec2"
1616
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
17+
smithyhttp "github.com/aws/smithy-go/transport/http"
1718
"github.com/stretchr/testify/assert"
1819
"github.com/stretchr/testify/require"
1920
"go.opentelemetry.io/collector/pdata/pcommon"
@@ -30,10 +31,9 @@ type mockMetadata struct {
3031
retIDDoc imds.InstanceIdentityDocument
3132
retErrIDDoc error
3233

33-
retHostname string
34-
retErrHostname error
35-
36-
isAvailable bool
34+
retHostname string
35+
retErrHostname error
36+
retErrUnavailable error
3737
}
3838

3939
var _ ec2provider.Provider = (*mockMetadata)(nil)
@@ -51,10 +51,7 @@ func (e *mockClientBuilderError) buildClient(_ context.Context, _ string, _ *htt
5151
}
5252

5353
func (mm mockMetadata) InstanceID(_ context.Context) (string, error) {
54-
if !mm.isAvailable {
55-
return "", errUnavailable
56-
}
57-
return "", nil
54+
return "", mm.retErrUnavailable
5855
}
5956

6057
func (mm mockMetadata) Get(_ context.Context) (imds.InstanceIdentityDocument, error) {
@@ -173,7 +170,6 @@ func TestDetector_Detect(t *testing.T) {
173170
InstanceType: "c4.xlarge",
174171
},
175172
retHostname: "example-hostname",
176-
isAvailable: true,
177173
}},
178174
args: args{ctx: context.Background()},
179175
want: func() pcommon.Resource {
@@ -203,7 +199,6 @@ func TestDetector_Detect(t *testing.T) {
203199
InstanceType: "c4.xlarge",
204200
},
205201
retHostname: "example-hostname",
206-
isAvailable: true,
207202
}},
208203
tagKeyRegexes: []*regexp.Regexp{regexp.MustCompile("^tag1$"), regexp.MustCompile("^tag2$")},
209204
args: args{ctx: context.Background()},
@@ -237,7 +232,6 @@ func TestDetector_Detect(t *testing.T) {
237232
InstanceType: "c4.xlarge",
238233
},
239234
retHostname: "example-hostname",
240-
isAvailable: true,
241235
}},
242236
args: args{ctx: context.Background()},
243237
want: func() pcommon.Resource {
@@ -259,20 +253,29 @@ func TestDetector_Detect(t *testing.T) {
259253
{
260254
name: "endpoint not available",
261255
fields: fields{metadataProvider: &mockMetadata{
262-
retIDDoc: imds.InstanceIdentityDocument{},
263-
retErrIDDoc: errors.New("should not be called"),
264-
isAvailable: false,
256+
retIDDoc: imds.InstanceIdentityDocument{},
257+
retErrIDDoc: errors.New("should not be called"),
258+
retErrUnavailable: errUnavailable,
265259
}},
266260
args: args{ctx: context.Background()},
267261
want: pcommon.NewResource(),
268262
wantErr: false,
269263
},
264+
{
265+
name: "network not ready",
266+
fields: fields{metadataProvider: &mockMetadata{
267+
retIDDoc: imds.InstanceIdentityDocument{},
268+
retErrUnavailable: &smithyhttp.RequestSendError{Err: errors.New("nope")},
269+
}},
270+
args: args{ctx: context.Background()},
271+
want: pcommon.NewResource(),
272+
wantErr: true,
273+
},
270274
{
271275
name: "get fails",
272276
fields: fields{metadataProvider: &mockMetadata{
273277
retIDDoc: imds.InstanceIdentityDocument{},
274278
retErrIDDoc: errors.New("get failed"),
275-
isAvailable: true,
276279
}},
277280
args: args{ctx: context.Background()},
278281
want: pcommon.NewResource(),
@@ -284,7 +287,6 @@ func TestDetector_Detect(t *testing.T) {
284287
retIDDoc: imds.InstanceIdentityDocument{},
285288
retHostname: "",
286289
retErrHostname: errors.New("hostname failed"),
287-
isAvailable: true,
288290
}},
289291
args: args{ctx: context.Background()},
290292
want: pcommon.NewResource(),

0 commit comments

Comments
 (0)