griffin-interceptors 0.1.4 → 0.1.9

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
- SHA1:
3
- metadata.gz: fe0fd2e86343914ca2f2f4a6716c415a2ac24936
4
- data.tar.gz: 4932bb8474d59d681889c86a607eb1f1c76c1758
2
+ SHA256:
3
+ metadata.gz: 239fb7f3036b63120fdeb63eeb5504848b95a1e89d34708186fec7fb7b492203
4
+ data.tar.gz: de98b38b9a0ee827b439948cf0179da9ab2f2bcb08b06da2db05de23403a95fa
5
5
  SHA512:
6
- metadata.gz: 1f2da5faf64741eaf1455fc209443fb5c2a99310fb39a23d27f949024d74b005692e027262d5b3a69d0b56be57b5f66350113b4810a899881a6697388bb0fb44
7
- data.tar.gz: 3a726be452970dffe6a7b0bb9117069352ae4d4a5e3d5a1442669a492518855fd28f04f729b63d4da46a4afa7de4c0796f108be59a1b6f96dca1048f23b49389
6
+ metadata.gz: 5051fa9479be355ae9a5982dfffd6896149c7261e9bea9f75ab8db3d72752eda04546eeeb12fdcb52b672bc7a60b7420806c1e782482dfa57b426b818bfee014
7
+ data.tar.gz: f6eec90328bd4aa6e7617a9b2bacdbcf7512565d3b97186ef39ff98a49fb74719c84c94270c8170959c9ea5402993434593ffb88c83cbc3dd0c1e8ab27732ac0
@@ -20,7 +20,8 @@ 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.5'
23
+ spec.add_dependency 'get_process_mem', '~> 0.2.3'
24
+ spec.add_dependency 'griffin', '>= 0.2.3'
24
25
 
25
26
  spec.add_development_dependency 'bundler'
26
27
  spec.add_development_dependency 'rake'
@@ -2,8 +2,8 @@
2
2
 
3
3
  require 'griffin/interceptors/server/payload_interceptor'
4
4
 
5
- # Check actionpack exists to use ActionDispatch::Http::ParameterFilter
6
- gem 'actionpack'
5
+ # Check actionpack exists to use ActionDispatch::Http::ParameterFilter if needed
6
+ gem 'actionpack' unless defined?(ActiveSupport::ParameterFilter)
7
7
 
8
8
  module Griffin
9
9
  module Interceptors
@@ -19,7 +19,13 @@ module Griffin
19
19
  end
20
20
  end
21
21
 
22
- @parameter_filter = ActionDispatch::Http::ParameterFilter.new(@filters)
22
+ # ActionDispatch::Http::ParameterFilter is deprecated and will be removed from Rails 6.1.
23
+ parameter_filter_klass = if defined?(ActiveSupport::ParameterFilter)
24
+ ActiveSupport::ParameterFilter
25
+ else
26
+ ActionDispatch::Http::ParameterFilter
27
+ end
28
+ @parameter_filter = parameter_filter_klass.new(@filters)
23
29
  end
24
30
 
25
31
  def server_streamer(call: nil, **)
@@ -45,7 +45,7 @@ module Griffin
45
45
  'grpc.code' => '0', # OK
46
46
  }
47
47
 
48
- if call.metadata['x-requst-id']
48
+ if call.metadata['x-request-id']
49
49
  log['grpc.x_request_id'] = call.metadata['x-request-id']
50
50
  end
51
51
 
@@ -19,26 +19,14 @@ module Griffin
19
19
 
20
20
  return yield if @ignored_services.include?(service_name)
21
21
 
22
- transaction_name = build_transaction_name(service_name, method.name)
23
- state = NewRelic::Agent::TransactionState.tl_get
24
-
25
22
  # 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
- resp = yield
33
- # gRPC alway returns HTTP status code 200
34
- state.current_transaction.http_response_code = '200'
35
-
36
- resp
37
- rescue => e
38
- NewRelic::Agent::Transaction.notice_error(e)
39
- raise e
40
- ensure
41
- NewRelic::Agent::Transaction.stop(state)
23
+ request = Request.new("/#{service_name}/#{method.name}", call.metadata['user-agent'], 'POST')
24
+
25
+ in_transaction("#{service_name}/#{method.name}", request) do |txn|
26
+ yield.tap do
27
+ # gRPC always returns HTTP status code 200.
28
+ txn.http_response_code = '200'
29
+ end
42
30
  end
43
31
  end
44
32
 
@@ -46,8 +34,21 @@ module Griffin
46
34
 
47
35
  private
48
36
 
49
- def build_transaction_name(service, mthd)
50
- "#{service}/#{mthd}"
37
+ if Gem::Version.new(NewRelic::VERSION::STRING) >= Gem::Version.new('6.0.0')
38
+ def in_transaction(partial_name, request)
39
+ NewRelic::Agent::Tracer.in_transaction(partial_name: partial_name, category: :web, options: { request: request }) do
40
+ yield NewRelic::Agent::Tracer.current_transaction
41
+ end
42
+ end
43
+ else
44
+ def in_transaction(partial_name, request)
45
+ state = NewRelic::Agent::TransactionState.tl_get
46
+ # Specify the controller category so that the transaction gets categorized as a web transaction.
47
+ # See https://github.com/newrelic/newrelic-ruby-agent/blob/5.7.0.350/lib/new_relic/agent/transaction.rb#L39.
48
+ NewRelic::Agent::Transaction.wrap(state, "Controller/#{partial_name}", :controller, request: request) do
49
+ yield state.current_transaction
50
+ end
51
+ end
51
52
  end
52
53
  end
53
54
  end
@@ -17,6 +17,9 @@ module Griffin
17
17
 
18
18
  raise GRPC::Unknown.new('Internal server error')
19
19
  end
20
+ ensure
21
+ Raven::Context.clear!
22
+ Raven::BreadcrumbBuffer.clear!
20
23
  end
21
24
 
22
25
  alias_method :server_streamer, :request_response
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'get_process_mem'
4
+
5
+ module Griffin
6
+ module Interceptors
7
+ module Server
8
+ class WorkerKillerInterceptor < GRPC::ServerInterceptor
9
+ def initialize(memory_limit_min: 1024**3, memory_limit_max: 2 * (1024**3), check_cycle: 16)
10
+ @worker_memory_limit_max = memory_limit_max
11
+ @worker_memory_limit_min = memory_limit_min
12
+ @worker_check_cycle = check_cycle
13
+ @worker_memory_limit = @worker_memory_limit_min + rand(@worker_memory_limit_max - @worker_memory_limit_min + 1)
14
+ @worker_check_count = 0
15
+ @sent_signals = false
16
+ end
17
+
18
+ def request_response(*)
19
+ yield.tap do
20
+ break if @sent_signals
21
+
22
+ @worker_process_start ||= Time.now
23
+ @worker_check_count += 1
24
+
25
+ if (@worker_check_count % @worker_check_cycle) == 0
26
+ rss = GetProcessMem.new.bytes
27
+ if rss > @worker_memory_limit
28
+ Griffin.logger.warn("Worker (pid: #{Process.pid}) exceeds memory limit (#{rss.to_i} bytes > #{@worker_memory_limit} bytes)")
29
+ send_restart_signal(@worker_process_start)
30
+ end
31
+
32
+ @worker_check_count = 0
33
+ end
34
+ end
35
+ end
36
+
37
+ alias_method :server_streamer, :request_response
38
+ alias_method :client_streamer, :request_response
39
+ alias_method :bidi_streamer, :request_response
40
+
41
+ private
42
+
43
+ def send_restart_signal(start_time)
44
+ # No need to get a lock, sending signals duplicately is acceptable
45
+ @sent_signals = true
46
+ pid = Process.pid
47
+ alive_sec = (Time.now - start_time).round
48
+
49
+ Griffin.logger.info("Send signal(USR1) pid: #{pid}, alive_sec: #{alive_sec}")
50
+ Process.kill :USR1, pid
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -1,5 +1,5 @@
1
1
  module Griffin
2
2
  module Interceptors
3
- VERSION = '0.1.4'.freeze
3
+ VERSION = '0.1.9'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,29 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: griffin-interceptors
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuta Iwama
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-01-10 00:00:00.000000000 Z
11
+ date: 2020-08-05 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: get_process_mem
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.2.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.2.3
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: griffin
15
29
  requirement: !ruby/object:Gem::Requirement
16
30
  requirements:
17
31
  - - ">="
18
32
  - !ruby/object:Gem::Version
19
- version: 0.1.5
33
+ version: 0.2.3
20
34
  type: :runtime
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: 0.1.5
40
+ version: 0.2.3
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: bundler
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -112,13 +126,14 @@ files:
112
126
  - lib/griffin/interceptors/server/rails_exception_interceptor.rb
113
127
  - lib/griffin/interceptors/server/raven_interceptor.rb
114
128
  - lib/griffin/interceptors/server/timeout_interceptor.rb
129
+ - lib/griffin/interceptors/server/worker_killer_interceptor.rb
115
130
  - lib/griffin/interceptors/server/x_request_id_interceptor.rb
116
131
  - lib/griffin/interceptors/version.rb
117
132
  homepage: https://github.com/cookpad/griffin-interceptors
118
133
  licenses:
119
134
  - MIT
120
135
  metadata: {}
121
- post_install_message:
136
+ post_install_message:
122
137
  rdoc_options: []
123
138
  require_paths:
124
139
  - lib
@@ -133,9 +148,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
133
148
  - !ruby/object:Gem::Version
134
149
  version: '0'
135
150
  requirements: []
136
- rubyforge_project:
137
- rubygems_version: 2.6.14.1
138
- signing_key:
151
+ rubygems_version: 3.1.2
152
+ signing_key:
139
153
  specification_version: 4
140
154
  summary: A collection of interceptors for griffin
141
155
  test_files: []