@@ -9,6 +9,8 @@ use reqwest::Method;
99use serde:: Deserialize ;
1010use thiserror:: Error ;
1111
12+ use crate :: metadata:: OrderedF64 ;
13+
1214use super :: AgentMetadata ;
1315
1416/// An error converting Fargate IMDS metadata to Agent metadata. This error
@@ -80,12 +82,25 @@ async fn read_ec2_metadata() -> Result<ImdsEc2InstanceMetadata, AwsProfilerMetad
8082 Ok ( serde_json:: from_str ( imds_document. as_ref ( ) ) ?)
8183}
8284
85+ #[ derive( Deserialize , Debug , PartialEq , Eq ) ]
86+ struct FargateLimits {
87+ #[ serde( rename = "CPU" ) ]
88+ cpu : Option < OrderedF64 > ,
89+ #[ serde( rename = "Memory" ) ]
90+ memory : Option < u64 > ,
91+ }
92+
8393#[ derive( Deserialize , Debug , PartialEq , Eq ) ]
8494struct FargateMetadata {
8595 #[ serde( rename = "Cluster" ) ]
8696 cluster : String ,
8797 #[ serde( rename = "TaskARN" ) ]
8898 task_arn : String ,
99+ // According to <https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-metadata-endpoint-v4-fargate-response.html>
100+ // Limits: The resource limits specified at the task levels such as CPU (expressed in vCPUs) and memory.
101+ // This parameter is omitted if no resource limits are defined.
102+ #[ serde( rename = "Limits" ) ]
103+ limits : Option < FargateLimits > ,
89104}
90105
91106async fn read_fargate_metadata (
@@ -144,6 +159,10 @@ impl super::AgentMetadata {
144159 . to_string ( ) ,
145160 ecs_task_arn : fargate_metadata. task_arn ,
146161 ecs_cluster_arn : fargate_metadata. cluster ,
162+ #[ cfg( feature = "__unstable-fargate-cpu-count" ) ]
163+ cpu_limit : fargate_metadata. limits . as_ref ( ) . and_then ( |limits| limits. cpu ) ,
164+ #[ cfg( feature = "__unstable-fargate-cpu-count" ) ]
165+ memory_limit : fargate_metadata. limits . as_ref ( ) . and_then ( |limits| limits. memory ) ,
147166 } )
148167 }
149168}
@@ -173,6 +192,7 @@ pub async fn load_agent_metadata() -> Result<AgentMetadata, AwsProfilerMetadataE
173192#[ cfg( test) ]
174193mod tests {
175194 use super :: * ;
195+ use test_case:: test_case;
176196
177197 // these constants are "anonymized" (aka randomly generated in that format)
178198
@@ -237,10 +257,29 @@ mod tests {
237257 )
238258 }
239259
240- #[ test]
241- fn test_fargate_metadata ( ) {
242- let json_str = r#"
243- {
260+ #[ test_case(
261+ r#"{
262+ "Cluster": "arn:aws:ecs:us-east-1:123456789012:cluster/profiler-metadata-cluster",
263+ "TaskARN": "arn:aws:ecs:us-east-1:123456789012:task/profiler-metadata-cluster/5261e761e0e2a3d92da3f02c8e5bab1f"
264+ }"# ,
265+ None ,
266+ None ,
267+ None
268+ ; "no_limits"
269+ ) ]
270+ #[ test_case(
271+ r#"{
272+ "Cluster": "arn:aws:ecs:us-east-1:123456789012:cluster/profiler-metadata-cluster",
273+ "TaskARN": "arn:aws:ecs:us-east-1:123456789012:task/profiler-metadata-cluster/5261e761e0e2a3d92da3f02c8e5bab1f",
274+ "Limits": {}
275+ }"# ,
276+ Some ( FargateLimits { cpu: None , memory: None } ) ,
277+ None ,
278+ None
279+ ; "empty_limits"
280+ ) ]
281+ #[ test_case(
282+ r#"{
244283 "Cluster": "arn:aws:ecs:us-east-1:123456789012:cluster/profiler-metadata-cluster",
245284 "TaskARN": "arn:aws:ecs:us-east-1:123456789012:task/profiler-metadata-cluster/5261e761e0e2a3d92da3f02c8e5bab1f",
246285 "Family": "profiler-metadata",
@@ -316,17 +355,27 @@ mod tests {
316355 "Utilized": 208,
317356 "Reserved": 20496
318357 }
319- }
320- "# ;
321-
322- let fargate_metadata: FargateMetadata = serde_json:: from_str ( & json_str) . unwrap ( ) ;
358+ }"# ,
359+ Some ( FargateLimits { cpu: Some ( 0.25 . into( ) ) , memory: Some ( 2048 ) } ) ,
360+ Some ( 0.25 . into( ) ) ,
361+ Some ( 2048 )
362+ ; "with_limits"
363+ ) ]
364+ fn test_fargate_metadata (
365+ json_str : & str ,
366+ expected_limits : Option < FargateLimits > ,
367+ _expected_cpu_limit : Option < OrderedF64 > ,
368+ _expected_memory_limit : Option < u64 > ,
369+ ) {
370+ let fargate_metadata: FargateMetadata = serde_json:: from_str ( json_str) . unwrap ( ) ;
323371
324372 assert_eq ! (
325373 fargate_metadata,
326374 FargateMetadata {
327375 cluster: "arn:aws:ecs:us-east-1:123456789012:cluster/profiler-metadata-cluster"
328376 . to_owned( ) ,
329377 task_arn: "arn:aws:ecs:us-east-1:123456789012:task/profiler-metadata-cluster/5261e761e0e2a3d92da3f02c8e5bab1f" . to_owned( ) ,
378+ limits: expected_limits,
330379 }
331380 ) ;
332381
@@ -339,6 +388,10 @@ mod tests {
339388 aws_region_id: "us-east-1" . to_owned( ) ,
340389 ecs_task_arn: "arn:aws:ecs:us-east-1:123456789012:task/profiler-metadata-cluster/5261e761e0e2a3d92da3f02c8e5bab1f" . to_owned( ) ,
341390 ecs_cluster_arn: "arn:aws:ecs:us-east-1:123456789012:cluster/profiler-metadata-cluster" . to_owned( ) ,
391+ #[ cfg( feature = "__unstable-fargate-cpu-count" ) ]
392+ cpu_limit: _expected_cpu_limit,
393+ #[ cfg( feature = "__unstable-fargate-cpu-count" ) ]
394+ memory_limit: _expected_memory_limit,
342395 }
343396 )
344397 }
0 commit comments