the_garage 2.3.3 → 2.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 +22 -1
- data/lib/garage/config.rb +8 -0
- data/lib/garage/strategy/auth_server.rb +9 -2
- data/lib/garage/tracer.rb +89 -0
- data/lib/garage/version.rb +1 -1
- metadata +6 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: df6a793861e979f1a7849b0d6ef3ef5764757980
|
4
|
+
data.tar.gz: df87e84d9f87372c8c2061d4aafabd6849d89b52
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 996522903af5c5b250cb7a99f41a8ed095a23c17b36076cd8bf34409b16d02b4f5ec6196b357238b1726452d8ceba415b481d4f2f56abbc7c19997972a557c97
|
7
|
+
data.tar.gz: c96ace465cac6ade9d7b477ab3c58b457b660e709067490213154f5b017392ca717b87980dd7058ada25ede679ad2de6c2d17ab43b20dbaf9259264c58e5b6a6
|
data/README.md
CHANGED
@@ -47,9 +47,15 @@ class Employee < ActiveRecord::Base
|
|
47
47
|
end
|
48
48
|
```
|
49
49
|
|
50
|
-
In your controller
|
50
|
+
In your controller classes:
|
51
51
|
|
52
52
|
```ruby
|
53
|
+
class ApplicationController < ActionController::Base
|
54
|
+
include Garage::ControllerHelper
|
55
|
+
|
56
|
+
# ...
|
57
|
+
end
|
58
|
+
|
53
59
|
class EmployeesController < ApplicationController
|
54
60
|
include Garage::RestfulActions
|
55
61
|
|
@@ -181,6 +187,21 @@ module MyStrategy
|
|
181
187
|
end
|
182
188
|
```
|
183
189
|
|
190
|
+
## Distributed tracing
|
191
|
+
In case you use auth-server strategy, you can setup distributed tracing for the service communication between garage application and auth server.
|
192
|
+
Currently, we support following tracers:
|
193
|
+
|
194
|
+
- `aws-xray` using [aws-xray](https://github.com/taiki45/aws-xray) gem.
|
195
|
+
- Bundle `aws-xray` gem in your application.
|
196
|
+
- Configure `service` option for a logical service name of the auth server.
|
197
|
+
|
198
|
+
```ruby
|
199
|
+
# e.g. aws-xray tracer
|
200
|
+
require 'aws/xray'
|
201
|
+
Garage::Tracer::AwsXrayTracer.service = 'your-auth-server-name'
|
202
|
+
Garage.configuration.tracer = Garage::Tracer::AwsXrayTracer
|
203
|
+
```
|
204
|
+
|
184
205
|
## Development
|
185
206
|
|
186
207
|
See [DEVELOPMENT.md](DEVELOPMENT.md).
|
data/lib/garage/config.rb
CHANGED
@@ -36,6 +36,14 @@ module Garage
|
|
36
36
|
instance_variable_defined?(:@strategy) ? @strategy : Garage::Strategy::NoAuthentication
|
37
37
|
end
|
38
38
|
|
39
|
+
# Support distributed tracing for auth server accesses.
|
40
|
+
#
|
41
|
+
# @param [Ojbect] tracer an object which implements tracer methods. See Garage::Tracer::NullTracer.
|
42
|
+
attr_writer :tracer
|
43
|
+
def tracer
|
44
|
+
@tracer ||= Garage::Tracer::NullTracer
|
45
|
+
end
|
46
|
+
|
39
47
|
def docs
|
40
48
|
@docs ||= Docs::Config.new
|
41
49
|
end
|
@@ -2,6 +2,8 @@ require 'json'
|
|
2
2
|
require 'net/http'
|
3
3
|
require 'uri'
|
4
4
|
|
5
|
+
require 'garage/tracer'
|
6
|
+
|
5
7
|
module Garage
|
6
8
|
module Strategy
|
7
9
|
module AuthServer
|
@@ -71,8 +73,13 @@ module Garage
|
|
71
73
|
private
|
72
74
|
|
73
75
|
def get
|
74
|
-
|
75
|
-
|
76
|
+
Tracer.start do |tracer|
|
77
|
+
request_header = tracer.inject_trace_context(header)
|
78
|
+
tracer.record_http_request('GET', uri.to_s, request_header['User-Agent'])
|
79
|
+
raw = http_client.get(path_with_query, request_header)
|
80
|
+
tracer.record_http_response(raw.code.to_i, raw['Content-Length'] || 0)
|
81
|
+
Response.new(raw)
|
82
|
+
end
|
76
83
|
end
|
77
84
|
|
78
85
|
def header
|
@@ -0,0 +1,89 @@
|
|
1
|
+
module Garage
|
2
|
+
module Tracer
|
3
|
+
extend self
|
4
|
+
extend Forwardable
|
5
|
+
|
6
|
+
def_delegators :tracer, :start, :inject_trace_context, :record_http_request, :record_http_response
|
7
|
+
|
8
|
+
private
|
9
|
+
|
10
|
+
def tracer
|
11
|
+
Garage.configuration.tracer
|
12
|
+
end
|
13
|
+
|
14
|
+
# Any tracers must have `.start` to start tracing context and:
|
15
|
+
# - `#inject_trace_context` to add tracing context to the given request header.
|
16
|
+
# - `#record_http_request` to record http request in tracer.
|
17
|
+
# - `#record_http_response` to recrod http response in tracer.
|
18
|
+
class NullTracer
|
19
|
+
def self.start(&block)
|
20
|
+
yield new
|
21
|
+
end
|
22
|
+
|
23
|
+
# @param [Hash] header
|
24
|
+
# @return [Hash]
|
25
|
+
def inject_trace_context(header)
|
26
|
+
header
|
27
|
+
end
|
28
|
+
|
29
|
+
# @param [String] method
|
30
|
+
# @param [String] url
|
31
|
+
# @param [String] user_agent
|
32
|
+
# @return [nil]
|
33
|
+
def record_http_request(method, url, user_agent)
|
34
|
+
end
|
35
|
+
|
36
|
+
# @param [Integer] status
|
37
|
+
# @param [Integer] content_length
|
38
|
+
def record_http_response(status, content_length)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
class AwsXrayTracer
|
43
|
+
class << self
|
44
|
+
attr_accessor :service
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.start(&block)
|
48
|
+
if Aws::Xray::Context.started?
|
49
|
+
Aws::Xray::Context.current.child_trace(remote: true, name: service) do |sub|
|
50
|
+
yield new(sub)
|
51
|
+
end
|
52
|
+
else
|
53
|
+
yield NullTracer.new
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def initialize(sub_segment)
|
58
|
+
@sub = sub_segment
|
59
|
+
end
|
60
|
+
|
61
|
+
def inject_trace_context(header)
|
62
|
+
header.merge('X-Amzn-Trace-Id' => @sub.generate_trace.to_header_value)
|
63
|
+
end
|
64
|
+
|
65
|
+
def record_http_request(method, url, user_agent)
|
66
|
+
request = Aws::Xray::Request.build(method: method.to_s.upcase, url: url, user_agent: user_agent)
|
67
|
+
@sub.set_http_request(request)
|
68
|
+
end
|
69
|
+
|
70
|
+
def record_http_response(status, content_length)
|
71
|
+
@sub.set_http_response(status, content_length || 0)
|
72
|
+
|
73
|
+
case status
|
74
|
+
when 499
|
75
|
+
cause = Aws::Xray::Cause.new(stack: caller, message: 'Got 499', type: 'http_request_error')
|
76
|
+
@sub.set_error(error: true, throttle: true, cause: cause)
|
77
|
+
when 400..498
|
78
|
+
cause = Aws::Xray::Cause.new(stack: caller, message: 'Got 4xx', type: 'http_request_error')
|
79
|
+
@sub.set_error(error: true, cause: cause)
|
80
|
+
when 500..599
|
81
|
+
cause = Aws::Xray::Cause.new(stack: caller, message: 'Got 5xx', type: 'http_request_error')
|
82
|
+
@sub.set_error(fault: true, remote: true, cause: cause)
|
83
|
+
else
|
84
|
+
# pass
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
data/lib/garage/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: the_garage
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tatsuhiko Miyagawa
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-06-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -247,11 +247,13 @@ files:
|
|
247
247
|
- lib/garage/strategy/test.rb
|
248
248
|
- lib/garage/test/migrator.rb
|
249
249
|
- lib/garage/token_scope.rb
|
250
|
+
- lib/garage/tracer.rb
|
250
251
|
- lib/garage/utils.rb
|
251
252
|
- lib/garage/version.rb
|
252
253
|
- lib/the_garage.rb
|
253
254
|
homepage: https://github.com/cookpad/garage
|
254
|
-
licenses:
|
255
|
+
licenses:
|
256
|
+
- MIT
|
255
257
|
metadata: {}
|
256
258
|
post_install_message:
|
257
259
|
rdoc_options: []
|
@@ -269,7 +271,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
269
271
|
version: '0'
|
270
272
|
requirements: []
|
271
273
|
rubyforge_project:
|
272
|
-
rubygems_version: 2.
|
274
|
+
rubygems_version: 2.6.11
|
273
275
|
signing_key:
|
274
276
|
specification_version: 4
|
275
277
|
summary: Garage Platform Engine
|