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