aws-xray-sdk 0.11.4 → 0.11.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b5ed0048518918a1d9894a88939b87fde9fdb313
4
- data.tar.gz: c998bf9b23dc468147c7846b6b50438c1da13b89
3
+ metadata.gz: 9c1ab8a4e8310389271dc8ea0fd7494f067f709d
4
+ data.tar.gz: ac7ef8e89a9123cb9d7e9f77ba63d663c55d56d6
5
5
  SHA512:
6
- metadata.gz: 942a1296ab40b2e3f5727edf7a685fb40e0254766f3c8b388de1a08cd0e67de361237d788245aff246139c6ce8d3eb8f83b584546dc3a180a9f945a181660f47
7
- data.tar.gz: 8f3a2c933526caf555116dbb2ea6d9b3f6056a22e42ab82ed2f5add6df82eab3e0b501f4bec02d97353d5e092e9c797dcd8a69b544202d6ea8c35d44dc994377
6
+ metadata.gz: a05a1d2a8c8a18df8503ac1943ff713c6e65a5991cba6790e66273e63b70748d726f8173ac2427d00808fdeb2519e63aa60ce8a13c22111652fa7b504d100c5c
7
+ data.tar.gz: 2e3f7eb44a7330d971dbbe562a6786d2aedb9a7319982c71c8cd7143c3ebe2d35c30a4dcdd5489e689ae7b26fe1dc6230a0dc82d8e727e9c5699f03378e73d39
@@ -30,9 +30,15 @@ module XRay
30
30
  req.path && (req.path == ('/GetSamplingRules') || req.path == ('/SamplingTargets'))
31
31
  end
32
32
 
33
+ # Instance Metadata Service provides endpoint 169.254.169.254 to
34
+ # provide EC2 metadata
35
+ def ec2_metadata_request?(req)
36
+ req.uri && req.uri.hostname == '169.254.169.254'
37
+ end
38
+
33
39
  def request(req, body = nil, &block)
34
- # Do not trace requests to xray or aws lambda runtime
35
- if xray_sampling_request?(req) || lambda_runtime_request?
40
+ # Do not trace requests to xray or aws lambda runtime or ec2 metadata endpoint
41
+ if xray_sampling_request?(req) || lambda_runtime_request? || ec2_metadata_request?(req)
36
42
  return super
37
43
  end
38
44
 
@@ -1,36 +1,91 @@
1
- require 'open-uri'
1
+ require 'net/http'
2
+ require 'json'
2
3
  require 'aws-xray-sdk/logger'
3
4
 
4
5
  module XRay
5
6
  module Plugins
6
- # A plugin that gets the EC2 instance-id and AZ if running on an EC2 instance.
7
+ # A plugin that gets the EC2 instance_id, availabiity_zone, instance_type, and ami_id if running on an EC2 instance.
7
8
  module EC2
8
9
  include Logging
9
10
 
10
11
  ORIGIN = 'AWS::EC2::Instance'.freeze
12
+
11
13
  # http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html#instancedata-data-retrieval
12
- ID_ADDR = 'http://169.254.169.254/latest/meta-data/instance-id'.freeze
13
- AZ_ADDR = 'http://169.254.169.254/latest/meta-data/placement/availability-zone'.freeze
14
+ METADATA_BASE_URL = 'http://169.254.169.254/latest'.freeze
14
15
 
15
16
  def self.aws
16
- @@aws ||= begin
17
- instance_id = open(ID_ADDR, open_timeout: 1).read
18
- az = open(AZ_ADDR, open_timeout: 1).read
19
- {
20
- ec2: {
21
- instance_id: instance_id,
22
- availability_zone: az
23
- }
17
+ @@aws = {}
18
+ token = get_token
19
+ ec2_metadata = get_metadata(token)
20
+ @@aws = {
21
+ ec2: ec2_metadata
22
+ }
23
+ end
24
+
25
+
26
+ private # private methods
27
+
28
+ def self.get_token
29
+ token_uri = URI(METADATA_BASE_URL + '/api/token')
30
+
31
+ req = Net::HTTP::Put.new(token_uri)
32
+ req['X-aws-ec2-metadata-token-ttl-seconds'] = '60'
33
+ begin
34
+ return do_request(req)
35
+ rescue StandardError => e
36
+ Logging.logger.warn %(can not get the IMDSv2 token due to: #{e.message}.)
37
+ ''
38
+ end
39
+ end
40
+
41
+ def self.get_metadata(token)
42
+ metadata_uri = URI(METADATA_BASE_URL + '/dynamic/instance-identity/document')
43
+
44
+ req = Net::HTTP::Get.new(metadata_uri)
45
+ if token != ''
46
+ req['X-aws-ec2-metadata-token'] = token
47
+ end
48
+
49
+ begin
50
+ metadata_json = do_request(req)
51
+ return parse_metadata(metadata_json)
52
+ rescue StandardError => e
53
+ Logging.logger.warn %(can not get the ec2 instance metadata due to: #{e.message}.)
54
+ {}
55
+ end
56
+ end
57
+
58
+ def self.parse_metadata(json_str)
59
+ metadata = {}
60
+ data = JSON(json_str)
61
+ metadata['instance_id'] = data['instanceId']
62
+ metadata['availability_zone'] = data['availabilityZone']
63
+ metadata['instance_type'] = data['instanceType']
64
+ metadata['ami_id'] = data['imageId']
65
+
66
+ metadata
67
+ end
68
+
69
+ def self.do_request(request)
70
+ begin
71
+ response = Net::HTTP.start(request.uri.hostname, read_timeout: 1) { |http|
72
+ http.request(request)
24
73
  }
74
+
75
+ if response.code == '200'
76
+ return response.body
77
+ else
78
+ raise(StandardError.new('Unsuccessful response::' + response.code + '::' + response.message))
79
+ end
25
80
  rescue StandardError => e
26
- # Two attempts in total to get EC2 metadata
81
+ # Two attempts in total to complete the request successfully
27
82
  @retries ||= 0
28
83
  if @retries < 1
29
84
  @retries += 1
30
85
  retry
31
86
  else
32
- @@aws = {}
33
- Logging.logger.warn %(can not get the ec2 instance metadata due to: #{e.message}.)
87
+ Logging.logger.warn %(Failed to complete request due to: #{e.message}.)
88
+ raise e
34
89
  end
35
90
  end
36
91
  end
@@ -1,3 +1,3 @@
1
1
  module XRay
2
- VERSION = '0.11.4'
2
+ VERSION = '0.11.5'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aws-xray-sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.4
4
+ version: 0.11.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Amazon Web Services
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-01 00:00:00.000000000 Z
11
+ date: 2020-06-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk-xray