Skip to content

Commit

Permalink
Backword compatible with prevous feature when not 1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
Etienne Chabert committed Sep 11, 2023
1 parent 3f8c871 commit 730ccb7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
18 changes: 8 additions & 10 deletions lib/aws-xray-sdk/plugins/ecs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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: {
Expand Down
10 changes: 8 additions & 2 deletions test/aws-xray-sdk/tc_plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 730ccb7

Please sign in to comment.