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 +4 -4
- data/README.md +6 -10
- data/example/campain_app_config.ru +2 -1
- data/example/fron_app_config.ru +2 -1
- data/example/recipe_app_config.ru +2 -1
- data/example/user_app_config.ru +2 -1
- data/lib/aws/xray.rb +19 -0
- data/lib/aws/xray/client.rb +4 -0
- data/lib/aws/xray/context.rb +18 -8
- data/lib/aws/xray/faraday.rb +2 -2
- data/lib/aws/xray/rack.rb +6 -12
- data/lib/aws/xray/rails.rb +4 -0
- data/lib/aws/xray/segment.rb +2 -2
- data/lib/aws/xray/sub_segment.rb +7 -7
- data/lib/aws/xray/{trace_header.rb → trace.rb} +1 -1
- data/lib/aws/xray/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2caae0694ce40c2e40f13c9ef137f20fe74afad1
|
4
|
+
data.tar.gz: 4d1063ecbb78d4ba27c798d6c5017268097e7bf8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
94
|
-
|
95
|
-
client.get('/foo')
|
91
|
+
Aws::Xray.trace do |seg|
|
92
|
+
client.get('/foo')
|
96
93
|
|
97
|
-
|
98
|
-
|
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
|
```
|
data/example/fron_app_config.ru
CHANGED
@@ -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
|
-
|
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|
|
data/example/user_app_config.ru
CHANGED
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
|
data/lib/aws/xray/client.rb
CHANGED
data/lib/aws/xray/context.rb
CHANGED
@@ -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
|
-
|
21
|
-
|
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,
|
30
|
-
Thread.current.thread_variable_set(VAR_NAME, Context.new(name, client,
|
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,
|
50
|
+
def initialize(name, client, trace)
|
41
51
|
@name = name
|
42
52
|
@client = client
|
43
|
-
@
|
44
|
-
@base_segment = Segment.build(@name,
|
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(@
|
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
|
data/lib/aws/xray/faraday.rb
CHANGED
@@ -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
|
-
|
18
|
-
req_env.request_headers[TRACE_HEADER] =
|
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/
|
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
|
-
|
32
|
-
|
25
|
+
trace = if header_value
|
26
|
+
Trace.build_from_header_value(header_value)
|
33
27
|
else
|
34
|
-
|
28
|
+
Trace.generate
|
35
29
|
end
|
36
30
|
|
37
|
-
Context.with_new_context(@name, @client,
|
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] =
|
36
|
+
headers[TRACE_HEADER] = trace.to_header_value
|
43
37
|
[status, headers, body]
|
44
38
|
end
|
45
39
|
end
|
data/lib/aws/xray/rails.rb
CHANGED
@@ -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
|
data/lib/aws/xray/segment.rb
CHANGED
@@ -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,
|
14
|
-
new(name: name, trace_id:
|
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
|
|
data/lib/aws/xray/sub_segment.rb
CHANGED
@@ -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(
|
9
|
-
new(name: name,
|
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:,
|
15
|
-
super(name: name, trace_id:
|
16
|
-
@
|
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
|
41
|
-
@
|
40
|
+
def generate_trace
|
41
|
+
@trace.copy(parent: @id)
|
42
42
|
end
|
43
43
|
end
|
44
44
|
end
|
data/lib/aws/xray/version.rb
CHANGED
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.
|
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/
|
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
|