From a419f4c69356b2243c04bd9396013e702f2bdf39 Mon Sep 17 00:00:00 2001 From: Alex Kats Date: Mon, 21 Jul 2025 21:39:43 -0400 Subject: [PATCH 1/4] updated getContainerID method to match regex --- CHANGELOG.md | 4 ++++ detectors/aws/ecs/ecs.go | 20 +++++++++++++------- detectors/aws/ecs/ecs_test.go | 31 +++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a066f1da71..54b5e9ef6f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,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 + +- Fixed ECS detector failing tests on Ubuntu by updating `getContainerID` method. (#7500) + diff --git a/detectors/aws/ecs/ecs.go b/detectors/aws/ecs/ecs.go index a256fb8430f..5a1678e71f0 100644 --- a/detectors/aws/ecs/ecs.go +++ b/detectors/aws/ecs/ecs.go @@ -10,6 +10,7 @@ import ( "fmt" "net/http" "os" + "regexp" "runtime" "strings" @@ -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. @@ -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) } // returns host name reported by the kernel. @@ -263,3 +259,13 @@ func (ecsUtils ecsDetectorUtils) getContainerName() (string, error) { } return hostName, nil } + +func getCgroupContainerID(fileData []byte) (string, error) { + splitData := strings.Split(strings.TrimSpace(string(fileData)), "\n") + for _, str := range splitData { + if ecsCgroupPathPattern.MatchString(str) { + return str[len(str)-containerIDLength:], nil + } + } + return "", nil +} diff --git a/detectors/aws/ecs/ecs_test.go b/detectors/aws/ecs/ecs_test.go index 2deb397258a..fd3d529e77d 100644 --- a/detectors/aws/ecs/ecs_test.go +++ b/detectors/aws/ecs/ecs_test.go @@ -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) + }) + } +} From abdcb314a349af397bd65a289e069c17f4e21b72 Mon Sep 17 00:00:00 2001 From: Alex Kats Date: Wed, 23 Jul 2025 00:39:55 -0400 Subject: [PATCH 2/4] updated chlog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 54b5e9ef6f8..7c0a45cdc4d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,7 +28,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ### Fixed -- Fixed ECS detector failing tests on Ubuntu by updating `getContainerID` method. (#7500) +- Fixed ECS detector failing tests on Ubuntu. (#7500) From c75c902da5a35829f562ff0449f605816dae9015 Mon Sep 17 00:00:00 2001 From: Alex Kats Date: Wed, 23 Jul 2025 20:22:46 -0400 Subject: [PATCH 3/4] updated chlog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c0a45cdc4d..12ee715fecc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,7 +28,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ### Fixed -- Fixed ECS detector failing tests on Ubuntu. (#7500) +- Improve the ECS detector correctness in `go.opentelemetry.io/contrib/detectors/aws/ecs`. (#7607) From 84f836a55c29dc3c7691b44aae8f03af55aedbbe Mon Sep 17 00:00:00 2001 From: Alex Kats Date: Fri, 25 Jul 2025 11:56:10 -0400 Subject: [PATCH 4/4] refactored cgroup method --- detectors/aws/ecs/ecs.go | 8 ++++---- detectors/aws/ecs/ecs_test.go | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/detectors/aws/ecs/ecs.go b/detectors/aws/ecs/ecs.go index 5a1678e71f0..b2753807664 100644 --- a/detectors/aws/ecs/ecs.go +++ b/detectors/aws/ecs/ecs.go @@ -248,7 +248,7 @@ func (ecsUtils ecsDetectorUtils) getContainerID() (string, error) { // For example, windows; or when running integration tests outside of a container. return "", nil } - return getCgroupContainerID(fileData) + return getCgroupContainerID(fileData), nil } // returns host name reported by the kernel. @@ -260,12 +260,12 @@ func (ecsUtils ecsDetectorUtils) getContainerName() (string, error) { return hostName, nil } -func getCgroupContainerID(fileData []byte) (string, error) { +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:], nil + return str[len(str)-containerIDLength:] } } - return "", nil + return "" } diff --git a/detectors/aws/ecs/ecs_test.go b/detectors/aws/ecs/ecs_test.go index fd3d529e77d..ede92a48b86 100644 --- a/detectors/aws/ecs/ecs_test.go +++ b/detectors/aws/ecs/ecs_test.go @@ -256,7 +256,7 @@ func TestCgroupContainerID(t *testing.T) { for _, c := range cgroups { t.Run(c.cgroupPath, func(t *testing.T) { - containerID, _ := getCgroupContainerID([]byte(c.cgroupPath)) + containerID := getCgroupContainerID([]byte(c.cgroupPath)) assert.Equal(t, c.wantContainerID, containerID) }) }