-
Notifications
You must be signed in to change notification settings - Fork 591
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement aws.ecs.* resource attributes (#2626)
* Implemented aws.ecs.* resource attributes in go.opentelemetry.io/detectors/aws/ecs * Update detectors/aws/ecs/ecs_test.go Co-authored-by: Chester Cheung <[email protected]> * Update detectors/aws/ecs/ecs_test.go Co-authored-by: Chester Cheung <[email protected]> * fix: lower-case the value of aws.ecs.launchtype * Add aws.logs.* support, remove spurious /aws prefix from log group * Add tests for V4 on Fargate launch type * Rebase * Fix integration tests, fix behavior on Windows After a surreal session of debugging, it turns out that httptest.NewServer fails on Windows by failing to bind the server socket unless the GoLang package name contains "test". To deal with that, I split the tests between "integration tests" needing a HTTP server into "ecs/test", and the other in "ecs". In order to work around the need of being resilient to the lack of /proc/self/cgroup (which happens in our tests but, above all, when running containers on Windows), the ECVS detector is now more lenient to not finding the container id. Co-authored-by: Chester Cheung <[email protected]>
- Loading branch information
1 parent
15ceaa2
commit 01b39e7
Showing
11 changed files
with
533 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package ecs | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"strings" | ||
"testing" | ||
|
||
ecs "go.opentelemetry.io/contrib/detectors/aws/ecs" | ||
"go.opentelemetry.io/otel/attribute" | ||
"go.opentelemetry.io/otel/sdk/resource" | ||
semconv "go.opentelemetry.io/otel/semconv/v1.12.0" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
const ( | ||
metadataV4EnvVar = "ECS_CONTAINER_METADATA_URI_V4" | ||
) | ||
|
||
// successfully returns resource when process is running on Amazon ECS environment | ||
// with Metadata v4 with the EC2 Launch type. | ||
func TestDetectV4LaunchTypeEc2(t *testing.T) { | ||
testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { | ||
if strings.HasSuffix(req.URL.String(), "/task") { | ||
content, err := os.ReadFile("metadatav4-response-task-ec2.json") | ||
if err == nil { | ||
_, err = res.Write(content) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
} else { | ||
content, err := os.ReadFile("metadatav4-response-container-ec2.json") | ||
if err == nil { | ||
_, err = res.Write(content) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
} | ||
})) | ||
defer testServer.Close() | ||
|
||
os.Clearenv() | ||
_ = os.Setenv(metadataV4EnvVar, testServer.URL) | ||
|
||
hostname, err := os.Hostname() | ||
assert.NoError(t, err, "Error") | ||
|
||
attributes := []attribute.KeyValue{ | ||
semconv.CloudProviderAWS, | ||
semconv.CloudPlatformAWSECS, | ||
semconv.ContainerNameKey.String(hostname), | ||
// We are not running the test in an actual container, | ||
// the container id is tested with mocks of the cgroup | ||
// file in the unit tests | ||
semconv.ContainerIDKey.String(""), | ||
semconv.AWSECSContainerARNKey.String("arn:aws:ecs:us-west-2:111122223333:container/0206b271-b33f-47ab-86c6-a0ba208a70a9"), | ||
semconv.AWSECSClusterARNKey.String("arn:aws:ecs:us-west-2:111122223333:cluster/default"), | ||
semconv.AWSECSLaunchtypeKey.String("ec2"), | ||
semconv.AWSECSTaskARNKey.String("arn:aws:ecs:us-west-2:111122223333:task/default/158d1c8083dd49d6b527399fd6414f5c"), | ||
semconv.AWSECSTaskFamilyKey.String("curltest"), | ||
semconv.AWSECSTaskRevisionKey.String("26"), | ||
semconv.AWSLogGroupNamesKey.String("/ecs/metadata"), | ||
semconv.AWSLogGroupARNsKey.String("arn:aws:logs:us-west-2:111122223333:log-group:/ecs/metadata:*"), | ||
semconv.AWSLogStreamNamesKey.String("ecs/curl/8f03e41243824aea923aca126495f665"), | ||
semconv.AWSLogStreamARNsKey.String("arn:aws:logs:us-west-2:111122223333:log-group:/ecs/metadata:log-stream:ecs/curl/8f03e41243824aea923aca126495f665"), | ||
} | ||
expectedResource := resource.NewWithAttributes(semconv.SchemaURL, attributes...) | ||
detector := ecs.NewResourceDetector() | ||
res, err := detector.Detect(context.Background()) | ||
|
||
assert.Equal(t, nil, err, "Detector should not fail") | ||
assert.Equal(t, expectedResource, res, "Resource returned is incorrect") | ||
} | ||
|
||
// successfully returns resource when process is running on Amazon ECS environment | ||
// with Metadata v4 with the Fargate Launch type. | ||
func TestDetectV4LaunchTypeFargate(t *testing.T) { | ||
testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { | ||
if strings.HasSuffix(req.URL.String(), "/task") { | ||
content, err := os.ReadFile("metadatav4-response-task-fargate.json") | ||
if err == nil { | ||
_, err = res.Write(content) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
} else { | ||
content, err := os.ReadFile("metadatav4-response-container-fargate.json") | ||
if err == nil { | ||
_, err = res.Write(content) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
} | ||
})) | ||
defer testServer.Close() | ||
|
||
os.Clearenv() | ||
_ = os.Setenv(metadataV4EnvVar, testServer.URL) | ||
|
||
hostname, err := os.Hostname() | ||
assert.NoError(t, err, "Error") | ||
|
||
attributes := []attribute.KeyValue{ | ||
semconv.CloudProviderAWS, | ||
semconv.CloudPlatformAWSECS, | ||
semconv.ContainerNameKey.String(hostname), | ||
// We are not running the test in an actual container, | ||
// the container id is tested with mocks of the cgroup | ||
// file in the unit tests | ||
semconv.ContainerIDKey.String(""), | ||
semconv.AWSECSContainerARNKey.String("arn:aws:ecs:us-west-2:111122223333:container/05966557-f16c-49cb-9352-24b3a0dcd0e1"), | ||
semconv.AWSECSClusterARNKey.String("arn:aws:ecs:us-west-2:111122223333:cluster/default"), | ||
semconv.AWSECSLaunchtypeKey.String("fargate"), | ||
semconv.AWSECSTaskARNKey.String("arn:aws:ecs:us-west-2:111122223333:task/default/e9028f8d5d8e4f258373e7b93ce9a3c3"), | ||
semconv.AWSECSTaskFamilyKey.String("curltest"), | ||
semconv.AWSECSTaskRevisionKey.String("3"), | ||
semconv.AWSLogGroupNamesKey.String("/ecs/containerlogs"), | ||
semconv.AWSLogGroupARNsKey.String("arn:aws:logs:us-west-2:111122223333:log-group:/ecs/containerlogs:*"), | ||
semconv.AWSLogStreamNamesKey.String("ecs/curl/cd189a933e5849daa93386466019ab50"), | ||
semconv.AWSLogStreamARNsKey.String("arn:aws:logs:us-west-2:111122223333:log-group:/ecs/containerlogs:log-stream:ecs/curl/cd189a933e5849daa93386466019ab50"), | ||
} | ||
expectedResource := resource.NewWithAttributes(semconv.SchemaURL, attributes...) | ||
detector := ecs.NewResourceDetector() | ||
res, err := detector.Detect(context.Background()) | ||
|
||
assert.Equal(t, nil, err, "Detector should not fail") | ||
assert.Equal(t, expectedResource, res, "Resource returned is incorrect") | ||
} |
Oops, something went wrong.