griffin-interceptors 0.1.1 → 0.1.2

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: 772bd86a69a96858bd66e4f81551ddcf0b9a6549
4
- data.tar.gz: 951bacb34052702b3f89249fd29acf68dc1e5596
3
+ metadata.gz: d687a238cf3ecde03021fd40766e81c2a863246b
4
+ data.tar.gz: 3c8421032378fd6c396c5752d1433550932b76f2
5
5
  SHA512:
6
- metadata.gz: 2e04ae6298bc7a9a7034de52381afc47a219de1fa48bc394a742b65d0fe33f147efd7e2868e1d995c958ca710ce5f74452afc8bc90c2a772f6fd6ac757a06f4e
7
- data.tar.gz: 1af485be413670f6a7b3b77c1e68fcba6d9b8f139fa934f778057e222d03f255fcb3b9bc74140b0a613d50d866d37742540d5bfa19da5a04c7d6ea3108cc6284
6
+ metadata.gz: 410427b0ef506f0e6c04f9ab7ef5adde9fe1c788acb55582359c052839837054b4bf14700bc755bb2561031ff7ea393bf73f544dc29a111ebdf4727bdb8dedb7
7
+ data.tar.gz: 9d0a90d658c1fb7a51ac28392875d57c7eaa9b9d3d540489c9cb06f0fa8bba9ee7015cbc5c60ef6de283b3bd4e048d261f9896f010e673359f849ae39cfe83f3
@@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
20
20
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
21
  spec.require_paths = ['lib']
22
22
 
23
- spec.add_dependency 'griffin', '~> 0.1.3'
23
+ spec.add_dependency 'griffin', '>= 0.1.5'
24
24
 
25
25
  spec.add_development_dependency 'bundler'
26
26
  spec.add_development_dependency 'rake'
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'grpc_kit'
4
+ require 'forwardable'
5
+
6
+ module Griffin
7
+ module Interceptors
8
+ class CallStream < GrpcKit::Call
9
+ include Enumerable
10
+ extend Forwardable
11
+ delegate %i[send_msg recv] => :@inner
12
+
13
+ # @params call [GrpcKit::Call]
14
+ def initialize(inner)
15
+ @inner = inner
16
+ end
17
+
18
+ def each
19
+ loop { yield(recv) }
20
+ end
21
+
22
+ def method_missing(name, *args, &block)
23
+ @inner.public_send(name, *args, &block)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'griffin/interceptors/call_stream'
4
+
3
5
  module Griffin
4
6
  module Interceptors
5
7
  module Client
@@ -8,31 +10,23 @@ module Griffin
8
10
  # @param logger [Logger]
9
11
  # @param base_log [Hash<String,String>]
10
12
  def initialize(inner, logger, base_log = {}, filter = nil)
11
- @inner = inner
13
+ super(inner)
12
14
  @logger = logger
13
15
  @filter = filter
14
16
  @base_log = base_log
15
17
  end
16
18
 
17
- def send_msg(msg, **opts)
19
+ def send_msg(msg)
18
20
  @logger.info(@base_log.merge('grpc.request.content' => extract_content(msg)))
19
- @inner.send_msg(msg, **opts)
21
+ super
20
22
  end
21
23
 
22
- def recv(**opt)
23
- @inner.recv(**opt).tap do |resp|
24
+ def recv
25
+ super.tap do |resp|
24
26
  @logger.info(@base_log.merge('grpc.response.content' => extract_content(resp)))
25
27
  end
26
28
  end
27
29
 
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
30
  private
37
31
 
38
32
  def extract_content(request)
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ gem 'newrelic_rpm'
4
+
5
+ module Griffin
6
+ module Interceptors
7
+ module Server
8
+ class NewRelicInterceptor < GRPC::ServerInterceptor
9
+ def initialize(ignored_services: [])
10
+ @ignored_services = ignored_services.map(&:service_name)
11
+ end
12
+
13
+ Request = Struct.new(:path, :user_agent, :request_method)
14
+
15
+ def request_response(method: nil, call: nil, **)
16
+ return yield if NewRelic::Agent.instance.started?
17
+
18
+ service_name = call.service_name
19
+
20
+ return yield if @ignored_services.include?(service_name)
21
+
22
+ transaction_name = build_transaction_name(service_name, method.name)
23
+ state = NewRelic::Agent::TransactionState.tl_get
24
+
25
+ # gRPC's HTTP method is fixed. https://github.com/grpc/grpc/blob/af89e8c00e796f3398b09b7daed693df2b14da56/doc/PROTOCOL-HTTP2.md
26
+ req = Request.new("/#{transaction_name}", call.metadata['user-agent'], 'POST')
27
+ # ":controller" is not correct category name for gRPC, But since we want to categorized this transaction as web transactions.
28
+ # https://docs.newrelic.com/docs/apm/transactions/key-transactions/introduction-key-transactions
29
+ NewRelic::Agent::Transaction.start(state, :controller, transaction_name: "Controller/#{transaction_name}", request: req)
30
+
31
+ begin
32
+ yield
33
+ # gRPC alway returns HTTP status code 200
34
+ state.current_transaction.http_response_code = '200'
35
+ rescue => e
36
+ NewRelic::Agent::Transaction.notice_error(e)
37
+ raise e
38
+ ensure
39
+ NewRelic::Agent::Transaction.stop(state)
40
+ end
41
+ end
42
+
43
+ # For now, we don't support server_streamer, client_streamer and bidi_streamer
44
+
45
+ private
46
+
47
+ def build_transaction_name(service, mthd)
48
+ "#{service}/#{mthd}"
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -1,26 +1,28 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'griffin/interceptors/call_stream'
4
+
3
5
  module Griffin
4
6
  module Interceptors
5
7
  module Server
6
- class PayloadStreamer
8
+ class PayloadStreamer < Griffin::Interceptors::CallStream
7
9
  # @param inner [GrpcKit::Calls::Call]
8
10
  # @param logger [Logger]
9
11
  # @param base_log [Hash<String,String>]
10
12
  def initialize(inner, logger, base_log = {}, filter = nil)
11
- @inner = inner
13
+ super(inner)
12
14
  @logger = logger
13
15
  @filter = filter
14
16
  @base_log = base_log
15
17
  end
16
18
 
17
- def send_msg(msg, **opts)
19
+ def send_msg(msg)
18
20
  @logger.info(@base_log.merge('grpc.response.content' => extract_content(msg)))
19
- @inner.send_msg(msg, **opts)
21
+ super
20
22
  end
21
23
 
22
- def recv(**opt)
23
- @inner.recv(**opt).tap do |req|
24
+ def recv
25
+ super.tap do |req|
24
26
  @logger.info(@base_log.merge('grpc.request.content' => extract_content(req)))
25
27
  end
26
28
  end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'timeout'
4
+
5
+ module Griffin
6
+ module Interceptors
7
+ module Server
8
+ class TimeoutInterceptor < GRPC::ServerInterceptor
9
+ DEFAULT_TIMEOUT = 5
10
+
11
+ def initialize(timeout = DEFAULT_TIMEOUT)
12
+ @timeout = timeout
13
+ end
14
+
15
+ def request_response(*)
16
+ Timeout.timeout(@timeout) do
17
+ yield
18
+ end
19
+ end
20
+
21
+ # For now, we don't support server_streamer, client_streamer and bidi_streamer
22
+ end
23
+ end
24
+ end
25
+ end
@@ -1,5 +1,5 @@
1
1
  module Griffin
2
2
  module Interceptors
3
- VERSION = '0.1.1'.freeze
3
+ VERSION = '0.1.2'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: griffin-interceptors
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuta Iwama
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-11-28 00:00:00.000000000 Z
11
+ date: 2018-12-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: griffin
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 0.1.3
19
+ version: 0.1.5
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 0.1.3
26
+ version: 0.1.5
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -97,6 +97,7 @@ files:
97
97
  - bin/console
98
98
  - bin/setup
99
99
  - griffin-interceptors.gemspec
100
+ - lib/griffin/interceptors/call_stream.rb
100
101
  - lib/griffin/interceptors/client/filtered_payload_interceptor.rb
101
102
  - lib/griffin/interceptors/client/logging_interceptor.rb
102
103
  - lib/griffin/interceptors/client/payload_interceptor.rb
@@ -105,10 +106,12 @@ files:
105
106
  - lib/griffin/interceptors/server/clear_connection_interceptor.rb
106
107
  - lib/griffin/interceptors/server/filtered_payload_interceptor.rb
107
108
  - lib/griffin/interceptors/server/logging_interceptor.rb
109
+ - lib/griffin/interceptors/server/newrelic_interceptor.rb
108
110
  - lib/griffin/interceptors/server/payload_interceptor.rb
109
111
  - lib/griffin/interceptors/server/payload_streamer.rb
110
112
  - lib/griffin/interceptors/server/rails_exception_interceptor.rb
111
113
  - lib/griffin/interceptors/server/raven_interceptor.rb
114
+ - lib/griffin/interceptors/server/timeout_interceptor.rb
112
115
  - lib/griffin/interceptors/server/x_request_id_interceptor.rb
113
116
  - lib/griffin/interceptors/version.rb
114
117
  homepage: https://github.com/cookpad/griffin-interceptors