From 8d428d5a3b90f3c7ae9b18a386e05ad54e4fde8c Mon Sep 17 00:00:00 2001 From: Etienne Chabert Date: Mon, 11 Sep 2023 10:09:30 +0200 Subject: [PATCH 01/11] First step --- lib/aws-xray-sdk/recorder.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/aws-xray-sdk/recorder.rb b/lib/aws-xray-sdk/recorder.rb index b4a8e33..e911723 100644 --- a/lib/aws-xray-sdk/recorder.rb +++ b/lib/aws-xray-sdk/recorder.rb @@ -222,6 +222,13 @@ def populate_runtime_context(segment, sample) } } aws.merge! xray_meta + cloudwatch_meta = { + cloudwatch_logs: { + log_group: "???" + arn: "???" + } + } + aws.merge! cloudwatch_meta end @service ||= { From 170a226b66af8dfab6dbb10d10e86253a3f410b6 Mon Sep 17 00:00:00 2001 From: Etienne Chabert Date: Mon, 11 Sep 2023 10:10:44 +0200 Subject: [PATCH 02/11] Add comment --- lib/aws-xray-sdk/recorder.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/aws-xray-sdk/recorder.rb b/lib/aws-xray-sdk/recorder.rb index e911723..0d22556 100644 --- a/lib/aws-xray-sdk/recorder.rb +++ b/lib/aws-xray-sdk/recorder.rb @@ -224,6 +224,7 @@ def populate_runtime_context(segment, sample) aws.merge! xray_meta cloudwatch_meta = { cloudwatch_logs: { + # Help me: Are the log_group and arn for already available somewhere in the SDK ? log_group: "???" arn: "???" } From 318026c1606afbe20b8c3dac43614adb242dcd26 Mon Sep 17 00:00:00 2001 From: Etienne Chabert Date: Mon, 11 Sep 2023 10:12:55 +0200 Subject: [PATCH 03/11] Typo --- lib/aws-xray-sdk/recorder.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/aws-xray-sdk/recorder.rb b/lib/aws-xray-sdk/recorder.rb index 0d22556..97b9d3d 100644 --- a/lib/aws-xray-sdk/recorder.rb +++ b/lib/aws-xray-sdk/recorder.rb @@ -225,7 +225,7 @@ def populate_runtime_context(segment, sample) cloudwatch_meta = { cloudwatch_logs: { # Help me: Are the log_group and arn for already available somewhere in the SDK ? - log_group: "???" + log_group: "???", arn: "???" } } From 3b970f093b3b18043d6c7229d065be5f362333fc Mon Sep 17 00:00:00 2001 From: Etienne Chabert Date: Mon, 11 Sep 2023 10:41:14 +0200 Subject: [PATCH 04/11] Copy logic of EC2 plugin for ECS --- lib/aws-xray-sdk/plugins/ecs.rb | 91 ++++++++++++++++++++++++++++++++- lib/aws-xray-sdk/recorder.rb | 8 --- 2 files changed, 89 insertions(+), 10 deletions(-) diff --git a/lib/aws-xray-sdk/plugins/ecs.rb b/lib/aws-xray-sdk/plugins/ecs.rb index b511b90..188db1b 100644 --- a/lib/aws-xray-sdk/plugins/ecs.rb +++ b/lib/aws-xray-sdk/plugins/ecs.rb @@ -10,13 +10,100 @@ module ECS ORIGIN = 'AWS::ECS::Container'.freeze + METADATA_BASE_URL = 'http://169.254.169.254/latest'.freeze + def self.aws - @@aws ||= begin - { ecs: { container: Socket.gethostname } } + @@aws = {} + token = get_token + metadata = get_metadata(token) + + @@aws = { + { ecs: { container: metadata[:ecs] } }, + cloudwatch_logs: metadata[:cloudwatch_logs] + } + end + + private + + def self.get_token + token_uri = URI(METADATA_BASE_URL + '/api/token') + + req = Net::HTTP::Put.new(token_uri) + req['X-aws-ec2-metadata-token-ttl-seconds'] = '60' + begin + return do_request(req) + rescue StandardError => e + Logging.logger.warn %(cannot get the IMDSv2 token due to: #{e.message}.) + '' + end + end + + def self.get_metadata(token) + metadata_uri = URI(METADATA_BASE_URL + '/dynamic/instance-identity/document') + + req = Net::HTTP::Get.new(metadata_uri) + if token != '' + req['X-aws-ec2-metadata-token'] = token + end + + begin + metadata_json = do_request(req) + return parse_metadata(metadata_json) + rescue StandardError => e + Logging.logger.warn %(cannot get the ec2 instance metadata due to: #{e.message}.) + {} + end + end + + def self.parse_metadata(json_str) + data = JSON(json_str) + + begin + container_hostname = Socket.gethostname rescue StandardError => e @@aws = {} Logging.logger.warn %(cannot get the ecs container hostname due to: #{e.message}.) end + + metadata = { + 'ecs': { + 'container': container_hostname, + 'container_arn': data['ContainerARN'], + }, + 'cloudwatch_logs': { + 'log_driver': data['LogDriver'], + 'log_option': data['LogOptions'], + 'log_group': data['awslogs-group'], + 'log_region': data['awslogs-region'], + 'arn': data['ContainerARN'] + } + } + + metadata + end + + def self.do_request(request) + begin + response = Net::HTTP.start(request.uri.hostname, read_timeout: 1) { |http| + http.request(request) + } + + if response.code == '200' + return response.body + else + raise(StandardError.new('Unsuccessful response::' + response.code + '::' + response.message)) + end + rescue StandardError => e + # Two attempts in total to complete the request successfully + @retries ||= 0 + if @retries < 1 + @retries += 1 + retry + else + Logging.logger.warn %(Failed to complete request due to: #{e.message}.) + raise e + end + end end end end diff --git a/lib/aws-xray-sdk/recorder.rb b/lib/aws-xray-sdk/recorder.rb index 97b9d3d..b4a8e33 100644 --- a/lib/aws-xray-sdk/recorder.rb +++ b/lib/aws-xray-sdk/recorder.rb @@ -222,14 +222,6 @@ def populate_runtime_context(segment, sample) } } aws.merge! xray_meta - cloudwatch_meta = { - cloudwatch_logs: { - # Help me: Are the log_group and arn for already available somewhere in the SDK ? - log_group: "???", - arn: "???" - } - } - aws.merge! cloudwatch_meta end @service ||= { From 64159e199e4c157707c44efaa770037ef9749f37 Mon Sep 17 00:00:00 2001 From: Etienne Chabert Date: Mon, 11 Sep 2023 10:46:06 +0200 Subject: [PATCH 05/11] Typo --- lib/aws-xray-sdk/plugins/ecs.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/aws-xray-sdk/plugins/ecs.rb b/lib/aws-xray-sdk/plugins/ecs.rb index 188db1b..05c2c9e 100644 --- a/lib/aws-xray-sdk/plugins/ecs.rb +++ b/lib/aws-xray-sdk/plugins/ecs.rb @@ -18,7 +18,7 @@ def self.aws metadata = get_metadata(token) @@aws = { - { ecs: { container: metadata[:ecs] } }, + ecs: { container: metadata[:ecs] }, cloudwatch_logs: metadata[:cloudwatch_logs] } end From 3064c60a8044ecf228ef53a8668b21a30d6466e7 Mon Sep 17 00:00:00 2001 From: Etienne Chabert Date: Mon, 11 Sep 2023 10:48:10 +0200 Subject: [PATCH 06/11] Back to symbols --- lib/aws-xray-sdk/plugins/ecs.rb | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/aws-xray-sdk/plugins/ecs.rb b/lib/aws-xray-sdk/plugins/ecs.rb index 05c2c9e..c17ecf4 100644 --- a/lib/aws-xray-sdk/plugins/ecs.rb +++ b/lib/aws-xray-sdk/plugins/ecs.rb @@ -66,16 +66,16 @@ def self.parse_metadata(json_str) end metadata = { - 'ecs': { - 'container': container_hostname, - 'container_arn': data['ContainerARN'], + ecs: { + container: container_hostname, + container_arn: data['ContainerARN'], }, - 'cloudwatch_logs': { - 'log_driver': data['LogDriver'], - 'log_option': data['LogOptions'], - 'log_group': data['awslogs-group'], - 'log_region': data['awslogs-region'], - 'arn': data['ContainerARN'] + cloudwatch_logs: { + log_driver: data['LogDriver'], + log_option: data['LogOptions'], + log_group: data['awslogs-group'], + log_region: data['awslogs-region'], + arn: data['ContainerARN'] } } From 74037ec5b049edaa849f8825328ffc9842866d71 Mon Sep 17 00:00:00 2001 From: Etienne Chabert Date: Mon, 11 Sep 2023 10:53:22 +0200 Subject: [PATCH 07/11] Copy paste all EC2 tests as starting point for ECS --- test/aws-xray-sdk/tc_plugin.rb | 75 ++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/test/aws-xray-sdk/tc_plugin.rb b/test/aws-xray-sdk/tc_plugin.rb index a9becb3..ccc3d57 100644 --- a/test/aws-xray-sdk/tc_plugin.rb +++ b/test/aws-xray-sdk/tc_plugin.rb @@ -20,6 +20,10 @@ def test_get_runtime_context .to_raise(StandardError) stub_request(:any, XRay::Plugins::EC2::METADATA_BASE_URL + '/dynamic/instance-identity/document') .to_raise(StandardError) + stub_request(:any, XRay::Plugins::ECS::METADATA_BASE_URL + '/api/token') + .to_raise(StandardError) + stub_request(:any, XRay::Plugins::ECS::METADATA_BASE_URL + '/dynamic/instance-identity/document') + .to_raise(StandardError) XRay::Plugins::EC2.aws XRay::Plugins::ECS.aws WebMock.reset! @@ -80,4 +84,75 @@ def test_ec2_metadata_fail assert expected, XRay::Plugins::EC2.aws WebMock.reset! end + + def test_get_runtime_context + XRay::Plugins::ElasticBeanstalk.aws + stub_request(:any, XRay::Plugins::EC2::METADATA_BASE_URL + '/api/token') + .to_raise(StandardError) + stub_request(:any, XRay::Plugins::EC2::METADATA_BASE_URL + '/dynamic/instance-identity/document') + .to_raise(StandardError) + stub_request(:any, XRay::Plugins::ECS::METADATA_BASE_URL + '/api/token') + .to_raise(StandardError) + stub_request(:any, XRay::Plugins::ECS::METADATA_BASE_URL + '/dynamic/instance-identity/document') + .to_raise(StandardError) + XRay::Plugins::EC2.aws + XRay::Plugins::ECS.aws + WebMock.reset! + end + + def test_ecs_metadata_v2_successful + dummy_json = '{\"availabilityZone\" : \"us-east-2a\", \"imageId\" : \"ami-03cca83dd001d4666\", + \"instanceId\" : \"i-07a181803de94c666\", \"instanceType\" : \"t3.xlarge\"}' + + stub_request(:put, XRay::Plugins::ECS::METADATA_BASE_URL + '/api/token') + .to_return(status: 200, body: 'some_token', headers: {}) + + stub_request(:get, XRay::Plugins::ECS::METADATA_BASE_URL + '/dynamic/instance-identity/document') + .to_return(status: 200, body: dummy_json, headers: {}) + + expected = { + ec2: { + instance_id: 'i-07a181803de94c666', + availability_zone: 'us-east-2a', + instance_type: 't3.xlarge', + ami_id: 'ami-03cca83dd001d4666' + } + } + assert expected, XRay::Plugins::ECS.aws + WebMock.reset! + end + + def test_ecs_metadata_v1_successful + dummy_json = '{\"availabilityZone\" : \"cn-north-1a\", \"imageId\" : \"ami-03cca83dd001d4111\", + \"instanceId\" : \"i-07a181803de94c111\", \"instanceType\" : \"t2.xlarge\"}' + + stub_request(:put, XRay::Plugins::ECS::METADATA_BASE_URL + '/api/token') + .to_raise(StandardError) + + stub_request(:get, XRay::Plugins::ECS::METADATA_BASE_URL + '/dynamic/instance-identity/document') + .to_return(status: 200, body: dummy_json, headers: {}) + + expected = { + ec2: { + instance_id: 'i-07a181803de94c111', + availability_zone: 'cn-north-1a', + instance_type: 't2.xlarge', + ami_id: 'ami-03cca83dd001d4111' + } + } + assert expected, XRay::Plugins::ECS.aws + WebMock.reset! + end + + def test_ecs_metadata_fail + stub_request(:put, XRay::Plugins::ECS::METADATA_BASE_URL + '/api/token') + .to_raise(StandardError) + + stub_request(:get, XRay::Plugins::ECS::METADATA_BASE_URL + '/dynamic/instance-identity/document') + .to_raise(StandardError) + + expected = {} + assert expected, XRay::Plugins::ECS.aws + WebMock.reset! + end end From de3600b45640fa6b99ec5b29ff3ca5c0448a6484 Mon Sep 17 00:00:00 2001 From: Etienne Chabert Date: Mon, 11 Sep 2023 10:54:46 +0200 Subject: [PATCH 08/11] Comments --- test/aws-xray-sdk/tc_plugin.rb | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/test/aws-xray-sdk/tc_plugin.rb b/test/aws-xray-sdk/tc_plugin.rb index ccc3d57..da2b578 100644 --- a/test/aws-xray-sdk/tc_plugin.rb +++ b/test/aws-xray-sdk/tc_plugin.rb @@ -29,6 +29,7 @@ def test_get_runtime_context WebMock.reset! end + # EC2 Plugin def test_ec2_metadata_v2_successful dummy_json = '{\"availabilityZone\" : \"us-east-2a\", \"imageId\" : \"ami-03cca83dd001d4666\", \"instanceId\" : \"i-07a181803de94c666\", \"instanceType\" : \"t3.xlarge\"}' @@ -85,21 +86,7 @@ def test_ec2_metadata_fail WebMock.reset! end - def test_get_runtime_context - XRay::Plugins::ElasticBeanstalk.aws - stub_request(:any, XRay::Plugins::EC2::METADATA_BASE_URL + '/api/token') - .to_raise(StandardError) - stub_request(:any, XRay::Plugins::EC2::METADATA_BASE_URL + '/dynamic/instance-identity/document') - .to_raise(StandardError) - stub_request(:any, XRay::Plugins::ECS::METADATA_BASE_URL + '/api/token') - .to_raise(StandardError) - stub_request(:any, XRay::Plugins::ECS::METADATA_BASE_URL + '/dynamic/instance-identity/document') - .to_raise(StandardError) - XRay::Plugins::EC2.aws - XRay::Plugins::ECS.aws - WebMock.reset! - end - + # ECS Plugin def test_ecs_metadata_v2_successful dummy_json = '{\"availabilityZone\" : \"us-east-2a\", \"imageId\" : \"ami-03cca83dd001d4666\", \"instanceId\" : \"i-07a181803de94c666\", \"instanceType\" : \"t3.xlarge\"}' @@ -143,7 +130,7 @@ def test_ecs_metadata_v1_successful assert expected, XRay::Plugins::ECS.aws WebMock.reset! end - + def test_ecs_metadata_fail stub_request(:put, XRay::Plugins::ECS::METADATA_BASE_URL + '/api/token') .to_raise(StandardError) From 3f8c87181eab17bd688a5277e7f50e6cba78d6cd Mon Sep 17 00:00:00 2001 From: Etienne Chabert Date: Mon, 11 Sep 2023 12:29:20 +0200 Subject: [PATCH 09/11] Somewhat working ? --- lib/aws-xray-sdk/plugins/ecs.rb | 42 +++++-------------- test/aws-xray-sdk/tc_plugin.rb | 71 +++++++++++++-------------------- 2 files changed, 38 insertions(+), 75 deletions(-) diff --git a/lib/aws-xray-sdk/plugins/ecs.rb b/lib/aws-xray-sdk/plugins/ecs.rb index c17ecf4..07fff18 100644 --- a/lib/aws-xray-sdk/plugins/ecs.rb +++ b/lib/aws-xray-sdk/plugins/ecs.rb @@ -10,47 +10,30 @@ module ECS ORIGIN = 'AWS::ECS::Container'.freeze - METADATA_BASE_URL = 'http://169.254.169.254/latest'.freeze + # Only compatible with v4! + # The v3 metadata url does not contain cloudwatch informations + METADATA_ENV_KEY = 'ECS_CONTAINER_METADATA_URI_V4' def self.aws @@aws = {} - token = get_token - metadata = get_metadata(token) + metadata = get_metadata() @@aws = { - ecs: { container: metadata[:ecs] }, + ecs: metadata[:ecs], cloudwatch_logs: metadata[:cloudwatch_logs] } end private - def self.get_token - token_uri = URI(METADATA_BASE_URL + '/api/token') - - req = Net::HTTP::Put.new(token_uri) - req['X-aws-ec2-metadata-token-ttl-seconds'] = '60' - begin - return do_request(req) - rescue StandardError => e - Logging.logger.warn %(cannot get the IMDSv2 token due to: #{e.message}.) - '' - end - end - - def self.get_metadata(token) - metadata_uri = URI(METADATA_BASE_URL + '/dynamic/instance-identity/document') - - req = Net::HTTP::Get.new(metadata_uri) - if token != '' - req['X-aws-ec2-metadata-token'] = token - end - + def self.get_metadata() begin + metadata_uri = URI(ENV[METADATA_ENV_KEY]) + req = Net::HTTP::Get.new(metadata_uri) metadata_json = do_request(req) return parse_metadata(metadata_json) rescue StandardError => e - Logging.logger.warn %(cannot get the ec2 instance metadata due to: #{e.message}.) + Logging.logger.warn %(cannot get the ecs instance metadata due to: #{e.message}.) {} end end @@ -71,14 +54,11 @@ def self.parse_metadata(json_str) container_arn: data['ContainerARN'], }, cloudwatch_logs: { - log_driver: data['LogDriver'], - log_option: data['LogOptions'], - log_group: data['awslogs-group'], - log_region: data['awslogs-region'], + log_group: data["LogOptions"]['awslogs-group'], + log_region: data["LogOptions"]['awslogs-region'], arn: data['ContainerARN'] } } - metadata end diff --git a/test/aws-xray-sdk/tc_plugin.rb b/test/aws-xray-sdk/tc_plugin.rb index da2b578..aa530ec 100644 --- a/test/aws-xray-sdk/tc_plugin.rb +++ b/test/aws-xray-sdk/tc_plugin.rb @@ -20,10 +20,6 @@ def test_get_runtime_context .to_raise(StandardError) stub_request(:any, XRay::Plugins::EC2::METADATA_BASE_URL + '/dynamic/instance-identity/document') .to_raise(StandardError) - stub_request(:any, XRay::Plugins::ECS::METADATA_BASE_URL + '/api/token') - .to_raise(StandardError) - stub_request(:any, XRay::Plugins::ECS::METADATA_BASE_URL + '/dynamic/instance-identity/document') - .to_raise(StandardError) XRay::Plugins::EC2.aws XRay::Plugins::ECS.aws WebMock.reset! @@ -48,6 +44,7 @@ def test_ec2_metadata_v2_successful ami_id: 'ami-03cca83dd001d4666' } } + # We should probably use `assert_equal` here ? Always true otherwise... assert expected, XRay::Plugins::EC2.aws WebMock.reset! end @@ -87,59 +84,45 @@ def test_ec2_metadata_fail end # ECS Plugin - def test_ecs_metadata_v2_successful - dummy_json = '{\"availabilityZone\" : \"us-east-2a\", \"imageId\" : \"ami-03cca83dd001d4666\", - \"instanceId\" : \"i-07a181803de94c666\", \"instanceType\" : \"t3.xlarge\"}' - - stub_request(:put, XRay::Plugins::ECS::METADATA_BASE_URL + '/api/token') - .to_return(status: 200, body: 'some_token', headers: {}) + def test_ecs_metadata_successful + dummy_metadata_uri = 'http://169.254.170.2/v4/a_random_id' + dummy_json = { + "ContainerARN"=>"arn:aws:ecs:eu-central-1:an_id:container/a_cluster/a_cluster_id/a_task_id", + "LogOptions"=>{"awslogs-group"=>"/ecs/a_service_name", "awslogs-region"=>"eu-central-1", "awslogs-stream"=>"ecs/a_service_name/a_task_id"}, + } - stub_request(:get, XRay::Plugins::ECS::METADATA_BASE_URL + '/dynamic/instance-identity/document') - .to_return(status: 200, body: dummy_json, headers: {}) + ENV[XRay::Plugins::ECS::METADATA_ENV_KEY] = dummy_metadata_uri + stub_request(:get, dummy_metadata_uri) + .to_return(status: 200, body: dummy_json.to_json, headers: {}) expected = { - ec2: { - instance_id: 'i-07a181803de94c666', - availability_zone: 'us-east-2a', - instance_type: 't3.xlarge', - ami_id: 'ami-03cca83dd001d4666' - } + ecs: { + container: Socket.gethostname, + container_arn: 'arn:aws:ecs:eu-central-1:an_id:container/a_cluster/a_cluster_id/a_task_id', + }, + cloudwatch_logs: {:log_group=>"/ecs/a_service_name", :log_region=>"eu-central-1", :arn=>"arn:aws:ecs:eu-central-1:an_id:container/a_cluster/a_cluster_id/a_task_id"} } - assert expected, XRay::Plugins::ECS.aws + assert_equal expected, XRay::Plugins::ECS.aws WebMock.reset! + ENV.delete(XRay::Plugins::ECS::METADATA_ENV_KEY) end - def test_ecs_metadata_v1_successful - dummy_json = '{\"availabilityZone\" : \"cn-north-1a\", \"imageId\" : \"ami-03cca83dd001d4111\", - \"instanceId\" : \"i-07a181803de94c111\", \"instanceType\" : \"t2.xlarge\"}' + def test_ecs_metadata_fail + dummy_metadata_uri = 'http://169.254.170.2/v4/a_random_id' + ENV['ECS_CONTAINER_METADATA_URI_V4'] = dummy_metadata_uri - stub_request(:put, XRay::Plugins::ECS::METADATA_BASE_URL + '/api/token') + stub_request(:get, dummy_metadata_uri) .to_raise(StandardError) - stub_request(:get, XRay::Plugins::ECS::METADATA_BASE_URL + '/dynamic/instance-identity/document') - .to_return(status: 200, body: dummy_json, headers: {}) - - expected = { - ec2: { - instance_id: 'i-07a181803de94c111', - availability_zone: 'cn-north-1a', - instance_type: 't2.xlarge', - ami_id: 'ami-03cca83dd001d4111' - } - } - assert expected, XRay::Plugins::ECS.aws + expected = {:ecs=>nil, :cloudwatch_logs=>nil} + assert_equal expected, XRay::Plugins::ECS.aws WebMock.reset! + ENV.delete(XRay::Plugins::ECS::METADATA_ENV_KEY) end - def test_ecs_metadata_fail - stub_request(:put, XRay::Plugins::ECS::METADATA_BASE_URL + '/api/token') - .to_raise(StandardError) - - stub_request(:get, XRay::Plugins::ECS::METADATA_BASE_URL + '/dynamic/instance-identity/document') - .to_raise(StandardError) - - expected = {} - assert expected, XRay::Plugins::ECS.aws + def test_ecs_metadata_not_defined + expected = {:ecs=>nil, :cloudwatch_logs=>nil} + assert_equal expected, XRay::Plugins::ECS.aws WebMock.reset! end end From 730ccb7e727ecb26b68ee6d049d56b323b060bd8 Mon Sep 17 00:00:00 2001 From: Etienne Chabert Date: Mon, 11 Sep 2023 12:47:42 +0200 Subject: [PATCH 10/11] Backword compatible with prevous feature when not 1.4 --- lib/aws-xray-sdk/plugins/ecs.rb | 18 ++++++++---------- test/aws-xray-sdk/tc_plugin.rb | 10 ++++++++-- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/lib/aws-xray-sdk/plugins/ecs.rb b/lib/aws-xray-sdk/plugins/ecs.rb index 07fff18..f78cb5e 100644 --- a/lib/aws-xray-sdk/plugins/ecs.rb +++ b/lib/aws-xray-sdk/plugins/ecs.rb @@ -15,9 +15,15 @@ module ECS METADATA_ENV_KEY = 'ECS_CONTAINER_METADATA_URI_V4' def self.aws - @@aws = {} metadata = get_metadata() + begin + metadata[:ecs][:container] = Socket.gethostname + rescue StandardError => e + Logging.logger.warn %(cannot get the ecs container hostname due to: #{e.message}.) + metadata[:ecs][:container] = nil + end + @@aws = { ecs: metadata[:ecs], cloudwatch_logs: metadata[:cloudwatch_logs] @@ -34,23 +40,15 @@ def self.get_metadata() return parse_metadata(metadata_json) rescue StandardError => e Logging.logger.warn %(cannot get the ecs instance metadata due to: #{e.message}.) - {} + { ecs: {}, cloudwatch_logs: {} } end end def self.parse_metadata(json_str) data = JSON(json_str) - begin - container_hostname = Socket.gethostname - rescue StandardError => e - @@aws = {} - Logging.logger.warn %(cannot get the ecs container hostname due to: #{e.message}.) - end - metadata = { ecs: { - container: container_hostname, container_arn: data['ContainerARN'], }, cloudwatch_logs: { diff --git a/test/aws-xray-sdk/tc_plugin.rb b/test/aws-xray-sdk/tc_plugin.rb index aa530ec..bbb6ad2 100644 --- a/test/aws-xray-sdk/tc_plugin.rb +++ b/test/aws-xray-sdk/tc_plugin.rb @@ -114,14 +114,20 @@ def test_ecs_metadata_fail stub_request(:get, dummy_metadata_uri) .to_raise(StandardError) - expected = {:ecs=>nil, :cloudwatch_logs=>nil} + expected = { + ecs: {container: Socket.gethostname}, + cloudwatch_logs: {} + } assert_equal expected, XRay::Plugins::ECS.aws WebMock.reset! ENV.delete(XRay::Plugins::ECS::METADATA_ENV_KEY) end def test_ecs_metadata_not_defined - expected = {:ecs=>nil, :cloudwatch_logs=>nil} + expected = { + ecs: {container: Socket.gethostname}, + cloudwatch_logs: {} + } assert_equal expected, XRay::Plugins::ECS.aws WebMock.reset! end From b252f35f95942c4355f0850dd128ab3a4cd59059 Mon Sep 17 00:00:00 2001 From: Etienne Chabert Date: Mon, 11 Sep 2023 13:10:26 +0200 Subject: [PATCH 11/11] Add hint for fargate 1.4 in case of errors --- lib/aws-xray-sdk/plugins/ecs.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/aws-xray-sdk/plugins/ecs.rb b/lib/aws-xray-sdk/plugins/ecs.rb index f78cb5e..bfe25fe 100644 --- a/lib/aws-xray-sdk/plugins/ecs.rb +++ b/lib/aws-xray-sdk/plugins/ecs.rb @@ -3,8 +3,6 @@ module XRay module Plugins - # Due to lack of ECS container metadata service, the only host information - # available is the host name. module ECS include Logging @@ -39,7 +37,7 @@ def self.get_metadata() metadata_json = do_request(req) return parse_metadata(metadata_json) rescue StandardError => e - Logging.logger.warn %(cannot get the ecs instance metadata due to: #{e.message}.) + Logging.logger.warn %(cannot get the ecs instance metadata due to: #{e.message}. Make sure you are using Fargate platform version >=1.4.0) { ecs: {}, cloudwatch_logs: {} } end end