33
33
34
34
35
35
def _read_file (filename : str ) -> str :
36
- with open (os .path .join (os .path .dirname (__file__ ), filename )) as f :
36
+ with open (os .path .join (os .path .dirname (__file__ ), 'ecs' , filename )) as f :
37
37
return f .read ()
38
38
39
39
40
40
MetadataV4Uri = "mock-uri-4"
41
41
42
42
43
- MetadataV4ContainerResponse = _read_file ("metadatav4-response-container.json" )
43
+ MetadataV4ContainerResponseEc2 = _read_file ("metadatav4-response-container-ec2 .json" )
44
44
45
45
46
- MetadataV4TaskResponse = _read_file ("metadatav4-response-task.json" )
46
+ MetadataV4TaskResponseEc2 = _read_file ("metadatav4-response-task-ec2 .json" )
47
47
48
48
49
- def _http_get_function (url : str , * args , ** kwargs ) -> str :
49
+ MetadataV4ContainerResponseFargate = _read_file ("metadatav4-response-container-fargate.json" )
50
+
51
+
52
+ MetadataV4TaskResponseFargate = _read_file ("metadatav4-response-task-fargate.json" )
53
+
54
+
55
+ def _http_get_function_ec2 (url : str , * args , ** kwargs ) -> str :
56
+ if url == MetadataV4Uri :
57
+ return MetadataV4ContainerResponseEc2
58
+ if url == f"{ MetadataV4Uri } /task" :
59
+ return MetadataV4TaskResponseEc2
60
+
61
+
62
+ def _http_get_function_fargate (url : str , * args , ** kwargs ) -> str :
50
63
if url == MetadataV4Uri :
51
- return MetadataV4ContainerResponse
64
+ return MetadataV4ContainerResponseFargate
52
65
if url == f"{ MetadataV4Uri } /task" :
53
- return MetadataV4TaskResponse
66
+ return MetadataV4TaskResponseFargate
54
67
55
68
56
69
class AwsEcsResourceDetectorTest (unittest .TestCase ):
@@ -123,13 +136,13 @@ def test_simple_create_metadata_v3(
123
136
@patch (
124
137
"opentelemetry.sdk.extension.aws.resource.ecs._http_get" ,
125
138
)
126
- def test_simple_create_metadata_v4 (
139
+ def test_simple_create_metadata_v4_launchtype_ec2 (
127
140
self ,
128
141
mock_http_get_function ,
129
142
mock_open_function ,
130
143
mock_socket_gethostname ,
131
144
):
132
- mock_http_get_function .side_effect = _http_get_function
145
+ mock_http_get_function .side_effect = _http_get_function_ec2
133
146
actual = AwsEcsResourceDetector ().detect ()
134
147
self .maxDiff = None
135
148
self .assertDictEqual (
@@ -150,3 +163,62 @@ def test_simple_create_metadata_v4(
150
163
}
151
164
),
152
165
)
166
+
167
+ @patch .dict (
168
+ "os.environ" ,
169
+ {"ECS_CONTAINER_METADATA_URI_V4" : MetadataV4Uri },
170
+ clear = True ,
171
+ )
172
+ @patch (
173
+ "socket.gethostname" ,
174
+ return_value = f"{ MockEcsResourceAttributes [ResourceAttributes .CONTAINER_NAME ]} " ,
175
+ )
176
+ @patch (
177
+ "builtins.open" ,
178
+ new_callable = mock_open ,
179
+ read_data = f"""14:name=systemd:/docker/{ MockEcsResourceAttributes [ResourceAttributes .CONTAINER_ID ]}
180
+ 13:rdma:/
181
+ 12:pids:/docker/bogusContainerIdThatShouldNotBeOneSetBecauseTheFirstOneWasPicked
182
+ 11:hugetlb:/docker/bogusContainerIdThatShouldNotBeOneSetBecauseTheFirstOneWasPicked
183
+ 10:net_prio:/docker/bogusContainerIdThatShouldNotBeOneSetBecauseTheFirstOneWasPicked
184
+ 9:perf_event:/docker/bogusContainerIdThatShouldNotBeOneSetBecauseTheFirstOneWasPicked
185
+ 8:net_cls:/docker/bogusContainerIdThatShouldNotBeOneSetBecauseTheFirstOneWasPicked
186
+ 7:freezer:/docker/
187
+ 6:devices:/docker/bogusContainerIdThatShouldNotBeOneSetBecauseTheFirstOneWasPicked
188
+ 5:memory:/docker/bogusContainerIdThatShouldNotBeOneSetBecauseTheFirstOneWasPicked
189
+ 4:blkio:/docker/bogusContainerIdThatShouldNotBeOneSetBecauseTheFirstOneWasPicked
190
+ 3:cpuacct:/docker/bogusContainerIdThatShouldNotBeOneSetBecauseTheFirstOneWasPicked
191
+ 2:cpu:/docker/bogusContainerIdThatShouldNotBeOneSetBecauseTheFirstOneWasPicked
192
+ 1:cpuset:/docker/bogusContainerIdThatShouldNotBeOneSetBecauseTheFirstOneWasPicked
193
+ """ ,
194
+ )
195
+ @patch (
196
+ "opentelemetry.sdk.extension.aws.resource.ecs._http_get" ,
197
+ )
198
+ def test_simple_create_metadata_v4_launchtype_fargate (
199
+ self ,
200
+ mock_http_get_function ,
201
+ mock_open_function ,
202
+ mock_socket_gethostname ,
203
+ ):
204
+ mock_http_get_function .side_effect = _http_get_function_fargate
205
+ actual = AwsEcsResourceDetector ().detect ()
206
+ self .maxDiff = None
207
+ self .assertDictEqual (
208
+ actual .attributes .copy (),
209
+ OrderedDict (
210
+ {
211
+ ** MockEcsResourceAttributes ,
212
+ ResourceAttributes .AWS_LOG_GROUP_NAMES : ("/ecs/containerlogs" ,),
213
+ ResourceAttributes .AWS_LOG_GROUP_ARNS : ("arn:aws:logs:us-west-2:111122223333:log-group:/ecs/containerlogs:*" ,),
214
+ ResourceAttributes .AWS_LOG_STREAM_NAMES : ("ecs/curl/cd189a933e5849daa93386466019ab50" ,),
215
+ ResourceAttributes .AWS_LOG_STREAM_ARNS : ("arn:aws:logs:us-west-2:111122223333:log-group:/ecs/containerlogs:log-stream:ecs/curl/cd189a933e5849daa93386466019ab50" ,),
216
+ ResourceAttributes .AWS_ECS_CONTAINER_ARN : "arn:aws:ecs:us-west-2:111122223333:container/05966557-f16c-49cb-9352-24b3a0dcd0e1" ,
217
+ ResourceAttributes .AWS_ECS_CLUSTER_ARN : "arn:aws:ecs:us-west-2:111122223333:cluster/default" ,
218
+ ResourceAttributes .AWS_ECS_LAUNCHTYPE : "fargate" ,
219
+ ResourceAttributes .AWS_ECS_TASK_ARN : "arn:aws:ecs:us-west-2:111122223333:task/default/e9028f8d5d8e4f258373e7b93ce9a3c3" ,
220
+ ResourceAttributes .AWS_ECS_TASK_FAMILY : "curltest" ,
221
+ ResourceAttributes .AWS_ECS_TASK_REVISION : "3" ,
222
+ }
223
+ ),
224
+ )
0 commit comments