/
hub.rb
179 lines (133 loc) · 3.99 KB
/
hub.rb
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
require "sentry/scope"
require "sentry/client"
module Sentry
class Hub
include ArgumentCheckingHelper
attr_reader :last_event_id
def initialize(client, scope)
first_layer = Layer.new(client, scope)
@stack = [first_layer]
@last_event_id = nil
end
def new_from_top
Hub.new(current_client, current_scope)
end
def current_client
current_layer&.client
end
def configuration
current_client.configuration
end
def current_scope
current_layer&.scope
end
def clone
layer = current_layer
if layer
scope = layer.scope&.dup
Hub.new(layer.client, scope)
end
end
def bind_client(client)
layer = current_layer
if layer
layer.client = client
end
end
def configure_scope(&block)
block.call(current_scope)
end
def with_scope(&block)
push_scope
yield(current_scope)
ensure
pop_scope
end
def push_scope
new_scope =
if current_scope
current_scope.dup
else
Scope.new
end
@stack << Layer.new(current_client, new_scope)
end
def pop_scope
@stack.pop
end
def start_transaction(transaction: nil, custom_sampling_context: {}, **options)
return unless configuration.tracing_enabled?
transaction ||= Transaction.new(**options.merge(hub: self))
sampling_context = {
transaction_context: transaction.to_hash,
parent_sampled: transaction.parent_sampled
}
sampling_context.merge!(custom_sampling_context)
transaction.set_initial_sample_decision(sampling_context: sampling_context)
transaction
end
def capture_exception(exception, **options, &block)
check_argument_type!(exception, ::Exception)
return unless current_client
options[:hint] ||= {}
options[:hint][:exception] = exception
event = current_client.event_from_exception(exception, options[:hint])
return unless event
capture_event(event, **options, &block)
end
def capture_message(message, **options, &block)
check_argument_type!(message, ::String)
return unless current_client
options[:hint] ||= {}
options[:hint][:message] = message
backtrace = options.delete(:backtrace)
event = current_client.event_from_message(message, options[:hint], backtrace: backtrace)
capture_event(event, **options, &block)
end
def capture_event(event, **options, &block)
check_argument_type!(event, Sentry::Event)
return unless current_client
hint = options.delete(:hint) || {}
scope = current_scope.dup
if block
block.call(scope)
elsif custom_scope = options[:scope]
scope.update_from_scope(custom_scope)
elsif !options.empty?
scope.update_from_options(**options)
end
event = current_client.capture_event(event, scope, hint)
@last_event_id = event&.event_id
event
end
def add_breadcrumb(breadcrumb, hint: {})
return unless configuration.enabled_in_current_env?
if before_breadcrumb = current_client.configuration.before_breadcrumb
breadcrumb = before_breadcrumb.call(breadcrumb, hint)
end
return unless breadcrumb
current_scope.add_breadcrumb(breadcrumb)
end
# this doesn't do anything to the already initialized background worker
# but it temporarily disables dispatching events to it
def with_background_worker_disabled(&block)
original_background_worker_threads = configuration.background_worker_threads
configuration.background_worker_threads = 0
block.call
ensure
configuration.background_worker_threads = original_background_worker_threads
end
private
def current_layer
@stack.last
end
class Layer
attr_accessor :client
attr_reader :scope
def initialize(client, scope)
@client = client
@scope = scope
end
end
end
end