griffin-interceptors 0.1.9 → 0.2.0

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
  SHA256:
3
- metadata.gz: 239fb7f3036b63120fdeb63eeb5504848b95a1e89d34708186fec7fb7b492203
4
- data.tar.gz: de98b38b9a0ee827b439948cf0179da9ab2f2bcb08b06da2db05de23403a95fa
3
+ metadata.gz: ffed86abcb2abcde04f6c18a0eca28277125f9828d83c4366d39b3e906fe226d
4
+ data.tar.gz: a02c68e604410540cf6637f0500b01966c8cf3338fe1d81138a71b3efbe8d8f4
5
5
  SHA512:
6
- metadata.gz: 5051fa9479be355ae9a5982dfffd6896149c7261e9bea9f75ab8db3d72752eda04546eeeb12fdcb52b672bc7a60b7420806c1e782482dfa57b426b818bfee014
7
- data.tar.gz: f6eec90328bd4aa6e7617a9b2bacdbcf7512565d3b97186ef39ff98a49fb74719c84c94270c8170959c9ea5402993434593ffb88c83cbc3dd0c1e8ab27732ac0
6
+ metadata.gz: 6d200ccd31a654190d80adf6f5549bc70cf817e5e20859e10c9eca395148a7b85bfbfb2288d2aaf5d485a5865f9a0602c903676d400c32e6009e79dfca7bc31f
7
+ data.tar.gz: '091fd8f580cea51fe2f8f85c4f18bf913f2c9b85f63fbf17247433223d48b21a82085917b6596dba2347775b8a05b0194fa188ca5fb437a2c514c2a2e00a808a'
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- gem 'sentry-raven'
4
-
5
3
  module Griffin
6
4
  module Interceptors
7
5
  module Server
@@ -10,13 +8,13 @@ module Griffin
10
8
  begin
11
9
  yield
12
10
  rescue ActiveRecord::RecordNotFound => e
13
- Raven.capture_exception(e)
11
+ capture_exception_if_defined(e)
14
12
  raise GRPC::NotFound.new(e.message)
15
13
  rescue ActiveRecord::StaleObjectError => e
16
- Raven.capture_exception(e)
14
+ capture_exception_if_defined(e)
17
15
  raise GRPC::Aborted.new(e.message)
18
16
  rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved => e
19
- Raven.capture_exception(e)
17
+ capture_exception_if_defined(e)
20
18
  raise GRPC::FailedPrecondition.new(e.message)
21
19
  end
22
20
  end
@@ -24,6 +22,16 @@ module Griffin
24
22
  alias_method :server_streamer, :request_response
25
23
  alias_method :client_streamer, :request_response
26
24
  alias_method :bidi_streamer, :request_response
25
+
26
+ private
27
+
28
+ def capture_exception_if_defined(e)
29
+ if defined?(Sentry)
30
+ Sentry.capture_exception(e)
31
+ elsif defined?(Raven)
32
+ Raven.capture_exception(e)
33
+ end
34
+ end
27
35
  end
28
36
  end
29
37
  end
@@ -6,7 +6,11 @@ module Griffin
6
6
  module Interceptors
7
7
  module Server
8
8
  class RavenInterceptor < GRPC::ServerInterceptor
9
- def request_response(*)
9
+ def request_response(call: nil, **)
10
+ if call.metadata['x-request-id']
11
+ Raven.tags_context(request_id: call.metadata['x-request-id'])
12
+ end
13
+
10
14
  begin
11
15
  yield
12
16
  rescue => e
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ gem 'scout_apm'
4
+
5
+ require 'scout_apm/layer'
6
+ require 'scout_apm/request_manager'
7
+
8
+ module Griffin
9
+ module Interceptors
10
+ module Server
11
+ class ScoutApmInterceptor < GRPC::ServerInterceptor
12
+ # Specify the controller layer so that the transaction gets categorized as a web transaction.
13
+ LAYER_TYPE = 'Controller'
14
+
15
+ def initialize(ignored_services: [], sampling_rate: 1.0)
16
+ @ignored_services = ignored_services.map(&:service_name)
17
+ @sampling_rate = sampling_rate.to_f
18
+ end
19
+
20
+ def request_response(request: nil, call: nil, method: nil)
21
+ service_name = call.service_name
22
+ method_name = method.name
23
+
24
+ return yield if rand > @sampling_rate || @ignored_services.include?(service_name)
25
+
26
+ layer = ScoutApm::Layer.new(LAYER_TYPE, "#{service_name}/#{method_name}")
27
+
28
+ req = ScoutApm::RequestManager.lookup
29
+ req.start_layer(layer)
30
+ req.annotate_request(uri: "/#{service_name}/#{method_name}")
31
+
32
+ ScoutApm::Context.add(
33
+ request_id: call.metadata['x-request-id'],
34
+ user_agent: call.metadata['user-agent'],
35
+ )
36
+
37
+ begin
38
+ yield
39
+ rescue => e
40
+ req.error!
41
+ raise e
42
+ ensure
43
+ req.stop_layer
44
+ end
45
+ end
46
+
47
+ # For now, we don't support server_streamer, client_streamer and bidi_streamer
48
+ # alias_method :server_streamer, :request_response
49
+ # alias_method :client_streamer, :request_response
50
+ # alias_method :bidi_streamer, :request_response
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ gem "sentry-ruby"
4
+
5
+ module Griffin
6
+ module Interceptors
7
+ module Server
8
+ class SentryInterceptor < GRPC::ServerInterceptor
9
+ TRANSACTION_OP = "griffin"
10
+
11
+ def request_response(request: nil, call: nil, method: nil)
12
+ return yield unless Sentry.initialized?
13
+
14
+ Sentry.clone_hub_to_current_thread
15
+
16
+ Sentry.with_scope do |scope|
17
+ Sentry.with_session_tracking do
18
+ scope.clear_breadcrumbs
19
+
20
+ service_name = call.service_name
21
+ method_name = method.name
22
+ scope.set_transaction_name("#{service_name}/#{method_name}")
23
+
24
+ transaction = start_transaction(call.metadata, scope)
25
+ scope.set_span(transaction) if transaction
26
+
27
+ if call.metadata["x-request-id"]
28
+ scope.set_tags(request_id: call.metadata["x-request-id"])
29
+ end
30
+
31
+ begin
32
+ response = yield
33
+ rescue GRPC::BadStatus => e
34
+ finish_transaction(transaction, e.code)
35
+ raise e
36
+ rescue => e
37
+ GRPC.logger.error("Internal server error: #{e}")
38
+ finish_transaction(transaction, GrpcKit::StatusCodes::INTERNAL)
39
+ Sentry.capture_exception(e)
40
+
41
+ raise GRPC::Unknown.new("Internal server error")
42
+ end
43
+
44
+ finish_transaction(transaction, GrpcKit::StatusCodes::OK)
45
+
46
+ response
47
+ end
48
+ end
49
+ end
50
+
51
+ alias_method :server_streamer, :request_response
52
+ alias_method :client_streamer, :request_response
53
+ alias_method :bidi_streamer, :request_response
54
+
55
+ private
56
+
57
+ def start_transaction(metadata, scope)
58
+ sentry_trace = metadata["grpc-sentry-trace"]
59
+ options = { name: scope.transaction_name, op: TRANSACTION_OP }
60
+ transaction = Sentry::Transaction.from_sentry_trace(sentry_trace, **options) if sentry_trace
61
+ Sentry.start_transaction(transaction: transaction, custom_sampling_context: { metadata: metadata }, **options)
62
+ end
63
+
64
+ def finish_transaction(transaction, status_code)
65
+ return unless transaction
66
+
67
+ transaction.set_http_status(status_code)
68
+ transaction.finish
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -1,5 +1,5 @@
1
1
  module Griffin
2
2
  module Interceptors
3
- VERSION = '0.1.9'.freeze
3
+ VERSION = '0.2.0'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: griffin-interceptors
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.9
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuta Iwama
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-08-05 00:00:00.000000000 Z
11
+ date: 2022-05-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: get_process_mem
@@ -125,6 +125,8 @@ files:
125
125
  - lib/griffin/interceptors/server/payload_streamer.rb
126
126
  - lib/griffin/interceptors/server/rails_exception_interceptor.rb
127
127
  - lib/griffin/interceptors/server/raven_interceptor.rb
128
+ - lib/griffin/interceptors/server/scout_apm_interceptor.rb
129
+ - lib/griffin/interceptors/server/sentry_interceptor.rb
128
130
  - lib/griffin/interceptors/server/timeout_interceptor.rb
129
131
  - lib/griffin/interceptors/server/worker_killer_interceptor.rb
130
132
  - lib/griffin/interceptors/server/x_request_id_interceptor.rb
@@ -148,7 +150,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
148
150
  - !ruby/object:Gem::Version
149
151
  version: '0'
150
152
  requirements: []
151
- rubygems_version: 3.1.2
153
+ rubygems_version: 3.2.32
152
154
  signing_key:
153
155
  specification_version: 4
154
156
  summary: A collection of interceptors for griffin