Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- `go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace`
- `go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp`

### Fixed

- Improve the ECS detector correctness in `go.opentelemetry.io/contrib/detectors/aws/ecs`. (#7607)

<!-- Released section -->
<!-- Don't change this section unless doing release -->

Expand Down
20 changes: 13 additions & 7 deletions detectors/aws/ecs/ecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"net/http"
"os"
"regexp"
"runtime"
"strings"

Expand All @@ -35,6 +36,7 @@ var (
errCannotParseTaskArn = errors.New("cannot parse region and account ID from the Task's ARN: the ARN does not contain at least 6 segments separated by the ':' character")
errCannotRetrieveLogsGroupMetadataV4 = errors.New("the ECS Metadata v4 did not return a AwsLogGroup name")
errCannotRetrieveLogsStreamMetadataV4 = errors.New("the ECS Metadata v4 did not return a AwsLogStream name")
ecsCgroupPathPattern = regexp.MustCompile(`/ecs/[^/]+/[a-f0-9]{64}$`)
)

// Create interface for methods needing to be mocked.
Expand Down Expand Up @@ -246,13 +248,7 @@ func (ecsUtils ecsDetectorUtils) getContainerID() (string, error) {
// For example, windows; or when running integration tests outside of a container.
return "", nil
}
splitData := strings.Split(strings.TrimSpace(string(fileData)), "\n")
for _, str := range splitData {
if len(str) > containerIDLength {
return str[len(str)-containerIDLength:], nil
}
}
return "", nil
return getCgroupContainerID(fileData), nil
}

// returns host name reported by the kernel.
Expand All @@ -263,3 +259,13 @@ func (ecsUtils ecsDetectorUtils) getContainerName() (string, error) {
}
return hostName, nil
}

func getCgroupContainerID(fileData []byte) string {
splitData := strings.Split(strings.TrimSpace(string(fileData)), "\n")
for _, str := range splitData {
if ecsCgroupPathPattern.MatchString(str) {
return str[len(str)-containerIDLength:]
}
}
return ""
}
31 changes: 31 additions & 0 deletions detectors/aws/ecs/ecs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,34 @@ func TestLogsAttributesAlternatePartition(t *testing.T) {
}
assert.Equal(t, expectedAttributes, actualAttributes, "logs attributes are incorrect")
}

func TestCgroupContainerID(t *testing.T) {
cgroups := []struct {
cgroupPath string
wantContainerID string
}{
{
"10:memory:/ecs/my-task-name/1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
"1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
},
{
"10:memory:/ecs/api_service_1/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
},
{
"10:memory:/ecs/my-task-name/12345abc",
"",
},
{
"10:memory:/docker/my-task-name/1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
"",
},
}

for _, c := range cgroups {
t.Run(c.cgroupPath, func(t *testing.T) {
containerID := getCgroupContainerID([]byte(c.cgroupPath))
assert.Equal(t, c.wantContainerID, containerID)
})
}
}
Loading