-
-
Notifications
You must be signed in to change notification settings - Fork 532
Expand file tree
/
Copy pathconcurrent_ruby.rb
More file actions
58 lines (49 loc) · 1.95 KB
/
concurrent_ruby.rb
File metadata and controls
58 lines (49 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
require 'sentry-ruby'
require 'concurrent-ruby'
module Sentry
module ConcurrentRuby
SENTRY_THREAD_KEY = :sentry_hub
SENTRY_SPAN_KEY = :sentry_span
def self.install
patch_futures
patch_promises
end
def self.patch_futures
::Concurrent::Promises.singleton_class.class_eval do
alias_method :original_future, :future
def future(*args, &block)
current_hub = Sentry.get_current_hub
current_span = Sentry.get_current_scope.get_span
original_future(*args) do
#set the hub and span
Thread.current.thread_variable_set(Sentry::ConcurrentRuby::SENTRY_THREAD_KEY, current_hub)
Thread.current.thread_variable_set(Sentry::ConcurrentRuby::SENTRY_SPAN_KEY, current_span)
Sentry.with_child_span(op: "api.request", description: "Concurrent::Future") do |child_span|
Sentry.get_current_scope.set_span(child_span)
block.call
end
end
end
end
end
def self.patch_promises
::Concurrent::Promises::Future.class_eval do
alias_method :original_on_resolution, :on_resolution
def on_resolution(&block)
current_hub = Thread.current.thread_variable_get(Sentry::ConcurrentRuby::SENTRY_THREAD_KEY) || Sentry.get_current_hub
current_span = Sentry.get_current_scope.get_span
original_on_resolution do |*args|
Thread.current.thread_variable_set(Sentry::ConcurrentRuby::SENTRY_THREAD_KEY, current_hub)
Thread.current.thread_variable_set(Sentry::ConcurrentRuby::SENTRY_SPAN_KEY, current_span)
Sentry.with_child_span(op: "api.request", description: "Concurrent::Promise") do |child_span|
Sentry.get_current_scope.set_span(child_span)
block.call(*args)
end
end
end
end
end
end
end
# Install after Sentry loaded
Sentry::ConcurrentRuby.install if defined?(Sentry)