Skip to content

Commit

Permalink
Somewhat working ?
Browse files Browse the repository at this point in the history
  • Loading branch information
Etienne Chabert committed Sep 11, 2023
1 parent de3600b commit 3f8c871
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 75 deletions.
42 changes: 11 additions & 31 deletions lib/aws-xray-sdk/plugins/ecs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
71 changes: 27 additions & 44 deletions test/aws-xray-sdk/tc_plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand All @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 3f8c871

Please sign in to comment.