Skip to content

Commit 7567204

Browse files
authored
Merge pull request open-telemetry#58 from sethAmazon/fix-imds-retry
Fix imds retry
2 parents 082d76a + 8ed5eea commit 7567204

File tree

34 files changed

+253
-47
lines changed

34 files changed

+253
-47
lines changed

.github/workflows/build-and-test.yml

+1
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ jobs:
213213
git diff --exit-code '.github/ISSUE_TEMPLATE' || (echo 'Dropdowns in issue templates are out of date, please run "make generate-gh-issue-templates" and commit the changes in this PR.' && exit 1)
214214
unittest-matrix:
215215
strategy:
216+
fail-fast: false
216217
matrix:
217218
go-version: ["1.20", 1.19] # 1.20 is interpreted as 1.2 without quotes
218219
group:

cmd/configschema/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ require (
277277
github.com/alecthomas/participle/v2 v2.0.0 // indirect
278278
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect
279279
github.com/aliyun/aliyun-log-go-sdk v0.1.44 // indirect
280-
github.com/amazon-contributing/opentelemetry-collector-contrib/override/aws v0.0.0-20230712194013-e85dd5f17ce4 // indirect
280+
github.com/amazon-contributing/opentelemetry-collector-contrib/override/aws v0.0.0-20230719154634-7dfcb28377c2 // indirect
281281
github.com/andybalholm/brotli v1.0.5 // indirect
282282
github.com/antonmedv/expr v1.12.5 // indirect
283283
github.com/apache/arrow/go/arrow v0.0.0-20211112161151-bc219186db40 // indirect

cmd/otelcontribcol/exporters_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"runtime"
2626
"testing"
2727

28+
override "github.com/amazon-contributing/opentelemetry-collector-contrib/override/aws"
2829
"github.com/stretchr/testify/assert"
2930
"github.com/stretchr/testify/require"
3031
"go.opentelemetry.io/collector/component"
@@ -496,6 +497,7 @@ func TestDefaultExporters(t *testing.T) {
496497
expectedExporters[k] = struct{}{}
497498
}
498499
for _, tt := range tests {
500+
override.IMDSRetryer = nil
499501
_, ok := expFactories[tt.exporter]
500502
if !ok {
501503
// not part of the distro, skipping.

cmd/otelcontribcol/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module github.com/open-telemetry/opentelemetry-collector-contrib/cmd/otelcontrib
55
go 1.19
66

77
require (
8+
github.com/amazon-contributing/opentelemetry-collector-contrib/override/aws v0.0.0-20230719154634-7dfcb28377c2
89
github.com/open-telemetry/opentelemetry-collector-contrib/connector/countconnector v0.77.0
910
github.com/open-telemetry/opentelemetry-collector-contrib/connector/servicegraphconnector v0.77.0
1011
github.com/open-telemetry/opentelemetry-collector-contrib/connector/spanmetricsconnector v0.77.0
@@ -269,7 +270,6 @@ require (
269270
github.com/alecthomas/participle/v2 v2.0.0 // indirect
270271
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect
271272
github.com/aliyun/aliyun-log-go-sdk v0.1.44 // indirect
272-
github.com/amazon-contributing/opentelemetry-collector-contrib/override/aws v0.0.0-20230712194013-e85dd5f17ce4 // indirect
273273
github.com/andybalholm/brotli v1.0.5 // indirect
274274
github.com/antonmedv/expr v1.12.5 // indirect
275275
github.com/apache/arrow/go/arrow v0.0.0-20211112161151-bc219186db40 // indirect

exporter/awscloudwatchlogsexporter/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ require (
1919
)
2020

2121
require (
22-
github.com/amazon-contributing/opentelemetry-collector-contrib/override/aws v0.0.0-20230712194013-e85dd5f17ce4 // indirect
22+
github.com/amazon-contributing/opentelemetry-collector-contrib/override/aws v0.0.0-20230719154634-7dfcb28377c2 // indirect
2323
github.com/davecgh/go-spew v1.1.1 // indirect
2424
github.com/gogo/protobuf v1.3.2 // indirect
2525
github.com/golang/protobuf v1.5.3 // indirect

exporter/awsemfexporter/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ require (
2424
)
2525

2626
require (
27-
github.com/amazon-contributing/opentelemetry-collector-contrib/override/aws v0.0.0-20230712194013-e85dd5f17ce4 // indirect
27+
github.com/amazon-contributing/opentelemetry-collector-contrib/override/aws v0.0.0-20230719154634-7dfcb28377c2 // indirect
2828
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
2929
github.com/davecgh/go-spew v1.1.1 // indirect
3030
github.com/gogo/protobuf v1.3.2 // indirect

exporter/awsxrayexporter/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ require (
1818
)
1919

2020
require (
21-
github.com/amazon-contributing/opentelemetry-collector-contrib/override/aws v0.0.0-20230712194013-e85dd5f17ce4 // indirect
21+
github.com/amazon-contributing/opentelemetry-collector-contrib/override/aws v0.0.0-20230719154634-7dfcb28377c2 // indirect
2222
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
2323
github.com/davecgh/go-spew v1.1.1 // indirect
2424
github.com/gogo/protobuf v1.3.2 // indirect

exporter/datadogexporter/config_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ package datadogexporter
1717
import (
1818
"testing"
1919

20+
override "github.com/amazon-contributing/opentelemetry-collector-contrib/override/aws"
2021
"github.com/stretchr/testify/assert"
2122
"go.opentelemetry.io/collector/confmap"
2223
)
2324

2425
func TestValidate(t *testing.T) {
25-
26+
override.IMDSRetryer = nil
2627
tests := []struct {
2728
name string
2829
cfg *Config

exporter/datadogexporter/go.mod

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ module github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datado
22

33
go 1.19
44

5+
replace github.com/amazon-contributing/opentelemetry-collector-contrib/override/aws => ../../override/aws
6+
57
require (
68
github.com/DataDog/agent-payload/v5 v5.0.83
79
github.com/DataDog/datadog-agent/pkg/trace v0.45.0-rc.4
@@ -12,7 +14,8 @@ require (
1214
github.com/DataDog/opentelemetry-mapping-go/pkg/quantile v0.2.0
1315
github.com/DataDog/sketches-go v1.4.2
1416
github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp v1.13.1
15-
github.com/aws/aws-sdk-go v1.44.255
17+
github.com/amazon-contributing/opentelemetry-collector-contrib/override/aws v0.0.0-20230719154634-7dfcb28377c2
18+
github.com/aws/aws-sdk-go v1.44.293
1619
github.com/cenkalti/backoff/v4 v4.2.1
1720
github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/ecsutil v0.77.0
1821
github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal v0.77.0

exporter/datadogexporter/go.sum

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

exporter/datadogexporter/internal/hostmetadata/internal/ec2/ec2.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ import (
2222
"sync"
2323

2424
"github.com/DataDog/opentelemetry-mapping-go/pkg/otlp/attributes/source"
25+
override "github.com/amazon-contributing/opentelemetry-collector-contrib/override/aws"
2526
"github.com/aws/aws-sdk-go/aws"
26-
"github.com/aws/aws-sdk-go/aws/client"
2727
"github.com/aws/aws-sdk-go/aws/ec2metadata"
2828
"github.com/aws/aws-sdk-go/aws/session"
2929
"github.com/aws/aws-sdk-go/service/ec2"
@@ -64,17 +64,20 @@ func GetHostInfo(logger *zap.Logger) (hostInfo *HostInfo) {
6464
return
6565
}
6666

67+
ctx, cancel := context.WithTimeout(context.Background(), 2*override.TimePerCall)
68+
defer cancel()
69+
6770
meta := ec2metadata.New(sess, &aws.Config{
68-
Retryer: client.DefaultRetryer{NumMaxRetries: 5},
71+
Retryer: override.IMDSRetryer,
6972
})
7073

71-
if idDoc, err := meta.GetInstanceIdentityDocument(); err == nil {
74+
if idDoc, err := meta.GetInstanceIdentityDocumentWithContext(ctx); err == nil {
7275
hostInfo.InstanceID = idDoc.InstanceID
7376
} else {
7477
logger.Warn("Failed to get EC2 instance id document", zap.Error(err))
7578
}
7679

77-
if ec2Hostname, err := meta.GetMetadata("hostname"); err == nil {
80+
if ec2Hostname, err := meta.GetMetadataWithContext(ctx, "hostname"); err == nil {
7881
hostInfo.EC2Hostname = ec2Hostname
7982
} else {
8083
logger.Warn("Failed to get EC2 hostname", zap.Error(err))

exporter/datadogexporter/internal/hostmetadata/metadata_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
"github.com/DataDog/opentelemetry-mapping-go/pkg/otlp/attributes"
2727
"github.com/DataDog/opentelemetry-mapping-go/pkg/otlp/attributes/azure"
28+
override "github.com/amazon-contributing/opentelemetry-collector-contrib/override/aws"
2829
"github.com/stretchr/testify/assert"
2930
"github.com/stretchr/testify/require"
3031
"go.opentelemetry.io/collector/component"
@@ -64,6 +65,7 @@ var (
6465
)
6566

6667
func TestFillHostMetadata(t *testing.T) {
68+
override.IMDSRetryer = nil
6769
params := exportertest.NewNopCreateSettings()
6870
params.BuildInfo = mockBuildInfo
6971

extension/awsproxy/go.mod

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ require (
1313
)
1414

1515
require (
16-
github.com/aws/aws-sdk-go v1.44.255 // indirect
16+
github.com/amazon-contributing/opentelemetry-collector-contrib/override/aws v0.0.0-20230719154634-7dfcb28377c2 // indirect
17+
github.com/aws/aws-sdk-go v1.44.293 // indirect
1718
github.com/davecgh/go-spew v1.1.1 // indirect
1819
github.com/fsnotify/fsnotify v1.6.0 // indirect
1920
github.com/gogo/protobuf v1.3.2 // indirect

extension/awsproxy/go.sum

+4-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ require (
257257
github.com/alecthomas/participle/v2 v2.0.0 // indirect
258258
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect
259259
github.com/aliyun/aliyun-log-go-sdk v0.1.44 // indirect
260-
github.com/amazon-contributing/opentelemetry-collector-contrib/override/aws v0.0.0-20230712194013-e85dd5f17ce4 // indirect
260+
github.com/amazon-contributing/opentelemetry-collector-contrib/override/aws v0.0.0-20230719154634-7dfcb28377c2 // indirect
261261
github.com/andybalholm/brotli v1.0.5 // indirect
262262
github.com/antonmedv/expr v1.12.5 // indirect
263263
github.com/apache/arrow/go/arrow v0.0.0-20211112161151-bc219186db40 // indirect

internal/aws/awsutil/conn.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package awsutil // import "github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/awsutil"
1717

1818
import (
19+
"context"
1920
"crypto/tls"
2021
"errors"
2122
"net/http"
@@ -26,7 +27,6 @@ import (
2627
override "github.com/amazon-contributing/opentelemetry-collector-contrib/override/aws"
2728
"github.com/aws/aws-sdk-go/aws"
2829
"github.com/aws/aws-sdk-go/aws/awserr"
29-
"github.com/aws/aws-sdk-go/aws/client"
3030
"github.com/aws/aws-sdk-go/aws/credentials"
3131
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
3232
"github.com/aws/aws-sdk-go/aws/defaults"
@@ -47,9 +47,11 @@ type ConnAttr interface {
4747
type Conn struct{}
4848

4949
func (c *Conn) getEC2Region(s *session.Session) (string, error) {
50+
ctx, cancel := context.WithTimeout(context.Background(), override.TimePerCall)
51+
defer cancel()
5052
return ec2metadata.New(s, &aws.Config{
51-
Retryer: client.DefaultRetryer{NumMaxRetries: 5},
52-
}).Region()
53+
Retryer: override.IMDSRetryer,
54+
}).RegionWithContext(ctx)
5355
}
5456

5557
// AWS STS endpoint constants

internal/aws/awsutil/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/aw
33
go 1.19
44

55
require (
6-
github.com/amazon-contributing/opentelemetry-collector-contrib/override/aws v0.0.0-20230712194013-e85dd5f17ce4
6+
github.com/amazon-contributing/opentelemetry-collector-contrib/override/aws v0.0.0-20230719154634-7dfcb28377c2
77
github.com/aws/aws-sdk-go v1.44.293
88
github.com/stretchr/testify v1.8.2
99
go.uber.org/zap v1.24.0

internal/aws/proxy/conn.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package proxy // import "github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/proxy"
1616

1717
import (
18+
"context"
1819
"crypto/tls"
1920
"encoding/json"
2021
"errors"
@@ -25,10 +26,10 @@ import (
2526
"strings"
2627
"time"
2728

29+
override "github.com/amazon-contributing/opentelemetry-collector-contrib/override/aws"
2830
"github.com/aws/aws-sdk-go/aws"
2931
"github.com/aws/aws-sdk-go/aws/arn"
3032
"github.com/aws/aws-sdk-go/aws/awserr"
31-
"github.com/aws/aws-sdk-go/aws/client"
3233
"github.com/aws/aws-sdk-go/aws/credentials"
3334
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
3435
"github.com/aws/aws-sdk-go/aws/ec2metadata"
@@ -80,9 +81,11 @@ var newAWSSession = func(roleArn string, region string, log *zap.Logger) (*sessi
8081
}
8182

8283
var getEC2Region = func(s *session.Session) (string, error) {
84+
ctx, cancel := context.WithTimeout(context.Background(), override.TimePerCall)
85+
defer cancel()
8386
return ec2metadata.New(s, &aws.Config{
84-
Retryer: client.DefaultRetryer{NumMaxRetries: 5},
85-
}).Region()
87+
Retryer: override.IMDSRetryer,
88+
}).RegionWithContext(ctx)
8689
}
8790

8891
func getAWSConfigSession(c *Config, logger *zap.Logger) (*aws.Config, *session.Session, error) {

internal/aws/proxy/go.mod

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ module github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/pr
22

33
go 1.19
44

5+
replace github.com/amazon-contributing/opentelemetry-collector-contrib/override/aws => ../../../override/aws
6+
57
require (
6-
github.com/aws/aws-sdk-go v1.44.255
8+
github.com/amazon-contributing/opentelemetry-collector-contrib/override/aws v0.0.0-20230719154634-7dfcb28377c2
9+
github.com/aws/aws-sdk-go v1.44.293
710
github.com/open-telemetry/opentelemetry-collector-contrib/internal/common v0.77.0
811
github.com/stretchr/testify v1.8.2
912
go.opentelemetry.io/collector v0.77.0

internal/aws/proxy/go.sum

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/aws/xray/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/xr
33
go 1.19
44

55
require (
6+
github.com/amazon-contributing/opentelemetry-collector-contrib/override/aws v0.0.0-20230719154634-7dfcb28377c2
67
github.com/aws/aws-sdk-go v1.44.293
78
github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/awsutil v0.77.0
89
github.com/stretchr/testify v1.8.2
@@ -11,7 +12,6 @@ require (
1112
)
1213

1314
require (
14-
github.com/amazon-contributing/opentelemetry-collector-contrib/override/aws v0.0.0-20230712194013-e85dd5f17ce4 // indirect
1515
github.com/davecgh/go-spew v1.1.1 // indirect
1616
github.com/gogo/protobuf v1.3.2 // indirect
1717
github.com/golang/protobuf v1.5.3 // indirect

internal/aws/xray/telemetry/sender.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515
package telemetry // import "github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/xray/telemetry"
1616

1717
import (
18+
"context"
1819
"os"
1920
"sync"
2021
"time"
2122

23+
override "github.com/amazon-contributing/opentelemetry-collector-contrib/override/aws"
2224
"github.com/aws/aws-sdk-go/aws"
23-
"github.com/aws/aws-sdk-go/aws/client"
2425
"github.com/aws/aws-sdk-go/aws/ec2metadata"
2526
"github.com/aws/aws-sdk-go/aws/session"
2627
"github.com/aws/aws-sdk-go/service/xray"
@@ -169,7 +170,9 @@ type ec2MetadataProvider struct {
169170

170171
func (p ec2MetadataProvider) get() string {
171172
var metadata string
172-
if result, err := p.client.GetMetadata(p.metadataKey); err == nil {
173+
ctx, cancel := context.WithTimeout(context.Background(), override.TimePerCall)
174+
defer cancel()
175+
if result, err := p.client.GetMetadataWithContext(ctx, p.metadataKey); err == nil {
173176
metadata = result
174177
}
175178
return metadata
@@ -190,7 +193,7 @@ func ToOptions(cfg Config, sess *session.Session, settings *awsutil.AWSSessionSe
190193
}
191194
if !settings.LocalMode {
192195
metadataClient := ec2metadata.New(sess, &aws.Config{
193-
Retryer: client.DefaultRetryer{NumMaxRetries: 5},
196+
Retryer: override.IMDSRetryer,
194197
})
195198
hostnameProviders = append(hostnameProviders, ec2MetadataProvider{
196199
client: metadataClient,

internal/metadataproviders/aws/ec2/metadata.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ package ec2 // import "github.com/open-telemetry/opentelemetry-collector-contrib
1717
import (
1818
"context"
1919

20+
override "github.com/amazon-contributing/opentelemetry-collector-contrib/override/aws"
2021
"github.com/aws/aws-sdk-go/aws"
21-
"github.com/aws/aws-sdk-go/aws/client"
2222
"github.com/aws/aws-sdk-go/aws/ec2metadata"
2323
"github.com/aws/aws-sdk-go/aws/session"
2424
)
@@ -38,19 +38,25 @@ var _ Provider = (*metadataClient)(nil)
3838
func NewProvider(sess *session.Session) Provider {
3939
return &metadataClient{
4040
metadata: ec2metadata.New(sess, &aws.Config{
41-
Retryer: client.DefaultRetryer{NumMaxRetries: 5},
41+
Retryer: override.IMDSRetryer,
4242
}),
4343
}
4444
}
4545

4646
func (c *metadataClient) InstanceID(ctx context.Context) (string, error) {
47-
return c.metadata.GetMetadataWithContext(ctx, "instance-id")
47+
childCtx, cancel := context.WithTimeout(ctx, override.TimePerCall)
48+
defer cancel()
49+
return c.metadata.GetMetadataWithContext(childCtx, "instance-id")
4850
}
4951

5052
func (c *metadataClient) Hostname(ctx context.Context) (string, error) {
51-
return c.metadata.GetMetadataWithContext(ctx, "hostname")
53+
childCtx, cancel := context.WithTimeout(ctx, override.TimePerCall)
54+
defer cancel()
55+
return c.metadata.GetMetadataWithContext(childCtx, "hostname")
5256
}
5357

5458
func (c *metadataClient) Get(ctx context.Context) (ec2metadata.EC2InstanceIdentityDocument, error) {
55-
return c.metadata.GetInstanceIdentityDocumentWithContext(ctx)
59+
childCtx, cancel := context.WithTimeout(ctx, override.TimePerCall)
60+
defer cancel()
61+
return c.metadata.GetInstanceIdentityDocumentWithContext(childCtx)
5662
}

0 commit comments

Comments
 (0)