aws-xray 0.31.0 → 0.32.0

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: ec204a6182574fe431e244db52a75dfdde4ff3d4
4
- data.tar.gz: 2cc7a2963eceb3eb50a2d36d0f41128df1d8b70c
3
+ metadata.gz: 0f836cc2b95778f08b4b4e751d4917dcf44fb042
4
+ data.tar.gz: fa4505884970bdd4016d4a0e5b3ed0dd0d515ee9
5
5
  SHA512:
6
- metadata.gz: e7c5c8e2ad818a848e0adabd9386a1c4a355896d364eacbc5ede702aad07ea4b2e4787eb737a0bc19b7b372f460be24681ba0ee4c805e5347530f402ffcbde51
7
- data.tar.gz: 4ad68d2b3e93166a27a766749ac90ad6289503462b6ccac2b67c62867de2184d29f3dd8d0b879f79c171aacc5c9d35c5283e3d2f1a69a198c557dd62f07374ab
6
+ metadata.gz: 38563eadc0beeeb77c701e6faf8256d8b7ca64f04184105101f38fdb767ba5bba936363e26c88536f69351d606b358b2dae94f654f6e408dfd2835f538aee095
7
+ data.tar.gz: fe472b43014a9207c3bcd93916a1d2d6e4f625208ccb6a9f5d43113e33d6c55930def7d744661ecda79dc70e2df22b4b93f35df1984fa4093d5b7770f3a206df
data/.gitignore CHANGED
@@ -8,3 +8,4 @@
8
8
  /spec/reports/
9
9
  /tmp/
10
10
  example/Gemfile.lock
11
+ /db/
data/aws-xray.gemspec CHANGED
@@ -22,6 +22,7 @@ Gem::Specification.new do |spec|
22
22
  spec.require_paths = ['lib']
23
23
 
24
24
  spec.add_dependency 'rack'
25
+ spec.add_development_dependency 'activerecord'
25
26
  spec.add_development_dependency 'bundler'
26
27
  spec.add_development_dependency 'coveralls'
27
28
  spec.add_development_dependency 'faraday'
@@ -33,5 +34,6 @@ Gem::Specification.new do |spec|
33
34
  spec.add_development_dependency 'rsolr'
34
35
  spec.add_development_dependency 'rspec'
35
36
  spec.add_development_dependency 'simplecov'
37
+ spec.add_development_dependency 'sqlite3'
36
38
  spec.add_development_dependency 'webmock'
37
39
  end
@@ -84,7 +84,7 @@ module Aws
84
84
  base_segment.set_error(fault: true, e: e)
85
85
  raise e
86
86
  ensure
87
- base_segment.finish
87
+ base_segment.finish unless base_segment.finished?
88
88
  Client.send_segment(base_segment) if @trace.sampled?
89
89
  end
90
90
  end
@@ -104,7 +104,7 @@ module Aws
104
104
  sub.set_error(fault: true, e: e)
105
105
  raise e
106
106
  ensure
107
- sub.finish
107
+ sub.finish unless sub.finished?
108
108
  Client.send_segment(sub) if @trace.sampled?
109
109
  end
110
110
  end
@@ -0,0 +1,90 @@
1
+ require 'aws/xray'
2
+ require 'active_record'
3
+
4
+ module Aws
5
+ module Xray
6
+ module Hooks
7
+ module ActiveRecord
8
+ extend self
9
+
10
+ IGNORE_NAMES = [nil, 'SCHEMA']
11
+
12
+ # event has #name, #time, #end, #duration, #payload
13
+ # payload has #sql, #name, #connection_id, #binds, #cached
14
+ #
15
+ # @param [ActiveSupport::Notifications::Event] e
16
+ def record(e)
17
+ return if IGNORE_NAMES.include?(e.payload[:name])
18
+
19
+ if !e.payload[:cached] && Aws::Xray::Context.started?
20
+ pool, con = fetch_connection_and_pool(e.payload[:connection_id])
21
+ return if pool.nil? || con.nil?
22
+ name = build_name(pool.spec)
23
+ return unless name # skip when connected to default host (typycally localhost)
24
+
25
+ Aws::Xray::Context.current.start_subsegment(name: name, remote: true) do |sub|
26
+ sub.start(e.time)
27
+ sub.finish(e.end)
28
+ sub.set_sql(Aws::Xray::Sql.build(
29
+ url: build_url(pool.spec),
30
+ database_version: build_version(con),
31
+ ))
32
+ end
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ def fetch_connection_and_pool(id)
39
+ pool, con = nil, nil
40
+ ::ActiveRecord::Base.connection_handler.connection_pool_list.each do |p|
41
+ p.connections.each do |c|
42
+ if c.object_id == id
43
+ pool, con = p, c
44
+ end
45
+ end
46
+ end
47
+ return pool, con
48
+ end
49
+
50
+ def build_name(spec)
51
+ spec.config.with_indifferent_access[:host]
52
+ end
53
+
54
+ def build_url(spec)
55
+ config = spec.config.with_indifferent_access
56
+ adapter = config.delete(:adapter) || 'unknown'
57
+ host = config.delete(:host) || 'localhost'
58
+ port = config.delete(:port) || ''
59
+ username = config.delete(:username) || ''
60
+ database = config.delete(:database) || ''
61
+ config.delete(:password)
62
+ query = config.map {|k, v| "#{k}=#{v}" }.join('&')
63
+ query_str = query.empty? ? '' : "?#{query}"
64
+ "#{adapter}://#{username}@#{host}:#{port}/#{database}#{query_str}"
65
+ end
66
+
67
+ def build_version(con)
68
+ if con.respond_to?(:version)
69
+ if (v = con.version.instance_variable_get('@version'))
70
+ v.join('.')
71
+ else
72
+ nil
73
+ end
74
+ else
75
+ nil
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
82
+
83
+ # maybe old version?
84
+ if defined?(ActiveSupport::Notifications) && ActiveSupport::Notifications.respond_to?(:subscribe)
85
+ ActiveSupport::Notifications.subscribe('sql.active_record') do |*args|
86
+ Aws::Xray::Hooks::ActiveRecord.record(ActiveSupport::Notifications::Event.new(*args))
87
+ end
88
+ else
89
+ $stderr.puts('Skip hooking active record events because this version of active record is not supported')
90
+ end
@@ -1,3 +1,4 @@
1
1
  require 'aws/xray'
2
+ require 'aws/xray/hooks/active_record'
2
3
  require 'aws/xray/hooks/net_http'
3
4
  require 'aws/xray/hooks/rsolr'
@@ -86,10 +86,18 @@ module Aws
86
86
  end
87
87
  alias_method :set_metadata, :add_metadata
88
88
 
89
+ def start(now = Time.now)
90
+ @start_time = now.to_f
91
+ end
92
+
89
93
  def finish(now = Time.now)
90
94
  @end_time = now.to_f
91
95
  end
92
96
 
97
+ def finished?
98
+ !!@end_time
99
+ end
100
+
93
101
  def to_json
94
102
  to_h.to_json
95
103
  end
@@ -127,12 +135,6 @@ module Aws
127
135
  h[:parent_id] = @parent_id if @parent_id
128
136
  h
129
137
  end
130
-
131
- private
132
-
133
- def start(now = Time.now)
134
- @start_time = now.to_f
135
- end
136
138
  end
137
139
  end
138
140
  end
@@ -0,0 +1,13 @@
1
+ module Aws
2
+ module Xray
3
+ class Sql < Struct.new(:url, :database_version)
4
+ def self.build(url: nil, database_version: nil)
5
+ new(url, database_version)
6
+ end
7
+
8
+ def to_h
9
+ super.delete_if {|_, v| v.nil? }
10
+ end
11
+ end
12
+ end
13
+ end
@@ -1,4 +1,5 @@
1
1
  require 'aws/xray/segment'
2
+ require 'aws/xray/sql'
2
3
 
3
4
  module Aws
4
5
  module Xray
@@ -15,6 +16,7 @@ module Aws
15
16
  super(name: name, trace_id: trace.root, parent_id: parent_id)
16
17
  @trace = trace
17
18
  @remote = !!remote
19
+ @sql = nil
18
20
  end
19
21
 
20
22
  # Set traced=false if the downstream call is not traced app.
@@ -25,6 +27,11 @@ module Aws
25
27
  @http_request.traced = traced
26
28
  end
27
29
 
30
+ # @param [Aws::Xray::Sql] sql
31
+ def set_sql(sql)
32
+ @sql = sql
33
+ end
34
+
28
35
  def to_h
29
36
  h = super
30
37
  # x_forwarded_for is Segment only.
@@ -34,6 +41,7 @@ module Aws
34
41
  end
35
42
  h[:type] = TYPE_NAME
36
43
  h[:namespace] = 'remote' if @remote
44
+ h[:sql] = @sql.to_h if @sql
37
45
  h
38
46
  end
39
47
 
@@ -1,5 +1,5 @@
1
1
  module Aws
2
2
  module Xray
3
- VERSION = '0.31.0'
3
+ VERSION = '0.32.0'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aws-xray
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.31.0
4
+ version: 0.32.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Taiki Ono
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-06-28 00:00:00.000000000 Z
11
+ date: 2017-06-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: bundler
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -178,6 +192,20 @@ dependencies:
178
192
  - - ">="
179
193
  - !ruby/object:Gem::Version
180
194
  version: '0'
195
+ - !ruby/object:Gem::Dependency
196
+ name: sqlite3
197
+ requirement: !ruby/object:Gem::Requirement
198
+ requirements:
199
+ - - ">="
200
+ - !ruby/object:Gem::Version
201
+ version: '0'
202
+ type: :development
203
+ prerelease: false
204
+ version_requirements: !ruby/object:Gem::Requirement
205
+ requirements:
206
+ - - ">="
207
+ - !ruby/object:Gem::Version
208
+ version: '0'
181
209
  - !ruby/object:Gem::Dependency
182
210
  name: webmock
183
211
  requirement: !ruby/object:Gem::Requirement
@@ -231,6 +259,7 @@ files:
231
259
  - lib/aws/xray/errors.rb
232
260
  - lib/aws/xray/faraday.rb
233
261
  - lib/aws/xray/header_parser.rb
262
+ - lib/aws/xray/hooks/active_record.rb
234
263
  - lib/aws/xray/hooks/all.rb
235
264
  - lib/aws/xray/hooks/net_http.rb
236
265
  - lib/aws/xray/hooks/rsolr.rb
@@ -240,6 +269,7 @@ files:
240
269
  - lib/aws/xray/response.rb
241
270
  - lib/aws/xray/segment.rb
242
271
  - lib/aws/xray/sockets.rb
272
+ - lib/aws/xray/sql.rb
243
273
  - lib/aws/xray/subsegment.rb
244
274
  - lib/aws/xray/trace.rb
245
275
  - lib/aws/xray/version.rb
@@ -266,7 +296,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
266
296
  version: '0'
267
297
  requirements: []
268
298
  rubyforge_project:
269
- rubygems_version: 2.6.11
299
+ rubygems_version: 2.5.2
270
300
  signing_key:
271
301
  specification_version: 4
272
302
  summary: The unofficial X-Ray Tracing SDK for Ruby.