Skip to content

Commit

Permalink
added failure message - ECS resource detector (#568)
Browse files Browse the repository at this point in the history
* added failure scenario when getting container fails

* fix test case failure

* add changelog

Co-authored-by: Anthony Mirabella <[email protected]>
  • Loading branch information
bhautikpip and Aneurysm9 authored Feb 5, 2021
1 parent fd60b5f commit cc69f93
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

### Added

- Adding `ot-tracer` propagator (#562)
- Add `ot-tracer` propagator (#562)

### Changed

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

### Fixed

- Added failure message for AWS ECS resource detector for better debugging (#568)

## [0.16.0] - 2021-01-13

### Fixed
Expand Down
15 changes: 10 additions & 5 deletions detectors/aws/ecs/ecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ const (
)

var (
empty = resource.Empty()
errCannotReadContainerID = errors.New("failed to read container ID from cGroupFile")
errCannotReadCGroupFile = errors.New("ECS resource detector failed to read cGroupFile")
errNotOnECS = errors.New("process is not on ECS, cannot detect environment variables from ECS")
empty = resource.Empty()
errCannotReadContainerID = errors.New("failed to read container ID from cGroupFile")
errCannotReadContainerName = errors.New("failed to read hostname")
errCannotReadCGroupFile = errors.New("ECS resource detector failed to read cGroupFile")
errNotOnECS = errors.New("process is not on ECS, cannot detect environment variables from ECS")
)

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

// returns host name reported by the kernel
func (ecsUtils ecsDetectorUtils) getContainerName() (string, error) {
return os.Hostname()
hostName, err := os.Hostname()
if err != nil {
return "", errCannotReadContainerName
}
return hostName, nil
}
17 changes: 17 additions & 0 deletions detectors/aws/ecs/ecs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,23 @@ func TestDetectCannotReadContainerID(t *testing.T) {
assert.Equal(t, 0, len(resource.Attributes()))
}

//returns empty resource when detector cannot read container Name
func TestDetectCannotReadContainerName(t *testing.T) {
os.Clearenv()
os.Setenv(metadataV3EnvVar, "3")
os.Setenv(metadataV4EnvVar, "4")
detectorUtils := new(MockDetectorUtils)

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

detector := ResourceDetector{detectorUtils}
resource, err := detector.Detect(context.Background())

assert.Equal(t, errCannotReadContainerName, err)
assert.Equal(t, 0, len(resource.Attributes()))
}

//returns empty resource when process is not running ECS
func TestReturnsIfNoEnvVars(t *testing.T) {
os.Clearenv()
Expand Down

0 comments on commit cc69f93

Please sign in to comment.