| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package ecs |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"fmt" |
| 9 |
"testing" |
| 10 |
|
| 11 |
metadata "github.com/brunoscheufler/aws-ecs-metadata-go" |
| 12 |
"github.com/stretchr/testify/assert" |
| 13 |
"github.com/stretchr/testify/mock" |
| 14 |
"go.opentelemetry.io/otel/attribute" |
| 15 |
"go.opentelemetry.io/otel/sdk/resource" |
| 16 |
semconv "go.opentelemetry.io/otel/semconv/v1.40.0" |
| 17 |
) |
| 18 |
|
| 19 |
// Create interface for functions that need to be mocked. |
| 20 |
type MockDetectorUtils struct { |
| 21 |
mock.Mock |
| 22 |
} |
| 23 |
|
| 24 |
func (detectorUtils *MockDetectorUtils) getContainerID() (string, error) { |
| 25 |
args := detectorUtils.Called() |
| 26 |
return args.String(0), args.Error(1) |
| 27 |
} |
| 28 |
|
| 29 |
func (detectorUtils *MockDetectorUtils) getContainerName() (string, error) { |
| 30 |
args := detectorUtils.Called() |
| 31 |
return args.String(0), args.Error(1) |
| 32 |
} |
| 33 |
|
| 34 |
func (detectorUtils *MockDetectorUtils) getContainerMetadataV4(context.Context) (*metadata.ContainerMetadataV4, error) { |
| 35 |
args := detectorUtils.Called() |
| 36 |
return args.Get(0).(*metadata.ContainerMetadataV4), args.Error(1) |
| 37 |
} |
| 38 |
|
| 39 |
func (detectorUtils *MockDetectorUtils) getTaskMetadataV4(context.Context) (*metadata.TaskMetadataV4, error) { |
| 40 |
args := detectorUtils.Called() |
| 41 |
return args.Get(0).(*metadata.TaskMetadataV4), args.Error(1) |
| 42 |
} |
| 43 |
|
| 44 |
// successfully returns resource when process is running on Amazon ECS environment |
| 45 |
// with no Metadata v4. |
| 46 |
func TestDetectV3(t *testing.T) { |
| 47 |
t.Setenv(metadataV3EnvVar, "3") |
| 48 |
|
| 49 |
detectorUtils := new(MockDetectorUtils) |
| 50 |
|
| 51 |
detectorUtils.On("getContainerName").Return("container-Name", nil) |
| 52 |
detectorUtils.On("getContainerID").Return("0123456789A", nil) |
| 53 |
detectorUtils.On("getContainerMetadataV4").Return(nil, fmt.Errorf("not supported")) |
| 54 |
detectorUtils.On("getTaskMetadataV4").Return(nil, fmt.Errorf("not supported")) |
| 55 |
|
| 56 |
attributes := []attribute.KeyValue{ |
| 57 |
semconv.CloudProviderAWS, |
| 58 |
semconv.CloudPlatformAWSECS, |
| 59 |
semconv.ContainerName("container-Name"), |
| 60 |
semconv.ContainerID("0123456789A"), |
| 61 |
} |
| 62 |
expectedResource := resource.NewWithAttributes(semconv.SchemaURL, attributes...) |
| 63 |
detector := &resourceDetector{utils: detectorUtils} |
| 64 |
res, _ := detector.Detect(t.Context()) |
| 65 |
|
| 66 |
assert.Equal(t, expectedResource, res, "Resource returned is incorrect") |
| 67 |
} |
| 68 |
|
| 69 |
// successfully returns resource when process is running on Amazon ECS environment |
| 70 |
// with Metadata v4. |
| 71 |
func TestDetectV4(t *testing.T) { |
| 72 |
t.Setenv(metadataV4EnvVar, "4") |
| 73 |
|
| 74 |
detectorUtils := new(MockDetectorUtils) |
| 75 |
|
| 76 |
detectorUtils.On("getContainerName").Return("container-Name", nil) |
| 77 |
detectorUtils.On("getContainerID").Return("0123456789A", nil) |
| 78 |
detectorUtils.On("getContainerMetadataV4").Return(&metadata.ContainerMetadataV4{ |
| 79 |
ContainerARN: "arn:aws:ecs:us-west-2:111122223333:container/05966557-f16c-49cb-9352-24b3a0dcd0e1", |
| 80 |
}, nil) |
| 81 |
detectorUtils.On("getTaskMetadataV4").Return(&metadata.TaskMetadataV4{ |
| 82 |
Cluster: "arn:aws:ecs:us-west-2:111122223333:cluster/default", |
| 83 |
TaskARN: "arn:aws:ecs:us-west-2:111122223333:task/default/e9028f8d5d8e4f258373e7b93ce9a3c3", |
| 84 |
Family: "curltest", |
| 85 |
Revision: "3", |
| 86 |
DesiredStatus: "RUNNING", |
| 87 |
KnownStatus: "RUNNING", |
| 88 |
Limits: metadata.Limits{ |
| 89 |
CPU: 0.25, |
| 90 |
Memory: 512, |
| 91 |
}, |
| 92 |
AvailabilityZone: "us-west-2a", |
| 93 |
LaunchType: "FARGATE", |
| 94 |
}, nil) |
| 95 |
|
| 96 |
attributes := []attribute.KeyValue{ |
| 97 |
semconv.CloudProviderAWS, |
| 98 |
semconv.CloudPlatformAWSECS, |
| 99 |
semconv.CloudAccountID("111122223333"), |
| 100 |
semconv.CloudRegion("us-west-2"), |
| 101 |
semconv.CloudAvailabilityZone("us-west-2a"), |
| 102 |
semconv.CloudResourceID("arn:aws:ecs:us-west-2:111122223333:container/05966557-f16c-49cb-9352-24b3a0dcd0e1"), |
| 103 |
semconv.ContainerName("container-Name"), |
| 104 |
semconv.ContainerID("0123456789A"), |
| 105 |
semconv.AWSECSClusterARN("arn:aws:ecs:us-west-2:111122223333:cluster/default"), |
| 106 |
semconv.AWSECSTaskARN("arn:aws:ecs:us-west-2:111122223333:task/default/e9028f8d5d8e4f258373e7b93ce9a3c3"), |
| 107 |
semconv.AWSECSLaunchtypeKey.String("fargate"), |
| 108 |
semconv.AWSECSTaskFamily("curltest"), |
| 109 |
semconv.AWSECSTaskRevision("3"), |
| 110 |
semconv.AWSECSContainerARN("arn:aws:ecs:us-west-2:111122223333:container/05966557-f16c-49cb-9352-24b3a0dcd0e1"), |
| 111 |
} |
| 112 |
expectedResource := resource.NewWithAttributes(semconv.SchemaURL, attributes...) |
| 113 |
detector := &resourceDetector{utils: detectorUtils} |
| 114 |
res, _ := detector.Detect(t.Context()) |
| 115 |
|
| 116 |
assert.Equal(t, expectedResource, res, "Resource returned is incorrect") |
| 117 |
} |
| 118 |
|
| 119 |
// returns empty resource when detector receives a bad task ARN from the Metadata v4 endpoint. |
| 120 |
func TestDetectBadARNsv4(t *testing.T) { |
| 121 |
t.Setenv(metadataV4EnvVar, "4") |
| 122 |
|
| 123 |
detectorUtils := new(MockDetectorUtils) |
| 124 |
|
| 125 |
detectorUtils.On("getContainerName").Return("container-Name", nil) |
| 126 |
detectorUtils.On("getContainerID").Return("0123456789A", nil) |
| 127 |
detectorUtils.On("getContainerMetadataV4").Return(&metadata.ContainerMetadataV4{ |
| 128 |
ContainerARN: "container/05966557-f16c-49cb-9352-24b3a0dcd0e1", |
| 129 |
}, nil) |
| 130 |
detectorUtils.On("getTaskMetadataV4").Return(&metadata.TaskMetadataV4{ |
| 131 |
Cluster: "default", |
| 132 |
TaskARN: "default/e9028f8d5d8e4f258373e7b93ce9a3c3", |
| 133 |
Family: "curltest", |
| 134 |
Revision: "3", |
| 135 |
DesiredStatus: "RUNNING", |
| 136 |
KnownStatus: "RUNNING", |
| 137 |
Limits: metadata.Limits{ |
| 138 |
CPU: 0.25, |
| 139 |
Memory: 512, |
| 140 |
}, |
| 141 |
AvailabilityZone: "us-west-2a", |
| 142 |
LaunchType: "FARGATE", |
| 143 |
}, nil) |
| 144 |
|
| 145 |
detector := &resourceDetector{utils: detectorUtils} |
| 146 |
_, err := detector.Detect(t.Context()) |
| 147 |
|
| 148 |
assert.Equal(t, errCannotParseTaskArn, err) |
| 149 |
} |
| 150 |
|
| 151 |
// returns empty resource when detector cannot read container ID. |
| 152 |
func TestDetectCannotReadContainerID(t *testing.T) { |
| 153 |
t.Setenv(metadataV3EnvVar, "3") |
| 154 |
detectorUtils := new(MockDetectorUtils) |
| 155 |
|
| 156 |
detectorUtils.On("getContainerName").Return("container-Name", nil) |
| 157 |
detectorUtils.On("getContainerID").Return("", nil) |
| 158 |
detectorUtils.On("getContainerMetadataV4").Return(nil, fmt.Errorf("not supported")) |
| 159 |
detectorUtils.On("getTaskMetadataV4").Return(nil, fmt.Errorf("not supported")) |
| 160 |
|
| 161 |
attributes := []attribute.KeyValue{ |
| 162 |
semconv.CloudProviderAWS, |
| 163 |
semconv.CloudPlatformAWSECS, |
| 164 |
semconv.ContainerName("container-Name"), |
| 165 |
semconv.ContainerID(""), |
| 166 |
} |
| 167 |
expectedResource := resource.NewWithAttributes(semconv.SchemaURL, attributes...) |
| 168 |
detector := &resourceDetector{utils: detectorUtils} |
| 169 |
res, err := detector.Detect(t.Context()) |
| 170 |
|
| 171 |
assert.NoError(t, err) |
| 172 |
assert.Equal(t, expectedResource, res, "Resource returned is incorrect") |
| 173 |
} |
| 174 |
|
| 175 |
// returns empty resource when detector cannot read container Name. |
| 176 |
func TestDetectCannotReadContainerName(t *testing.T) { |
| 177 |
t.Setenv(metadataV3EnvVar, "3") |
| 178 |
detectorUtils := new(MockDetectorUtils) |
| 179 |
|
| 180 |
detectorUtils.On("getContainerName").Return("", errCannotReadContainerName) |
| 181 |
detectorUtils.On("getContainerID").Return("0123456789A", nil) |
| 182 |
detectorUtils.On("getContainerMetadataV4").Return(nil, fmt.Errorf("not supported")) |
| 183 |
detectorUtils.On("getTaskMetadataV4").Return(nil, fmt.Errorf("not supported")) |
| 184 |
|
| 185 |
detector := &resourceDetector{utils: detectorUtils} |
| 186 |
res, err := detector.Detect(t.Context()) |
| 187 |
|
| 188 |
assert.Equal(t, errCannotReadContainerName, err) |
| 189 |
assert.Empty(t, res.Attributes()) |
| 190 |
} |
| 191 |
|
| 192 |
// returns empty resource when process is not running ECS. |
| 193 |
func TestReturnsIfNoEnvVars(t *testing.T) { |
| 194 |
detector := &resourceDetector{utils: nil} |
| 195 |
res, err := detector.Detect(t.Context()) |
| 196 |
|
| 197 |
// When not on ECS, the detector should return nil and not error. |
| 198 |
assert.NoError(t, err, "failure to detect when not on platform must not be an error") |
| 199 |
assert.Nil(t, res, "failure to detect should return a nil Resource to optimize merge") |
| 200 |
} |
| 201 |
|
| 202 |
// handles alternative aws partitions (e.g. AWS GovCloud). |
| 203 |
func TestLogsAttributesAlternatePartition(t *testing.T) { |
| 204 |
detector := &resourceDetector{utils: nil} |
| 205 |
|
| 206 |
containerMetadata := &metadata.ContainerMetadataV4{ |
| 207 |
LogDriver: "awslogs", |
| 208 |
LogOptions: struct { |
| 209 |
AwsLogsCreateGroup string `json:"awslogs-create-group"` |
| 210 |
AwsLogsGroup string `json:"awslogs-group"` |
| 211 |
AwsLogsStream string `json:"awslogs-stream"` |
| 212 |
AwsRegion string `json:"awslogs-region"` |
| 213 |
}{ |
| 214 |
"fake-create", |
| 215 |
"fake-group", |
| 216 |
"fake-stream", |
| 217 |
"", |
| 218 |
}, |
| 219 |
ContainerARN: "arn:arn-partition:arn-svc:arn-region:arn-account:arn-resource", |
| 220 |
} |
| 221 |
actualAttributes, err := detector.getLogsAttributes(containerMetadata) |
| 222 |
assert.NoError(t, err, "failure with nonstandard partition") |
| 223 |
|
| 224 |
expectedAttributes := []attribute.KeyValue{ |
| 225 |
semconv.AWSLogGroupNames(containerMetadata.LogOptions.AwsLogsGroup), |
| 226 |
semconv.AWSLogGroupARNs("arn:arn-partition:logs:arn-region:arn-account:log-group:fake-group:*"), |
| 227 |
semconv.AWSLogStreamNames(containerMetadata.LogOptions.AwsLogsStream), |
| 228 |
semconv.AWSLogStreamARNs("arn:arn-partition:logs:arn-region:arn-account:log-group:fake-group:log-stream:fake-stream"), |
| 229 |
} |
| 230 |
assert.Equal(t, expectedAttributes, actualAttributes, "logs attributes are incorrect") |
| 231 |
} |
| 232 |
|
| 233 |
func TestCgroupContainerID(t *testing.T) { |
| 234 |
cgroups := []struct { |
| 235 |
cgroupPath string |
| 236 |
wantContainerID string |
| 237 |
}{ |
| 238 |
{ |
| 239 |
"10:memory:/ecs/my-task-name/1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef", |
| 240 |
"1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef", |
| 241 |
}, |
| 242 |
{ |
| 243 |
"10:memory:/ecs/api_service_1/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", |
| 244 |
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", |
| 245 |
}, |
| 246 |
{ |
| 247 |
"10:memory:/ecs/my-task-name/12345abc", |
| 248 |
"", |
| 249 |
}, |
| 250 |
{ |
| 251 |
"10:memory:/docker/my-task-name/1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef", |
| 252 |
"", |
| 253 |
}, |
| 254 |
} |
| 255 |
|
| 256 |
for _, c := range cgroups { |
| 257 |
t.Run(c.cgroupPath, func(t *testing.T) { |
| 258 |
containerID := getCgroupContainerID([]byte(c.cgroupPath)) |
| 259 |
assert.Equal(t, c.wantContainerID, containerID) |
| 260 |
}) |
| 261 |
} |
| 262 |
} |