griffin-interceptors 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +13 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +58 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/griffin-interceptors.gemspec +29 -0
- data/lib/griffin/interceptors/client/filtered_payload_interceptor.rb +43 -0
- data/lib/griffin/interceptors/client/logging_interceptor.rb +61 -0
- data/lib/griffin/interceptors/client/payload_interceptor.rb +45 -0
- data/lib/griffin/interceptors/client/payload_streamer.rb +48 -0
- data/lib/griffin/interceptors/client/x_request_id_interceptor.rb +22 -0
- data/lib/griffin/interceptors/server/clear_connection_interceptor.rb +21 -0
- data/lib/griffin/interceptors/server/filtered_payload_interceptor.rb +43 -0
- data/lib/griffin/interceptors/server/logging_interceptor.rb +61 -0
- data/lib/griffin/interceptors/server/payload_interceptor.rb +45 -0
- data/lib/griffin/interceptors/server/payload_streamer.rb +40 -0
- data/lib/griffin/interceptors/server/raven_interceptor.rb +28 -0
- data/lib/griffin/interceptors/server/x_request_id_interceptor.rb +25 -0
- data/lib/griffin/interceptors/version.rb +5 -0
- metadata +137 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: fbf801dc9dfaa3ffd17dec259fa7d411222276a3
|
4
|
+
data.tar.gz: d46097bea89df3cc0f7acad34edc99a31861dadf
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2d60a73b3e89a2581717a43fc3e9a5906a79ea6b609cf6b1e73651f6c2f6d205c10a0ffad44e42b0016430b46d0796fd03662bbe50583bc6d52ac5e7cd18a78f
|
7
|
+
data.tar.gz: 9b404a3571b9f57e95654c55fc9d458046d98ddf2f0aba438cf51594a88a47c45da1ecb397d1cbdba2ca33f559784499e756090dff6c63c92699ae84ed6005cc
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 TODO: Yuta Iwama
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
# Griffin::Interceptors
|
2
|
+
|
3
|
+
Griffin::Interceptors is a collection of gRPC's interceptors for [griffin](https://github.com/ganmacs/griffin).
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'griffin-interceptors'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
```rb
|
20
|
+
class GreeterServer < Helloworld::Greeter::Service
|
21
|
+
def say_hello(hello_req, _unused_call)
|
22
|
+
Helloworld::HelloReply.new(message: "Hello #{hello_req.name}")
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
require 'griffin/interceptors/server/filtered_payload_interceptor'
|
27
|
+
require 'griffin/interceptors/server/logging_interceptor'
|
28
|
+
require 'griffin/interceptors/server/x_request_id_interceptor'
|
29
|
+
|
30
|
+
interceptors = [
|
31
|
+
Griffin::Interceptors::Server::FilteredPayloadInterceptor.new,
|
32
|
+
Griffin::Interceptors::Server::LoggingInterceptor.new,
|
33
|
+
Griffin::Interceptors::Server::XRequestIdInterceptor.new,
|
34
|
+
]
|
35
|
+
|
36
|
+
Griffin::Server.configure do |c|
|
37
|
+
c.bind '127.0.0.1'
|
38
|
+
|
39
|
+
c.port 50051
|
40
|
+
|
41
|
+
c.services GreeterServer.new
|
42
|
+
|
43
|
+
c.interceptors interceptors
|
44
|
+
|
45
|
+
c.workers 2
|
46
|
+
end
|
47
|
+
|
48
|
+
Griffin::Server.run
|
49
|
+
```
|
50
|
+
|
51
|
+
## Contributing
|
52
|
+
|
53
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/cookpad/griffin-interceptors. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
54
|
+
|
55
|
+
## License
|
56
|
+
|
57
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
58
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'griffin/interceptors'
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require 'irb'
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
lib = File.expand_path('lib', __dir__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'griffin/interceptors/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'griffin-interceptors'
|
7
|
+
spec.version = Griffin::Interceptors::VERSION
|
8
|
+
spec.authors = ['Yuta Iwama']
|
9
|
+
spec.email = ['yuta-iwama@cookpad.com']
|
10
|
+
|
11
|
+
spec.summary = 'A collection of interceptors for griffin'
|
12
|
+
spec.description = 'A collection of interceptors for griffin'
|
13
|
+
spec.homepage = 'https://github.com/cookpad/griffin-interceptors'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
17
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
end
|
19
|
+
spec.bindir = 'exe'
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ['lib']
|
22
|
+
|
23
|
+
spec.add_dependency 'griffin', '~> 0.1.3'
|
24
|
+
|
25
|
+
spec.add_development_dependency 'bundler'
|
26
|
+
spec.add_development_dependency 'rake'
|
27
|
+
spec.add_development_dependency 'rspec'
|
28
|
+
spec.add_development_dependency 'rubocop'
|
29
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'griffin/interceptors/client/payload_interceptor'
|
4
|
+
|
5
|
+
# Check actionpack exists to use ActionDispatch::Http::ParameterFilter
|
6
|
+
gem 'actionpack'
|
7
|
+
|
8
|
+
module Griffin
|
9
|
+
module Interceptors
|
10
|
+
module Client
|
11
|
+
class FilteredPayloadInterceptor < PayloadInterceptor
|
12
|
+
def initialize(filter_parameters: [])
|
13
|
+
@filters = filter_parameters.map do |filter|
|
14
|
+
case filter
|
15
|
+
when String, Symbol
|
16
|
+
Regexp.new(filter.to_s, true)
|
17
|
+
else
|
18
|
+
filter
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
@parameter_filter = ActionDispatch::Http::ParameterFilter.new(@filters)
|
23
|
+
end
|
24
|
+
|
25
|
+
def server_streamer(call: nil, **)
|
26
|
+
logs = {}
|
27
|
+
|
28
|
+
if call.metadata['x-request-id']
|
29
|
+
logs['grpc.x_request_id'] = call.metadata['x-request-id']
|
30
|
+
end
|
31
|
+
|
32
|
+
yield(Griffin::Interceptors::Client::PayloadStreamer.new(call, GRPC.logger, logs, @parameter_filter))
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def extract_content(request)
|
38
|
+
@parameter_filter.filter(super)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Griffin
|
4
|
+
module Interceptors
|
5
|
+
module Client
|
6
|
+
class LoggingInterceptor < GRPC::ClientInterceptor
|
7
|
+
def request_response(call: nil, **)
|
8
|
+
now = Time.now
|
9
|
+
log = build_log(call, now)
|
10
|
+
|
11
|
+
resp =
|
12
|
+
begin
|
13
|
+
yield
|
14
|
+
rescue => e
|
15
|
+
if e.is_a?(GRPC::BadStatus)
|
16
|
+
log['grpc.code'] = e.code
|
17
|
+
else
|
18
|
+
log['grpc.code'] = '2' # UNKNOWN
|
19
|
+
end
|
20
|
+
|
21
|
+
log['grpc.duration'] = (Time.now - now).to_s
|
22
|
+
GRPC.logger.info(log)
|
23
|
+
raise e
|
24
|
+
end
|
25
|
+
|
26
|
+
log['grpc.duration'] = (Time.now - now).to_s
|
27
|
+
GRPC.logger.info(log)
|
28
|
+
resp
|
29
|
+
end
|
30
|
+
|
31
|
+
alias_method :server_streamer, :request_response
|
32
|
+
alias_method :client_streamer, :request_response
|
33
|
+
alias_method :bidi_streamer, :request_response
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
# @return [Hash<String,String>]
|
38
|
+
def build_log(call, start_time)
|
39
|
+
log = {
|
40
|
+
'system' => 'grpc',
|
41
|
+
'span.kind' => 'client',
|
42
|
+
'grpc.method' => call.method_name,
|
43
|
+
'grpc.service_name' => call.service_name,
|
44
|
+
'grpc.start_time' => start_time.to_s,
|
45
|
+
'grpc.code' => '0', # OK
|
46
|
+
}
|
47
|
+
|
48
|
+
if call.metadata['x-requst-id']
|
49
|
+
log['grpc.x_request_id'] = call.metadata['x-request-id']
|
50
|
+
end
|
51
|
+
|
52
|
+
if call.deadline
|
53
|
+
log['grpc.request.deadline'] = call.deadline
|
54
|
+
end
|
55
|
+
|
56
|
+
log
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'griffin/interceptors/client/payload_streamer'
|
4
|
+
|
5
|
+
module Griffin
|
6
|
+
module Interceptors
|
7
|
+
module Client
|
8
|
+
class PayloadInterceptor < GRPC::ClientInterceptor
|
9
|
+
def request_response(request: nil, call: nil, **)
|
10
|
+
logs = {}
|
11
|
+
|
12
|
+
if call.metadata['x-request-id']
|
13
|
+
logs['grpc.x_request_id'] = call.metadata['x-request-id']
|
14
|
+
end
|
15
|
+
|
16
|
+
GRPC.logger.info(logs.merge('grpc.request.content' => extract_content(request)))
|
17
|
+
|
18
|
+
yield.tap do |resp|
|
19
|
+
logs['grpc.response.content'] = extract_content(resp)
|
20
|
+
GRPC.logger.info(logs)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def server_streamer(call: nil, **)
|
25
|
+
logs = {}
|
26
|
+
|
27
|
+
if call.metadata['x-request-id']
|
28
|
+
logs['grpc.x_request_id'] = call.metadata['x-request-id']
|
29
|
+
end
|
30
|
+
|
31
|
+
yield(Griffin::Interceptors::Client::PayloadStreamer.new(call, GRPC.logger, logs))
|
32
|
+
end
|
33
|
+
|
34
|
+
alias_method :client_streamer, :server_streamer
|
35
|
+
alias_method :bidi_streamer, :server_streamer
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def extract_content(msg)
|
40
|
+
msg.to_h
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Griffin
|
4
|
+
module Interceptors
|
5
|
+
module Client
|
6
|
+
class PayloadStreamer
|
7
|
+
# @param inner [GrpcKit::Calls::Call]
|
8
|
+
# @param logger [Logger]
|
9
|
+
# @param base_log [Hash<String,String>]
|
10
|
+
def initialize(inner, logger, base_log = {}, filter = nil)
|
11
|
+
@inner = inner
|
12
|
+
@logger = logger
|
13
|
+
@filter = filter
|
14
|
+
@base_log = base_log
|
15
|
+
end
|
16
|
+
|
17
|
+
def send_msg(msg, **opts)
|
18
|
+
@logger.info(@base_log.merge('grpc.request.content' => extract_content(msg)))
|
19
|
+
@inner.send_msg(msg, **opts)
|
20
|
+
end
|
21
|
+
|
22
|
+
def recv(**opt)
|
23
|
+
@inner.recv(**opt).tap do |resp|
|
24
|
+
@logger.info(@base_log.merge('grpc.response.content' => extract_content(resp)))
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def close_and_recv
|
29
|
+
@inner.close_and_recv
|
30
|
+
end
|
31
|
+
|
32
|
+
def close_and_send
|
33
|
+
@inner.close_and_send
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def extract_content(request)
|
39
|
+
if @filter
|
40
|
+
@filter.filter(request.to_h)
|
41
|
+
else
|
42
|
+
request.to_h
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'securerandom'
|
4
|
+
|
5
|
+
module Griffin
|
6
|
+
module Interceptors
|
7
|
+
module Client
|
8
|
+
class XRequestIdInterceptor < GRPC::ClientInterceptor
|
9
|
+
KEY = 'x-request-id'
|
10
|
+
|
11
|
+
def request_response(call: nil, **)
|
12
|
+
call.metadata[KEY] = SecureRandom.uuid
|
13
|
+
yield
|
14
|
+
end
|
15
|
+
|
16
|
+
alias_method :server_streamer, :request_response
|
17
|
+
alias_method :client_streamer, :request_response
|
18
|
+
alias_method :bidi_streamer, :request_response
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Griffin
|
4
|
+
module Interceptors
|
5
|
+
module Server
|
6
|
+
class ClearConnectionInterceptor < GRPC::ServerInterceptor
|
7
|
+
def request_response(*)
|
8
|
+
Rails.application.executor.wrap do
|
9
|
+
ActiveRecord::Base.connection_pool.with_connection do
|
10
|
+
yield
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
alias_method :server_streamer, :request_response
|
16
|
+
alias_method :client_streamer, :request_response
|
17
|
+
alias_method :bidi_streamer, :request_response
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'griffin/interceptors/server/payload_interceptor'
|
4
|
+
|
5
|
+
# Check actionpack exists to use ActionDispatch::Http::ParameterFilter
|
6
|
+
gem 'actionpack'
|
7
|
+
|
8
|
+
module Griffin
|
9
|
+
module Interceptors
|
10
|
+
module Server
|
11
|
+
class FilteredPayloadInterceptor < PayloadInterceptor
|
12
|
+
def initialize(filter_parameters: [])
|
13
|
+
@filters = filter_parameters.map do |filter|
|
14
|
+
case filter
|
15
|
+
when String, Symbol
|
16
|
+
Regexp.new(filter.to_s, true)
|
17
|
+
else
|
18
|
+
filter
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
@parameter_filter = ActionDispatch::Http::ParameterFilter.new(@filters)
|
23
|
+
end
|
24
|
+
|
25
|
+
def server_streamer(call: nil, **)
|
26
|
+
logs = {}
|
27
|
+
|
28
|
+
if call.metadata['x-request-id']
|
29
|
+
logs['grpc.x_request_id'] = call.metadata['x-request-id']
|
30
|
+
end
|
31
|
+
|
32
|
+
yield(Griffin::Interceptors::Server::PayloadStreamer.new(call, GRPC.logger, logs, @parameter_filter))
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def extract_content(request)
|
38
|
+
@parameter_filter.filter(super)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Griffin
|
4
|
+
module Interceptors
|
5
|
+
module Server
|
6
|
+
class LoggingInterceptor < GRPC::ServerInterceptor
|
7
|
+
def request_response(call: nil, **)
|
8
|
+
now = Time.now
|
9
|
+
log = build_log(call, now)
|
10
|
+
|
11
|
+
resp =
|
12
|
+
begin
|
13
|
+
yield
|
14
|
+
rescue => e
|
15
|
+
if e.is_a?(GRPC::BadStatus)
|
16
|
+
log['grpc.code'] = e.code
|
17
|
+
else
|
18
|
+
log['grpc.code'] = '2' # UNKNOWN
|
19
|
+
end
|
20
|
+
|
21
|
+
log['grpc.duration'] = (Time.now - now).to_s
|
22
|
+
GRPC.logger.info(log)
|
23
|
+
raise e
|
24
|
+
end
|
25
|
+
|
26
|
+
log['grpc.duration'] = (Time.now - now).to_s
|
27
|
+
GRPC.logger.info(log)
|
28
|
+
resp
|
29
|
+
end
|
30
|
+
|
31
|
+
alias_method :server_streamer, :request_response
|
32
|
+
alias_method :client_streamer, :request_response
|
33
|
+
alias_method :bidi_streamer, :request_response
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
# @return [Hash<String,String>]
|
38
|
+
def build_log(call, start_time)
|
39
|
+
log = {
|
40
|
+
'system' => 'grpc',
|
41
|
+
'span.kind' => 'server',
|
42
|
+
'grpc.method' => call.method_name,
|
43
|
+
'grpc.service_name' => call.service_name,
|
44
|
+
'grpc.start_time' => start_time.to_s,
|
45
|
+
'grpc.code' => '0', # OK
|
46
|
+
}
|
47
|
+
|
48
|
+
if call.metadata['x-requst-id']
|
49
|
+
log['grpc.x_request_id'] = call.metadata['x-request-id']
|
50
|
+
end
|
51
|
+
|
52
|
+
if call.deadline
|
53
|
+
log['grpc.request.deadline'] = call.deadline
|
54
|
+
end
|
55
|
+
|
56
|
+
log
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'griffin/interceptors/server/payload_streamer'
|
4
|
+
|
5
|
+
module Griffin
|
6
|
+
module Interceptors
|
7
|
+
module Server
|
8
|
+
class PayloadInterceptor < GRPC::ServerInterceptor
|
9
|
+
def request_response(request: nil, call: nil, **)
|
10
|
+
logs = {}
|
11
|
+
|
12
|
+
if call.metadata['x-request-id']
|
13
|
+
logs['grpc.x_request_id'] = call.metadata['x-request-id']
|
14
|
+
end
|
15
|
+
|
16
|
+
GRPC.logger.info(logs.merge('grpc.request.content' => extract_content(request)))
|
17
|
+
|
18
|
+
yield.tap do |resp|
|
19
|
+
logs['grpc.response.content'] = extract_content(resp)
|
20
|
+
GRPC.logger.info(logs)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def server_streamer(call: nil, **)
|
25
|
+
logs = {}
|
26
|
+
|
27
|
+
if call.metadata['x-request-id']
|
28
|
+
logs['grpc.x_request_id'] = call.metadata['x-request-id']
|
29
|
+
end
|
30
|
+
|
31
|
+
yield(Griffin::Interceptors::Server::PayloadStreamer.new(call, GRPC.logger, logs))
|
32
|
+
end
|
33
|
+
|
34
|
+
alias_method :client_streamer, :server_streamer
|
35
|
+
alias_method :bidi_streamer, :server_streamer
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def extract_content(msg)
|
40
|
+
msg.to_h
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Griffin
|
4
|
+
module Interceptors
|
5
|
+
module Server
|
6
|
+
class PayloadStreamer
|
7
|
+
# @param inner [GrpcKit::Calls::Call]
|
8
|
+
# @param logger [Logger]
|
9
|
+
# @param base_log [Hash<String,String>]
|
10
|
+
def initialize(inner, logger, base_log = {}, filter = nil)
|
11
|
+
@inner = inner
|
12
|
+
@logger = logger
|
13
|
+
@filter = filter
|
14
|
+
@base_log = base_log
|
15
|
+
end
|
16
|
+
|
17
|
+
def send_msg(msg, **opts)
|
18
|
+
@logger.info(@base_log.merge('grpc.response.content' => extract_content(msg)))
|
19
|
+
@inner.send_msg(msg, **opts)
|
20
|
+
end
|
21
|
+
|
22
|
+
def recv(**opt)
|
23
|
+
@inner.recv(**opt).tap do |req|
|
24
|
+
@logger.info(@base_log.merge('grpc.request.content' => extract_content(req)))
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def extract_content(request)
|
31
|
+
if @filter
|
32
|
+
@filter.filter(request.to_h)
|
33
|
+
else
|
34
|
+
request.to_h
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
gem 'sentry-raven'
|
4
|
+
|
5
|
+
module Griffin
|
6
|
+
module Interceptors
|
7
|
+
module Server
|
8
|
+
class RavenInterceptor < GRPC::ServerInterceptor
|
9
|
+
def request_response(*)
|
10
|
+
begin
|
11
|
+
yield
|
12
|
+
rescue => e
|
13
|
+
raise e if e.is_a?(GRPC::BadStatus)
|
14
|
+
|
15
|
+
GRPC.logger.error("Internal server error: #{e.message}")
|
16
|
+
Raven.capture_exception(e)
|
17
|
+
|
18
|
+
raise GRPC::Unknown.new('Internal server error')
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
alias_method :server_streamer, :request_response
|
23
|
+
alias_method :client_streamer, :request_response
|
24
|
+
alias_method :bidi_streamer, :request_response
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'securerandom'
|
4
|
+
|
5
|
+
module Griffin
|
6
|
+
module Interceptors
|
7
|
+
module Server
|
8
|
+
class XRequestIdInterceptor < GRPC::ServerInterceptor
|
9
|
+
KEY = 'x-request-id'
|
10
|
+
|
11
|
+
def request_response(call: nil, **)
|
12
|
+
unless call.metadata[KEY]
|
13
|
+
call.metadata[KEY] = SecureRandom.uuid
|
14
|
+
end
|
15
|
+
|
16
|
+
yield
|
17
|
+
end
|
18
|
+
|
19
|
+
alias_method :server_streamer, :request_response
|
20
|
+
alias_method :client_streamer, :request_response
|
21
|
+
alias_method :bidi_streamer, :request_response
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: griffin-interceptors
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yuta Iwama
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-11-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: griffin
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.1.3
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.1.3
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
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'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
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: rubocop
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: A collection of interceptors for griffin
|
84
|
+
email:
|
85
|
+
- yuta-iwama@cookpad.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".travis.yml"
|
93
|
+
- Gemfile
|
94
|
+
- LICENSE.txt
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- bin/console
|
98
|
+
- bin/setup
|
99
|
+
- griffin-interceptors.gemspec
|
100
|
+
- lib/griffin/interceptors/client/filtered_payload_interceptor.rb
|
101
|
+
- lib/griffin/interceptors/client/logging_interceptor.rb
|
102
|
+
- lib/griffin/interceptors/client/payload_interceptor.rb
|
103
|
+
- lib/griffin/interceptors/client/payload_streamer.rb
|
104
|
+
- lib/griffin/interceptors/client/x_request_id_interceptor.rb
|
105
|
+
- lib/griffin/interceptors/server/clear_connection_interceptor.rb
|
106
|
+
- lib/griffin/interceptors/server/filtered_payload_interceptor.rb
|
107
|
+
- lib/griffin/interceptors/server/logging_interceptor.rb
|
108
|
+
- lib/griffin/interceptors/server/payload_interceptor.rb
|
109
|
+
- lib/griffin/interceptors/server/payload_streamer.rb
|
110
|
+
- lib/griffin/interceptors/server/raven_interceptor.rb
|
111
|
+
- lib/griffin/interceptors/server/x_request_id_interceptor.rb
|
112
|
+
- lib/griffin/interceptors/version.rb
|
113
|
+
homepage: https://github.com/cookpad/griffin-interceptors
|
114
|
+
licenses:
|
115
|
+
- MIT
|
116
|
+
metadata: {}
|
117
|
+
post_install_message:
|
118
|
+
rdoc_options: []
|
119
|
+
require_paths:
|
120
|
+
- lib
|
121
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
122
|
+
requirements:
|
123
|
+
- - ">="
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - ">="
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0'
|
131
|
+
requirements: []
|
132
|
+
rubyforge_project:
|
133
|
+
rubygems_version: 2.6.14.1
|
134
|
+
signing_key:
|
135
|
+
specification_version: 4
|
136
|
+
summary: A collection of interceptors for griffin
|
137
|
+
test_files: []
|