Skip to content

Commit cc69f93

Browse files
added failure message - ECS resource detector (#568)
* added failure scenario when getting container fails * fix test case failure * add changelog Co-authored-by: Anthony Mirabella <[email protected]>
1 parent fd60b5f commit cc69f93

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

CHANGELOG.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
1010

1111
### Added
1212

13-
- Adding `ot-tracer` propagator (#562)
13+
- Add `ot-tracer` propagator (#562)
1414

1515
### Changed
1616

1717
- Rename project default branch from `master` to `main`.
1818

19+
### Fixed
20+
21+
- Added failure message for AWS ECS resource detector for better debugging (#568)
22+
1923
## [0.16.0] - 2021-01-13
2024

2125
### Fixed

detectors/aws/ecs/ecs.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@ const (
3535
)
3636

3737
var (
38-
empty = resource.Empty()
39-
errCannotReadContainerID = errors.New("failed to read container ID from cGroupFile")
40-
errCannotReadCGroupFile = errors.New("ECS resource detector failed to read cGroupFile")
41-
errNotOnECS = errors.New("process is not on ECS, cannot detect environment variables from ECS")
38+
empty = resource.Empty()
39+
errCannotReadContainerID = errors.New("failed to read container ID from cGroupFile")
40+
errCannotReadContainerName = errors.New("failed to read hostname")
41+
errCannotReadCGroupFile = errors.New("ECS resource detector failed to read cGroupFile")
42+
errNotOnECS = errors.New("process is not on ECS, cannot detect environment variables from ECS")
4243
)
4344

4445
// Create interface for methods needing to be mocked
@@ -102,5 +103,9 @@ func (ecsUtils ecsDetectorUtils) getContainerID() (string, error) {
102103

103104
// returns host name reported by the kernel
104105
func (ecsUtils ecsDetectorUtils) getContainerName() (string, error) {
105-
return os.Hostname()
106+
hostName, err := os.Hostname()
107+
if err != nil {
108+
return "", errCannotReadContainerName
109+
}
110+
return hostName, nil
106111
}

detectors/aws/ecs/ecs_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,23 @@ func TestDetectCannotReadContainerID(t *testing.T) {
8181
assert.Equal(t, 0, len(resource.Attributes()))
8282
}
8383

84+
//returns empty resource when detector cannot read container Name
85+
func TestDetectCannotReadContainerName(t *testing.T) {
86+
os.Clearenv()
87+
os.Setenv(metadataV3EnvVar, "3")
88+
os.Setenv(metadataV4EnvVar, "4")
89+
detectorUtils := new(MockDetectorUtils)
90+
91+
detectorUtils.On("getContainerName").Return("", errCannotReadContainerName)
92+
detectorUtils.On("getContainerID").Return("0123456789A", nil)
93+
94+
detector := ResourceDetector{detectorUtils}
95+
resource, err := detector.Detect(context.Background())
96+
97+
assert.Equal(t, errCannotReadContainerName, err)
98+
assert.Equal(t, 0, len(resource.Attributes()))
99+
}
100+
84101
//returns empty resource when process is not running ECS
85102
func TestReturnsIfNoEnvVars(t *testing.T) {
86103
os.Clearenv()

0 commit comments

Comments
 (0)