Skip to content

Commit e532370

Browse files
bhautikpipAneurysm9MrAlias
authored
Fix ECS resource detector bug (#569)
* added failure scenario when getting container fails * fix test case failure * add changelog * fix ecs resource detector bug * fix struct name as per golint suggestion * minor changes * added NewResourceDetector func and interface assertions * fix golint failure * minor changes to address review comments Co-authored-by: Anthony Mirabella <a9@aneurysm9.com> Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
1 parent b87d221 commit e532370

2 files changed

Lines changed: 26 additions & 21 deletions

File tree

detectors/aws/ecs/ecs.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,23 @@ type detectorUtils interface {
5252
type ecsDetectorUtils struct{}
5353

5454
// resource detector collects resource information from Elastic Container Service environment
55-
type ResourceDetector struct {
55+
type resourceDetector struct {
5656
utils detectorUtils
5757
}
5858

5959
// compile time assertion that ecsDetectorUtils implements detectorUtils interface
6060
var _ detectorUtils = (*ecsDetectorUtils)(nil)
6161

6262
// compile time assertion that resource detector implements the resource.Detector interface.
63-
var _ resource.Detector = (*ResourceDetector)(nil)
63+
var _ resource.Detector = (*resourceDetector)(nil)
64+
65+
// NewResourceDetector returns a resource detector that will detect AWS ECS resources.
66+
func NewResourceDetector() resource.Detector {
67+
return &resourceDetector{utils: ecsDetectorUtils{}}
68+
}
6469

6570
// Detect finds associated resources when running on ECS environment.
66-
func (detector *ResourceDetector) Detect(ctx context.Context) (*resource.Resource, error) {
71+
func (detector *resourceDetector) Detect(ctx context.Context) (*resource.Resource, error) {
6772
metadataURIV3 := os.Getenv(metadataV3EnvVar)
6873
metadataURIV4 := os.Getenv(metadataV4EnvVar)
6974

detectors/aws/ecs/ecs_test.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ func (detectorUtils *MockDetectorUtils) getContainerName() (string, error) {
4545
//succesfully return resource when process is running on Amazon ECS environment
4646
func TestDetect(t *testing.T) {
4747
os.Clearenv()
48-
os.Setenv(metadataV3EnvVar, "3")
49-
os.Setenv(metadataV4EnvVar, "4")
48+
_ = os.Setenv(metadataV3EnvVar, "3")
49+
_ = os.Setenv(metadataV4EnvVar, "4")
5050

5151
detectorUtils := new(MockDetectorUtils)
5252

@@ -58,52 +58,52 @@ func TestDetect(t *testing.T) {
5858
semconv.ContainerIDKey.String("0123456789A"),
5959
}
6060
expectedResource := resource.NewWithAttributes(labels...)
61-
detector := ResourceDetector{detectorUtils}
62-
resource, _ := detector.Detect(context.Background())
61+
detector := &resourceDetector{utils: detectorUtils}
62+
res, _ := detector.Detect(context.Background())
6363

64-
assert.Equal(t, resource, expectedResource, "Resource returned is incorrect")
64+
assert.Equal(t, res, expectedResource, "Resource returned is incorrect")
6565
}
6666

6767
//returns empty resource when detector cannot read container ID
6868
func TestDetectCannotReadContainerID(t *testing.T) {
6969
os.Clearenv()
70-
os.Setenv(metadataV3EnvVar, "3")
71-
os.Setenv(metadataV4EnvVar, "4")
70+
_ = os.Setenv(metadataV3EnvVar, "3")
71+
_ = os.Setenv(metadataV4EnvVar, "4")
7272
detectorUtils := new(MockDetectorUtils)
7373

7474
detectorUtils.On("getContainerName").Return("container-Name", nil)
7575
detectorUtils.On("getContainerID").Return("", errCannotReadContainerID)
7676

77-
detector := ResourceDetector{detectorUtils}
78-
resource, err := detector.Detect(context.Background())
77+
detector := &resourceDetector{utils: detectorUtils}
78+
res, err := detector.Detect(context.Background())
7979

8080
assert.Equal(t, errCannotReadContainerID, err)
81-
assert.Equal(t, 0, len(resource.Attributes()))
81+
assert.Equal(t, 0, len(res.Attributes()))
8282
}
8383

8484
//returns empty resource when detector cannot read container Name
8585
func TestDetectCannotReadContainerName(t *testing.T) {
8686
os.Clearenv()
87-
os.Setenv(metadataV3EnvVar, "3")
88-
os.Setenv(metadataV4EnvVar, "4")
87+
_ = os.Setenv(metadataV3EnvVar, "3")
88+
_ = os.Setenv(metadataV4EnvVar, "4")
8989
detectorUtils := new(MockDetectorUtils)
9090

9191
detectorUtils.On("getContainerName").Return("", errCannotReadContainerName)
9292
detectorUtils.On("getContainerID").Return("0123456789A", nil)
9393

94-
detector := ResourceDetector{detectorUtils}
95-
resource, err := detector.Detect(context.Background())
94+
detector := &resourceDetector{utils: detectorUtils}
95+
res, err := detector.Detect(context.Background())
9696

9797
assert.Equal(t, errCannotReadContainerName, err)
98-
assert.Equal(t, 0, len(resource.Attributes()))
98+
assert.Equal(t, 0, len(res.Attributes()))
9999
}
100100

101101
//returns empty resource when process is not running ECS
102102
func TestReturnsIfNoEnvVars(t *testing.T) {
103103
os.Clearenv()
104-
detector := ResourceDetector{}
105-
resource, err := detector.Detect(context.Background())
104+
detector := &resourceDetector{utils: nil}
105+
res, err := detector.Detect(context.Background())
106106

107107
assert.Equal(t, errNotOnECS, err)
108-
assert.Equal(t, 0, len(resource.Attributes()))
108+
assert.Equal(t, 0, len(res.Attributes()))
109109
}

0 commit comments

Comments
 (0)