aws-xray 0.3.0 → 0.4.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: 7205283919522f3fa51c16bc8e5f11136ba04a9c
4
- data.tar.gz: 0fa4f411671929e4c436939d190afa7fd48e181e
3
+ metadata.gz: 2caae0694ce40c2e40f13c9ef137f20fe74afad1
4
+ data.tar.gz: 4d1063ecbb78d4ba27c798d6c5017268097e7bf8
5
5
  SHA512:
6
- metadata.gz: 9c4aea978c59e664b39d10ca7df5c57c8537069fff4df2196b7e3213fa71c990a66e05cd6e0c816a95311d03e9ace2614ecddcad036e5b04c892f78ca51b1e1e
7
- data.tar.gz: 6519acd350a5c0a6ceddad31bdd54db472466cf35bc4d5e044404e4290475607296e9b820c64e40d1099307b715fb927ccf53c5a029b249f69531ee2cacf9a4d
6
+ metadata.gz: 72eccf23076cbfbd0d6254b3bcf36bb3b97d438024fc469193226dbabe550c3d4855a89fa4d3fac1e1a41a03d70f92c069d75354918bef1ff09ebb0a676aaa71
7
+ data.tar.gz: 7e7eab73da12c373b836b2f3811dc2574c6399033496480986f6b1e0d0faad04dca0869015851a46cd3508a4a23a3ef5f63822a95b4151b15fee76cbe7e4a9b5
data/README.md CHANGED
@@ -37,14 +37,12 @@ Aws::Xray.config.name = 'my-app'
37
37
  Rails.application.config.middleware.use Aws::Xray::Rack
38
38
  ```
39
39
 
40
- Or just require `aws/xray/rails`:
40
+ Or just require `aws/xray/rails`. It uses your application name by default.
41
+ e.g. `Legacy::MyBlog` -> `legacy-my-blog`.
41
42
 
42
43
  ```ruby
43
44
  # Gemfile
44
45
  gem 'aws-xray', require: 'aws/xray/rails'
45
-
46
- # config/initializers/aws_xray.rb
47
- Aws::Xray.config.name = 'my-app'
48
46
  ```
49
47
 
50
48
  To trace out-going HTTP requests, see below.
@@ -90,13 +88,11 @@ client = Faraday.new('...') do |builder|
90
88
  end
91
89
 
92
90
  # Start new tracing context then perform arbitrary actions in the block.
93
- Aws::Xray::Context.with_new_context('test-app', xray_client, trace_header) do
94
- Aws::Xray::Context.current.base_trace do
95
- client.get('/foo')
91
+ Aws::Xray.trace do |seg|
92
+ client.get('/foo')
96
93
 
97
- Aws::Xray::Context.current.child_trace do |sub|
98
- # DB access or something to trace.
99
- end
94
+ Aws::Xray::Context.current.child_trace do |sub|
95
+ # DB access or something to trace.
100
96
  end
101
97
  end
102
98
  ```
@@ -1,7 +1,8 @@
1
1
  require 'pry'
2
2
  require 'aws-xray'
3
3
 
4
- use Aws::Xray::Rack, name: 'campain-app'
4
+ Aws::Xray.config.name = 'campain-app'
5
+ use Aws::Xray::Rack
5
6
 
6
7
  class DbConnectionError < StandardError
7
8
  def initialize
@@ -4,7 +4,8 @@ require 'aws-xray'
4
4
 
5
5
  recipe_app = ENV.fetch('RECIPE_APP') # host:port
6
6
 
7
- use Aws::Xray::Rack, name: 'front-app'
7
+ Aws::Xray.config.name = 'front-app'
8
+ use Aws::Xray::Rack
8
9
 
9
10
  run Proc.new {|env|
10
11
  headers = { 'Host' => 'recipe-app' }
@@ -5,7 +5,8 @@ require 'aws-xray'
5
5
  user_app = ENV.fetch('USER_APP') # host:port
6
6
  campain_app = ENV.fetch('CAMPAIN_APP') # host:port
7
7
 
8
- use Aws::Xray::Rack, name: 'recipe-app'
8
+ Aws::Xray.config.name = 'recipe-app'
9
+ use Aws::Xray::Rack
9
10
 
10
11
  run Proc.new {|env|
11
12
  user_client = Faraday.new(url: "http://#{user_app}/", headers: { 'Host' => 'user-app' }) do |builder|
@@ -1,7 +1,8 @@
1
1
  require 'pry'
2
2
  require 'aws-xray'
3
3
 
4
- use Aws::Xray::Rack, name: 'user-app'
4
+ Aws::Xray.config.name = 'user-app'
5
+ use Aws::Xray::Rack
5
6
 
6
7
  run Proc.new {|env|
7
8
  ['200', {'Content-Type' => 'text/plain'}, ['user-1']]
data/lib/aws/xray.rb CHANGED
@@ -7,9 +7,28 @@ module Aws
7
7
  module Xray
8
8
  TRACE_HEADER = 'X-Amzn-Trace-Id'.freeze
9
9
 
10
+ class MissingNameError < ::StandardError
11
+ def initialize
12
+ super("`name` is empty. Configure this with `Aws::Xray.config.name = 'my-app'`.")
13
+ end
14
+ end
15
+
10
16
  @config = Configuration.new
11
17
  class << self
12
18
  attr_reader :config
13
19
  end
20
+
21
+ # @param [String] name a logical name of this tracing context.
22
+ def self.trace(name: nil)
23
+ name = name || config.name || raise(MissingNameError)
24
+ client = Client.new(Aws::Xray.config.client_options)
25
+ Context.with_new_context(name, client, Trace.generate) do
26
+ Context.current.base_trace do |seg|
27
+ yield seg
28
+ end
29
+ end
30
+ ensure
31
+ client.close
32
+ end
14
33
  end
15
34
  end
@@ -21,6 +21,10 @@ module Aws
21
21
  $stderr.puts("Can not send all bytes: #{len} sent")
22
22
  end
23
23
  end
24
+
25
+ def close
26
+ @sock.close
27
+ end
24
28
  end
25
29
  end
26
30
  end
@@ -13,12 +13,22 @@ module Aws
13
13
  end
14
14
  end
15
15
 
16
+ # @return [Aws::Xray::Context]
16
17
  def current
17
18
  Thread.current.thread_variable_get(VAR_NAME) || raise(NotSetError)
18
19
  end
19
20
 
20
- def with_new_context(name, client, trace_header)
21
- build_current(name, client, trace_header)
21
+ # @param [String] name logical name of this tracing context.
22
+ # @param [Aws::Xray::Client] client Require this parameter because the
23
+ # socket inside client can live longer than this context. For example
24
+ # the life-cycle of context is HTTP request based but the socket can
25
+ # live over HTTP requests cycle, it is opened when application starts
26
+ # then is closed when application exits.
27
+ # @param [Aws::Xray::Trace] trace newly generated trace or created with
28
+ # HTTP request header.
29
+ # @yield [Aws::Xray::Context] newly created context.
30
+ def with_new_context(name, client, trace)
31
+ build_current(name, client, trace)
22
32
  yield
23
33
  ensure
24
34
  remove_current
@@ -26,8 +36,8 @@ module Aws
26
36
 
27
37
  private
28
38
 
29
- def build_current(name, client, trace_header)
30
- Thread.current.thread_variable_set(VAR_NAME, Context.new(name, client, trace_header))
39
+ def build_current(name, client, trace)
40
+ Thread.current.thread_variable_set(VAR_NAME, Context.new(name, client, trace))
31
41
  end
32
42
 
33
43
  def remove_current
@@ -37,11 +47,11 @@ module Aws
37
47
 
38
48
  attr_reader :name
39
49
 
40
- def initialize(name, client, trace_header)
50
+ def initialize(name, client, trace)
41
51
  @name = name
42
52
  @client = client
43
- @trace_header = trace_header
44
- @base_segment = Segment.build(@name, trace_header)
53
+ @trace = trace
54
+ @base_segment = Segment.build(@name, trace)
45
55
  end
46
56
 
47
57
  # Rescue standard errors and record the error to the segment.
@@ -67,7 +77,7 @@ module Aws
67
77
  # @yield [Aws::Xray::SubSegment]
68
78
  # @return [Object] A value which given block returns.
69
79
  def child_trace(remote:, name:)
70
- sub = SubSegment.build(@trace_header, @base_segment, remote: remote, name: name)
80
+ sub = SubSegment.build(@trace, @base_segment, remote: remote, name: name)
71
81
  res = yield sub
72
82
  @client.send_segment(sub)
73
83
  res
@@ -14,8 +14,8 @@ module Aws
14
14
  name = @name || req_env.request_headers['Host'] || "unknown-request-from-#{Context.current.name}"
15
15
 
16
16
  Context.current.child_trace(remote: true, name: name) do |sub|
17
- propagate_trace_header = sub.generate_trace_header
18
- req_env.request_headers[TRACE_HEADER] = propagate_trace_header.to_header_value
17
+ propagate_trace = sub.generate_trace
18
+ req_env.request_headers[TRACE_HEADER] = propagate_trace.to_header_value
19
19
  sub.set_http_request(Request.build_from_faraday_env(req_env))
20
20
 
21
21
  @app.call(req_env).on_complete do |res_env|
data/lib/aws/xray/rack.rb CHANGED
@@ -1,4 +1,4 @@
1
- require 'aws/xray/trace_header'
1
+ require 'aws/xray/trace'
2
2
  require 'aws/xray/client'
3
3
  require 'aws/xray/context'
4
4
  require 'aws/xray/version_detector'
@@ -8,12 +8,6 @@ module Aws
8
8
  class Rack
9
9
  TRACE_ENV = 'HTTP_X_AMZN_TRACE_ID'.freeze
10
10
 
11
- class MissingNameError < ::StandardError
12
- def initialize
13
- super("`name` is empty. Configure this with `Aws::Xray.config.name = 'my-app'`.")
14
- end
15
- end
16
-
17
11
  # TODO: excluded_paths, included_paths
18
12
  #
19
13
  # @param [Hash] client_options For xray-agent client.
@@ -28,18 +22,18 @@ module Aws
28
22
 
29
23
  def call(env)
30
24
  header_value = env[TRACE_ENV]
31
- trace_header = if header_value
32
- TraceHeader.build_from_header_value(header_value)
25
+ trace = if header_value
26
+ Trace.build_from_header_value(header_value)
33
27
  else
34
- TraceHeader.generate
28
+ Trace.generate
35
29
  end
36
30
 
37
- Context.with_new_context(@name, @client, trace_header) do
31
+ Context.with_new_context(@name, @client, trace) do
38
32
  Context.current.base_trace do |seg|
39
33
  seg.set_http_request(Request.build_from_rack_env(env))
40
34
  status, headers, body = @app.call(env)
41
35
  seg.set_http_response(status, headers['Content-Length'])
42
- headers[TRACE_HEADER] = trace_header.to_header_value
36
+ headers[TRACE_HEADER] = trace.to_header_value
43
37
  [status, headers, body]
44
38
  end
45
39
  end
@@ -3,6 +3,10 @@ require 'aws/xray'
3
3
  module Aws
4
4
  module Xray
5
5
  class Railtie < ::Rails::Railtie
6
+ initializer 'aws-xray.set_name' do |app|
7
+ Aws::Xray.config.name = app.class.parent_name.underscore.gsub(/(\/|_)/, '-')
8
+ end
9
+
6
10
  initializer 'aws-xray.rack_middleware' do |app|
7
11
  app.middleware.use Aws::Xray::Rack
8
12
  end
@@ -10,8 +10,8 @@ module Aws
10
10
  # http://docs.aws.amazon.com/xray/latest/devguide/xray-api-segmentdocuments.html
11
11
  class Segment
12
12
  class << self
13
- def build(name, trace_header)
14
- new(name: name, trace_id: trace_header.root, parent_id: trace_header.parent)
13
+ def build(name, trace)
14
+ new(name: name, trace_id: trace.root, parent_id: trace.parent)
15
15
  end
16
16
  end
17
17
 
@@ -5,15 +5,15 @@ module Aws
5
5
  # http://docs.aws.amazon.com/xray/latest/devguide/xray-api-segmentdocuments.html
6
6
  class SubSegment < Segment
7
7
  # @param [Boolean] remote
8
- def self.build(trace_header, parent, remote:, name:)
9
- new(name: name, trace_header: trace_header, parent_id: parent.id, remote: remote)
8
+ def self.build(trace, parent, remote:, name:)
9
+ new(name: name, trace: trace, parent_id: parent.id, remote: remote)
10
10
  end
11
11
 
12
12
  TYPE_NAME = 'subsegment'.freeze
13
13
 
14
- def initialize(name:, trace_header:, parent_id:, remote:)
15
- super(name: name, trace_id: trace_header.root, parent_id: parent_id)
16
- @trace_header = trace_header
14
+ def initialize(name:, trace:, parent_id:, remote:)
15
+ super(name: name, trace_id: trace.root, parent_id: parent_id)
16
+ @trace = trace
17
17
  @remote = !!remote
18
18
  end
19
19
 
@@ -37,8 +37,8 @@ module Aws
37
37
  h
38
38
  end
39
39
 
40
- def generate_trace_header
41
- @trace_header.copy(parent: @id)
40
+ def generate_trace
41
+ @trace.copy(parent: @id)
42
42
  end
43
43
  end
44
44
  end
@@ -3,7 +3,7 @@ require 'securerandom'
3
3
 
4
4
  module Aws
5
5
  module Xray
6
- class TraceHeader
6
+ class Trace
7
7
  class << self
8
8
  def generate(now = Time.now)
9
9
  new(root: generate_root(now))
@@ -1,5 +1,5 @@
1
1
  module Aws
2
2
  module Xray
3
- VERSION = "0.3.0"
3
+ VERSION = "0.4.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aws-xray
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Taiki Ono
@@ -122,7 +122,7 @@ files:
122
122
  - lib/aws/xray/response.rb
123
123
  - lib/aws/xray/segment.rb
124
124
  - lib/aws/xray/sub_segment.rb
125
- - lib/aws/xray/trace_header.rb
125
+ - lib/aws/xray/trace.rb
126
126
  - lib/aws/xray/version.rb
127
127
  - lib/aws/xray/version_detector.rb
128
128
  homepage: https://github.com/taiki45/aws-xray