aws-xray 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.rspec +2 -0
- data/.travis.yml +10 -0
- data/README.md +64 -7
- data/Rakefile +6 -0
- data/aws-xray.gemspec +14 -11
- data/example/Gemfile +7 -0
- data/example/Procfile +5 -0
- data/example/README.md +27 -0
- data/example/campain_app_config.ru +7 -0
- data/example/fron_app_config.ru +18 -0
- data/example/recipe_app_config.ru +28 -0
- data/example/user_app_config.ru +7 -0
- data/lib/aws-xray.rb +1 -0
- data/lib/aws/xray.rb +4 -2
- data/lib/aws/xray/client.rb +26 -0
- data/lib/aws/xray/context.rb +72 -0
- data/lib/aws/xray/faraday.rb +27 -0
- data/lib/aws/xray/header_parser.rb +50 -0
- data/lib/aws/xray/rack.rb +36 -0
- data/lib/aws/xray/segment.rb +72 -0
- data/lib/aws/xray/sub_segment.rb +32 -0
- data/lib/aws/xray/trace_header.rb +54 -0
- data/lib/aws/xray/version.rb +1 -1
- metadata +71 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 44943f3ac93283072875bfa94985b9aa76828628
|
4
|
+
data.tar.gz: 7c58541a4ba68f4b7968505029229b1f2abb0c76
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e266ce6f924d71c447e1ca64775efbf55de67523965b3c805eb8fac5e9c99f936590857765156a8a8333ccf4d32a6cbcd0a04519220fcd5a7cd67315ff06f0b4
|
7
|
+
data.tar.gz: 1988b22141c57f19b2bb3963b2f691dbbcd8ecb6a08bdeb3037e4dfee0a20193d28c8f6899a82f7257d762eeb5a89113b382dbf1d97e9b7ab976a2c4a24221d4
|
data/.gitignore
CHANGED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,8 +1,24 @@
|
|
1
1
|
# Aws::Xray
|
2
|
+
[](https://travis-ci.org/taiki45/aws-xray)
|
3
|
+
[](https://badge.fury.io/rb/aws-xray)
|
2
4
|
|
3
|
-
|
5
|
+
The unofficial AWS X-Ray Tracing SDK for Ruby.
|
6
|
+
It enables you to capture in-coming HTTP requests and out-going HTTP requests and send them to xray-agent automatically.
|
4
7
|
|
5
|
-
|
8
|
+
AWS X-Ray is a ditributed tracing system. See more detail about AWS X-Ray at [official document](http://docs.aws.amazon.com/xray/latest/devguide/aws-xray.html).
|
9
|
+
|
10
|
+
## Current status
|
11
|
+
Implemented:
|
12
|
+
|
13
|
+
- Rack middleware.
|
14
|
+
- Faraday middleware.
|
15
|
+
- Propagatin support limited in single thread environment.
|
16
|
+
|
17
|
+
Not yet:
|
18
|
+
|
19
|
+
- Tracing HTTP request/response.
|
20
|
+
- Multi thread support.
|
21
|
+
- Tracing errors.
|
6
22
|
|
7
23
|
## Installation
|
8
24
|
|
@@ -16,13 +32,54 @@ And then execute:
|
|
16
32
|
|
17
33
|
$ bundle
|
18
34
|
|
19
|
-
|
35
|
+
## Usage
|
36
|
+
### Rack app
|
37
|
+
```ruby
|
38
|
+
# config.ru
|
39
|
+
require 'aws-xray'
|
40
|
+
use Aws::Xray::Rack
|
41
|
+
```
|
20
42
|
|
21
|
-
|
43
|
+
This allow your app to trace in-coming HTTP requests.
|
22
44
|
|
23
|
-
|
45
|
+
To trace out-going HTTP requests, use Faraday middleware.
|
24
46
|
|
25
|
-
|
47
|
+
```ruby
|
48
|
+
Faraday.new('...', headers: { 'Host' => 'down-stream-app-id' } ) do |builder|
|
49
|
+
builder.use Aws::Xray::Faraday
|
50
|
+
# ...
|
51
|
+
end
|
52
|
+
```
|
53
|
+
|
54
|
+
If you don't use any Service Discovery tools, pass the down stream app name to the Faraday middleware:
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
Faraday.new('...') do |builder|
|
58
|
+
builder.use Aws::Xray::Faraday, name: 'down-stream-app-id'
|
59
|
+
# ...
|
60
|
+
end
|
61
|
+
```
|
62
|
+
|
63
|
+
### non-Rack app (like background jobs)
|
64
|
+
```ruby
|
65
|
+
# Build HTTP client with Faraday builder.
|
66
|
+
# You can set the down stream app id to Host header as well.
|
67
|
+
client = Faraday.new('...') do |builder|
|
68
|
+
builder.use Aws::Xray::Faraday, name: 'down-stream-app-id'
|
69
|
+
# ...
|
70
|
+
end
|
71
|
+
|
72
|
+
# Start new tracing context then perform arbitrary actions in the block.
|
73
|
+
Aws::Xray::Context.with_new_context('test-app', xray_client, trace_header) do
|
74
|
+
Aws::Xray::Context.current.base_trace do
|
75
|
+
client.get('/foo')
|
76
|
+
|
77
|
+
Aws::Xray::Context.current.child_trace do |sub|
|
78
|
+
# DB access or something to trace.
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
```
|
26
83
|
|
27
84
|
## Development
|
28
85
|
|
@@ -32,7 +89,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
32
89
|
|
33
90
|
## Contributing
|
34
91
|
|
35
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
92
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/taiki45/aws-xray.
|
36
93
|
|
37
94
|
## License
|
38
95
|
|
data/Rakefile
CHANGED
data/aws-xray.gemspec
CHANGED
@@ -1,26 +1,29 @@
|
|
1
1
|
# coding: utf-8
|
2
|
-
lib = File.expand_path(
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require
|
4
|
+
require 'aws/xray/version'
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name =
|
7
|
+
spec.name = 'aws-xray'
|
8
8
|
spec.version = Aws::Xray::VERSION
|
9
|
-
spec.authors = [
|
10
|
-
spec.email = [
|
9
|
+
spec.authors = ['Taiki Ono']
|
10
|
+
spec.email = ['taiks.4559@gmail.com']
|
11
11
|
|
12
12
|
spec.summary = %q{The unofficial X-Ray Tracing SDK for Ruby.}
|
13
13
|
spec.description = spec.summary
|
14
|
-
spec.homepage =
|
15
|
-
spec.license =
|
14
|
+
spec.homepage = 'https://github.com/taiki45/aws-xray'
|
15
|
+
spec.license = 'MIT'
|
16
16
|
|
17
17
|
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
18
|
f.match(%r{^(test|spec|features)/})
|
19
19
|
end
|
20
|
-
spec.bindir =
|
20
|
+
spec.bindir = 'exe'
|
21
21
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
-
spec.require_paths = [
|
22
|
+
spec.require_paths = ['lib']
|
23
23
|
|
24
|
-
spec.
|
25
|
-
spec.add_development_dependency
|
24
|
+
spec.add_dependency 'faraday'
|
25
|
+
spec.add_development_dependency 'bundler'
|
26
|
+
spec.add_development_dependency 'rake'
|
27
|
+
spec.add_development_dependency 'rspec'
|
28
|
+
spec.add_development_dependency 'pry-byebug'
|
26
29
|
end
|
data/example/Gemfile
ADDED
data/example/Procfile
ADDED
@@ -0,0 +1,5 @@
|
|
1
|
+
front: env RECIPE_APP=127.0.0.1:3001 bundle exec rackup fron_app_config.ru -o 127.0.0.1 -p 3000
|
2
|
+
recipe: env USER_APP=127.0.0.1:3002 CAMPAIN_APP=127.0.0.1:3003 bundle exec rackup recipe_app_config.ru -o 127.0.0.1 -p 3001
|
3
|
+
user: bundle exec rackup user_app_config.ru -o 127.0.0.1 -p 3002
|
4
|
+
campain: bundle exec rackup campain_app_config.ru -o 127.0.0.1 -p 3003
|
5
|
+
agent: socat UDP-RECVFROM:2000,fork STDOUT
|
data/example/README.md
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# Example apps
|
2
|
+
```
|
3
|
+
--> user-app
|
4
|
+
client -> front-app -> recipe-app -|
|
5
|
+
--> campain-app
|
6
|
+
```
|
7
|
+
|
8
|
+
To simulate xray-agent, this example runs socat which prints given bytes to STDOUT.
|
9
|
+
|
10
|
+
## Running without xray-agent
|
11
|
+
At first, install [socat](http://www.dest-unreach.org/socat/).
|
12
|
+
|
13
|
+
```
|
14
|
+
bundle install
|
15
|
+
bundle exec foreman start
|
16
|
+
```
|
17
|
+
|
18
|
+
## Running with xray-agent
|
19
|
+
Follow official document to install xray-agent: http://docs.aws.amazon.com/xray/latest/devguide/xray-daemon.html
|
20
|
+
|
21
|
+
Run xray-agent on localhost. Usually it recieves packats at UDP:2000.
|
22
|
+
|
23
|
+
Run example apps without socat like:
|
24
|
+
|
25
|
+
```
|
26
|
+
bundle exec foreman start -m 'front=1,recipe=1,user=1,campain=1'
|
27
|
+
```
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'aws-xray'
|
3
|
+
|
4
|
+
recipe_app = ENV.fetch('RECIPE_APP') # host:port
|
5
|
+
|
6
|
+
use Aws::Xray::Rack, name: 'front-app'
|
7
|
+
|
8
|
+
run Proc.new {|env|
|
9
|
+
headers = { 'Host' => 'recipe-app' }
|
10
|
+
client = Faraday.new(url: "http://#{recipe_app}/", headers: headers) do |builder|
|
11
|
+
builder.use Aws::Xray::Faraday
|
12
|
+
builder.adapter Faraday.default_adapter
|
13
|
+
end
|
14
|
+
res = client.get('/')
|
15
|
+
|
16
|
+
body = "recipe_app returns: #{res.status}, #{res.body}"
|
17
|
+
['200', {'Content-Type' => 'text/plain'}, [body]]
|
18
|
+
}
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'aws-xray'
|
3
|
+
|
4
|
+
user_app = ENV.fetch('USER_APP') # host:port
|
5
|
+
campain_app = ENV.fetch('CAMPAIN_APP') # host:port
|
6
|
+
|
7
|
+
use Aws::Xray::Rack, name: 'recipe-app'
|
8
|
+
|
9
|
+
run Proc.new {|env|
|
10
|
+
user_client = Faraday.new(url: "http://#{user_app}/", headers: { 'Host' => 'user-app' }) do |builder|
|
11
|
+
builder.use Aws::Xray::Faraday
|
12
|
+
builder.adapter Faraday.default_adapter
|
13
|
+
end
|
14
|
+
user_res = user_client.get('/')
|
15
|
+
|
16
|
+
campain_client = Faraday.new(url: "http://#{campain_app}/", headers: { 'Host' => 'campain-app' }) do |builder|
|
17
|
+
builder.use Aws::Xray::Faraday
|
18
|
+
builder.adapter Faraday.default_adapter
|
19
|
+
end
|
20
|
+
campain_res = campain_client.get('/')
|
21
|
+
|
22
|
+
body = "awesome recipe by #{user_res.body}"
|
23
|
+
if campain_res.body == '1'
|
24
|
+
body << ': You got a campain!'
|
25
|
+
end
|
26
|
+
|
27
|
+
['200', {'Content-Type' => 'text/plain'}, [body]]
|
28
|
+
}
|
data/lib/aws-xray.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'aws/xray'
|
data/lib/aws/xray.rb
CHANGED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'socket'
|
2
|
+
|
3
|
+
module Aws
|
4
|
+
module Xray
|
5
|
+
class Client
|
6
|
+
# sock is for test
|
7
|
+
#
|
8
|
+
# XXX: keep options for implmenting copying later.
|
9
|
+
def initialize(host: '127.0.0.1', port: 2000, sock: nil)
|
10
|
+
@options = { host: host, port: port }
|
11
|
+
@sock = sock || Addrinfo.udp(host, port).connect
|
12
|
+
end
|
13
|
+
|
14
|
+
# @param [Aws::Xray::Segment] segment
|
15
|
+
def send_segment(segment)
|
16
|
+
segment.finish
|
17
|
+
payload = %!{"format": "json", "version": 1}\n#{segment.to_json}\n!
|
18
|
+
len = @sock.send(payload, 0)
|
19
|
+
# TODO: retry
|
20
|
+
if payload.size != len
|
21
|
+
$stderr.puts("Can not send all bytes: #{len} sent")
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'aws/xray/segment'
|
2
|
+
require 'aws/xray/sub_segment'
|
3
|
+
|
4
|
+
module Aws
|
5
|
+
module Xray
|
6
|
+
class Context
|
7
|
+
class << self
|
8
|
+
VAR_NAME = :_aws_xray_context_
|
9
|
+
|
10
|
+
class NotSetError < ::StandardError
|
11
|
+
def initialize
|
12
|
+
super('Context is not set for this thread')
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def current
|
17
|
+
Thread.current[VAR_NAME] || raise(NotSetError)
|
18
|
+
end
|
19
|
+
|
20
|
+
def with_new_context(name, client, trace_header)
|
21
|
+
build_current(name, client, trace_header)
|
22
|
+
yield
|
23
|
+
ensure
|
24
|
+
remove_current
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def build_current(name, client, trace_header)
|
30
|
+
Thread.current[VAR_NAME] = Context.new(name, client, trace_header)
|
31
|
+
end
|
32
|
+
|
33
|
+
# XXX: Use delete API if exists
|
34
|
+
def remove_current
|
35
|
+
Thread.current[VAR_NAME] = nil
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
attr_reader :name
|
40
|
+
|
41
|
+
def initialize(name, client, trace_header)
|
42
|
+
@name = name
|
43
|
+
@client = client
|
44
|
+
@trace_header = trace_header
|
45
|
+
@base_segment = Segment.build(@name, trace_header)
|
46
|
+
end
|
47
|
+
|
48
|
+
def base_trace
|
49
|
+
res = yield
|
50
|
+
@client.send_segment(@base_segment)
|
51
|
+
res
|
52
|
+
rescue => e
|
53
|
+
@base_segment.set_error(e)
|
54
|
+
@client.send_segment(@base_segment)
|
55
|
+
raise e
|
56
|
+
end
|
57
|
+
|
58
|
+
# @param [Boolean] remote
|
59
|
+
# @yield [Aws::Xray::SubSegment]
|
60
|
+
def child_trace(remote:, name:)
|
61
|
+
sub = SubSegment.build(@trace_header, @base_segment, remote: remote, name: name)
|
62
|
+
res = yield sub
|
63
|
+
@client.send_segment(sub)
|
64
|
+
res
|
65
|
+
rescue => e
|
66
|
+
sub.set_error(e)
|
67
|
+
@client.send_segment(sub)
|
68
|
+
raise e
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
|
3
|
+
module Aws
|
4
|
+
module Xray
|
5
|
+
class Faraday < ::Faraday::Middleware
|
6
|
+
def initialize(app, name = nil)
|
7
|
+
super(app)
|
8
|
+
@name = name
|
9
|
+
end
|
10
|
+
|
11
|
+
# XXX: use host header?
|
12
|
+
def call(req_env)
|
13
|
+
name = @name || req_env.request_headers['Host'] || "unknown-request-from-#{Context.current.name}"
|
14
|
+
|
15
|
+
Context.current.child_trace(remote: true, name: name) do |sub|
|
16
|
+
propagate_trace_header = sub.generate_trace_header
|
17
|
+
req_env.request_headers[TRACE_HEADER] = propagate_trace_header.to_header_value
|
18
|
+
sub.set_http_request(req_env)
|
19
|
+
|
20
|
+
@app.call(req_env).on_complete do |res_env|
|
21
|
+
sub.set_http_response(res_env)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module Aws
|
2
|
+
module Xray
|
3
|
+
module HeaderParser
|
4
|
+
extend self
|
5
|
+
|
6
|
+
# XXX: returns error when given invaild header_value
|
7
|
+
# Header format document: http://docs.aws.amazon.com/xray/latest/devguide/xray-concepts.html#xray-concepts-tracingheader
|
8
|
+
def parse(header_value)
|
9
|
+
h = {}
|
10
|
+
key = ''
|
11
|
+
value = ''
|
12
|
+
value_mode = false
|
13
|
+
header_value.chars.each_with_index do |c, i|
|
14
|
+
next if space?(c)
|
15
|
+
if delim?(c)
|
16
|
+
h[key] = value
|
17
|
+
key, value = '', ''
|
18
|
+
value_mode = false
|
19
|
+
next
|
20
|
+
end
|
21
|
+
|
22
|
+
if equal_mark?(c)
|
23
|
+
value_mode = true
|
24
|
+
next
|
25
|
+
end
|
26
|
+
|
27
|
+
if value_mode
|
28
|
+
value << c
|
29
|
+
else
|
30
|
+
key << c
|
31
|
+
end
|
32
|
+
end
|
33
|
+
h[key] = value if !key.empty? && !value.empty?
|
34
|
+
h
|
35
|
+
end
|
36
|
+
|
37
|
+
def space?(c)
|
38
|
+
c == ' '
|
39
|
+
end
|
40
|
+
|
41
|
+
def delim?(c)
|
42
|
+
c == ';'
|
43
|
+
end
|
44
|
+
|
45
|
+
def equal_mark?(c)
|
46
|
+
c == '='
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'aws/xray/trace_header'
|
2
|
+
require 'aws/xray/client'
|
3
|
+
require 'aws/xray/context'
|
4
|
+
|
5
|
+
module Aws
|
6
|
+
module Xray
|
7
|
+
class Rack
|
8
|
+
TRACE_ENV = 'HTTP_X_AMZN_TRACE_ID'.freeze
|
9
|
+
|
10
|
+
# XXX: excluded_paths, included_paths
|
11
|
+
# XXX: document about client_options
|
12
|
+
def initialize(app, name:, client_options: {})
|
13
|
+
@app = app
|
14
|
+
@name = name
|
15
|
+
@client = Client.new(client_options)
|
16
|
+
end
|
17
|
+
|
18
|
+
def call(env)
|
19
|
+
header_value = env[TRACE_ENV]
|
20
|
+
trace_header = if header_value
|
21
|
+
TraceHeader.build_from_header_value(header_value)
|
22
|
+
else
|
23
|
+
TraceHeader.generate
|
24
|
+
end
|
25
|
+
|
26
|
+
Context.with_new_context(@name, @client, trace_header) do
|
27
|
+
Context.current.base_trace do
|
28
|
+
status, headers, body = @app.call(env)
|
29
|
+
headers[TRACE_HEADER] = trace_header.to_header_value
|
30
|
+
[status, headers, body]
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'securerandom'
|
3
|
+
|
4
|
+
module Aws
|
5
|
+
module Xray
|
6
|
+
# http://docs.aws.amazon.com/xray/latest/devguide/xray-api-segmentdocuments.html
|
7
|
+
class Segment
|
8
|
+
class << self
|
9
|
+
def build(name, trace_header)
|
10
|
+
new(name: name, trace_id: trace_header.root, parent_id: trace_header.parent)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
attr_reader :name, :id, :trace_id, :parent_id
|
15
|
+
|
16
|
+
# TODO: securerandom?
|
17
|
+
def initialize(name:, trace_id:, parent_id: nil)
|
18
|
+
@name = name
|
19
|
+
@id = SecureRandom.hex(8)
|
20
|
+
@trace_id = trace_id
|
21
|
+
@parent_id = parent_id
|
22
|
+
start
|
23
|
+
@end_time = nil
|
24
|
+
@http_request = nil
|
25
|
+
@http_response = nil
|
26
|
+
@error = nil
|
27
|
+
end
|
28
|
+
|
29
|
+
# @param [Hash] env A Rack env
|
30
|
+
def set_http_request(env)
|
31
|
+
end
|
32
|
+
|
33
|
+
# @param [Array] res A Rack response
|
34
|
+
def set_http_response(res)
|
35
|
+
end
|
36
|
+
|
37
|
+
def set_error(e)
|
38
|
+
# TODO: Set error object
|
39
|
+
end
|
40
|
+
|
41
|
+
def finish(now = Time.now)
|
42
|
+
@end_time = now.to_f
|
43
|
+
end
|
44
|
+
|
45
|
+
def to_json
|
46
|
+
to_h.to_json
|
47
|
+
end
|
48
|
+
|
49
|
+
def to_h
|
50
|
+
h = {
|
51
|
+
name: @name,
|
52
|
+
id: @id,
|
53
|
+
trace_id: @trace_id,
|
54
|
+
start_time: @start_time,
|
55
|
+
}
|
56
|
+
if @end_time.nil?
|
57
|
+
h[:in_progress] = true
|
58
|
+
else
|
59
|
+
h[:end_time] = @end_time
|
60
|
+
end
|
61
|
+
h[:parent_id] = @parent_id if @parent_id
|
62
|
+
h
|
63
|
+
end
|
64
|
+
|
65
|
+
private
|
66
|
+
|
67
|
+
def start(now = Time.now)
|
68
|
+
@start_time = now.to_f
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'aws/xray/segment'
|
2
|
+
|
3
|
+
module Aws
|
4
|
+
module Xray
|
5
|
+
# http://docs.aws.amazon.com/xray/latest/devguide/xray-api-segmentdocuments.html
|
6
|
+
class SubSegment < Segment
|
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)
|
10
|
+
end
|
11
|
+
|
12
|
+
TYPE_NAME = 'subsegment'.freeze
|
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
|
17
|
+
@remote = !!remote
|
18
|
+
end
|
19
|
+
|
20
|
+
def to_h
|
21
|
+
h = super
|
22
|
+
h[:type] = TYPE_NAME
|
23
|
+
h[:namespace] = 'remote' if @remote
|
24
|
+
h
|
25
|
+
end
|
26
|
+
|
27
|
+
def generate_trace_header
|
28
|
+
@trace_header.copy(parent: @id)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'aws/xray/header_parser'
|
2
|
+
require 'securerandom'
|
3
|
+
|
4
|
+
module Aws
|
5
|
+
module Xray
|
6
|
+
class TraceHeader
|
7
|
+
class << self
|
8
|
+
def generate(now = Time.now)
|
9
|
+
new(root: generate_root(now))
|
10
|
+
end
|
11
|
+
|
12
|
+
def build_from_header_value(header_value)
|
13
|
+
h = HeaderParser.parse(header_value)
|
14
|
+
new(root: h.fetch('Root'), sampled: h['Sampled'] != '0', parent: h['Parent'])
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
# XXX: securerandom?
|
20
|
+
def generate_root(now)
|
21
|
+
"1-#{now.to_i.to_s(16)}-#{SecureRandom.hex(12)}"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
attr_reader :root, :parent
|
26
|
+
|
27
|
+
def initialize(root:, sampled: true, parent: nil)
|
28
|
+
@root = root
|
29
|
+
@sampled = sampled
|
30
|
+
@parent = parent
|
31
|
+
end
|
32
|
+
|
33
|
+
def to_header_value
|
34
|
+
v = "Root=#{@root};"
|
35
|
+
sampled? ? v << 'Sampled=1' : v << 'Sampled=0'
|
36
|
+
v << ";Parent=#{@parent}" if has_parent?
|
37
|
+
v
|
38
|
+
end
|
39
|
+
|
40
|
+
def sampled?
|
41
|
+
@sampled
|
42
|
+
end
|
43
|
+
|
44
|
+
def has_parent?
|
45
|
+
!!@parent
|
46
|
+
end
|
47
|
+
|
48
|
+
def copy(parent: nil)
|
49
|
+
parent = parent.nil? ? @parent : parent
|
50
|
+
self.class.new(root: @root, sampled: @sampled, parent: parent)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
data/lib/aws/xray/version.rb
CHANGED
metadata
CHANGED
@@ -1,43 +1,85 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: aws-xray
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.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-05-
|
11
|
+
date: 2017-05-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: faraday
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: bundler
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
16
30
|
requirements:
|
17
|
-
- - "
|
31
|
+
- - ">="
|
18
32
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
33
|
+
version: '0'
|
20
34
|
type: :development
|
21
35
|
prerelease: false
|
22
36
|
version_requirements: !ruby/object:Gem::Requirement
|
23
37
|
requirements:
|
24
|
-
- - "
|
38
|
+
- - ">="
|
25
39
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
40
|
+
version: '0'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: rake
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
30
44
|
requirements:
|
31
|
-
- - "
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry-byebug
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
32
74
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
75
|
+
version: '0'
|
34
76
|
type: :development
|
35
77
|
prerelease: false
|
36
78
|
version_requirements: !ruby/object:Gem::Requirement
|
37
79
|
requirements:
|
38
|
-
- - "
|
80
|
+
- - ">="
|
39
81
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
82
|
+
version: '0'
|
41
83
|
description: The unofficial X-Ray Tracing SDK for Ruby.
|
42
84
|
email:
|
43
85
|
- taiks.4559@gmail.com
|
@@ -46,6 +88,8 @@ extensions: []
|
|
46
88
|
extra_rdoc_files: []
|
47
89
|
files:
|
48
90
|
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".travis.yml"
|
49
93
|
- Gemfile
|
50
94
|
- LICENSE.txt
|
51
95
|
- README.md
|
@@ -53,7 +97,23 @@ files:
|
|
53
97
|
- aws-xray.gemspec
|
54
98
|
- bin/console
|
55
99
|
- bin/setup
|
100
|
+
- example/Gemfile
|
101
|
+
- example/Procfile
|
102
|
+
- example/README.md
|
103
|
+
- example/campain_app_config.ru
|
104
|
+
- example/fron_app_config.ru
|
105
|
+
- example/recipe_app_config.ru
|
106
|
+
- example/user_app_config.ru
|
107
|
+
- lib/aws-xray.rb
|
56
108
|
- lib/aws/xray.rb
|
109
|
+
- lib/aws/xray/client.rb
|
110
|
+
- lib/aws/xray/context.rb
|
111
|
+
- lib/aws/xray/faraday.rb
|
112
|
+
- lib/aws/xray/header_parser.rb
|
113
|
+
- lib/aws/xray/rack.rb
|
114
|
+
- lib/aws/xray/segment.rb
|
115
|
+
- lib/aws/xray/sub_segment.rb
|
116
|
+
- lib/aws/xray/trace_header.rb
|
57
117
|
- lib/aws/xray/version.rb
|
58
118
|
homepage: https://github.com/taiki45/aws-xray
|
59
119
|
licenses:
|
@@ -75,7 +135,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
75
135
|
version: '0'
|
76
136
|
requirements: []
|
77
137
|
rubyforge_project:
|
78
|
-
rubygems_version: 2.
|
138
|
+
rubygems_version: 2.5.2
|
79
139
|
signing_key:
|
80
140
|
specification_version: 4
|
81
141
|
summary: The unofficial X-Ray Tracing SDK for Ruby.
|