-
Notifications
You must be signed in to change notification settings - Fork 280
/
rollbar.rb
202 lines (166 loc) · 5.83 KB
/
rollbar.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
require 'net/protocol'
require 'net/https'
require 'socket'
require 'uri'
require 'open-uri'
require 'forwardable'
begin
require 'securerandom'
rescue LoadError
# Skip loading
end
require 'rollbar/version'
require 'rollbar/plugins'
require 'rollbar/configuration'
require 'rollbar/logger_proxy'
require 'rollbar/exceptions'
require 'rollbar/lazy_store'
require 'rollbar/notifier'
# The Rollbar module. It stores a Rollbar::Notifier per thread and
# provides some module methods in order to use the current thread notifier.
module Rollbar
PUBLIC_NOTIFIER_METHODS = %w[debug info warn warning error critical log logger
process_item process_from_async_handler scope
send_failsafe log_info log_debug log_warning
log_error silenced scope_object].freeze
class << self
extend Forwardable
def_delegators :notifier, *PUBLIC_NOTIFIER_METHODS
attr_writer :plugins, :root_notifier
def notifier
# Use the global instance @root_notifier so we don't fall
# in a infinite loop
Thread.current[:_rollbar_notifier] ||= Notifier.new(@root_notifier)
end
def notifier=(notifier)
Thread.current[:_rollbar_notifier] = notifier
end
# It's the first notifier instantiated in the
# process. We store it so all the next per-thread
# notifiers can inherit its configuration
# The methods Rollbar.configure, Rollbar.reconfigure,
# Rollbar.preconfigure and Rollbar.unconfigure work
# on this notifier.
# Before v2.13.0 these methods worked on the global
# configuration, so in the practice the behavior is the same,
# since they work on the root notifier's configuration
def root_notifier
@root_notifier ||= notifier
end
def preconfigure(&block)
root_notifier.preconfigure(&block)
end
# Configures the root notifier and loads the plugins
def configure(&block)
root_notifier.configure(&block)
plugins.load!
end
# Reconfigures the root notifier
def reconfigure(&block)
root_notifier.reconfigure(&block)
end
# Unconfigures the root notifier
def unconfigure
root_notifier.unconfigure
end
# Returns the configuration for the current notifier.
# The current notifier is Rollbar.notifier and exists
# one per thread.
def configuration
notifier.configuration
end
def safely?
configuration.safely?
end
def plugins
@plugins ||= Rollbar::Plugins.new
end
def last_report
Thread.current[:_rollbar_last_report]
end
def last_report=(report)
Thread.current[:_rollbar_last_report] = report
end
# Resets the scope for the current thread notifier. The notifier
# reference is kept so we reuse the notifier.
# This is a change from version 2.13.0. Before this version
# this method clears the notifier.
#
# It was used in order to reset the scope and reusing the global
# configuration Rollbar.configuration. Since now Rollbar.configuration
# points to the current notifier configuration, we can resue the
# notifier instance and just reset the scope.
def reset_notifier!
notifier.reset!
end
# Clears the current thread notifier and the root notifier.
# In the practice this should be used only on the specs
def clear_notifier!
self.notifier = nil
self.root_notifier = nil
end
# Create a new Notifier instance using the received options and
# set it as the current thread notifier.
# The calls to Rollbar inside the received block will use then this
# new Notifier object.
#
# @example
#
# new_scope = { job_type: 'scheduled' }
# new_config = { use_async: false }
#
# Rollbar.scoped(new_scope, new_config) do
# begin
# # do stuff
# rescue => e
# Rollbar.error(e)
# end
# end
def scoped(options = {}, config_overrides = {})
old_notifier = notifier
self.notifier = old_notifier.scope(options, config_overrides)
result = yield
result
ensure
self.notifier = old_notifier
end
# Create a new Notifier instance with a new configuration
# using the current one but merging the passed options.
def with_config(overrides, &block)
scoped(nil, overrides, &block)
end
def scope!(options = {})
notifier.scope!(options)
end
# Backwards compatibility methods
def report_exception(exception, request_data = nil, person_data = nil,
level = 'error')
Kernel.warn('[DEPRECATION] Rollbar.report_exception has been deprecated, ' \
'please use log() or one of the level functions')
scope = {}
scope[:request] = request_data if request_data
scope[:person] = person_data if person_data
Rollbar.scoped(scope) do
Rollbar.notifier.log(level, exception, :use_exception_level_filters => true)
end
end
def report_message(message, level = 'info', extra_data = nil)
Kernel.warn('[DEPRECATION] Rollbar.report_message has been deprecated, ' \
'please use log() or one of the level functions')
Rollbar.notifier.log(level, message, extra_data)
end
def report_message_with_request(message, level = 'info', request_data = nil,
person_data = nil, extra_data = nil)
Kernel.warn('[DEPRECATION] Rollbar.report_message_with_request has been ' \
'deprecated, please use log() or one of the level functions')
scope = {}
scope[:request] = request_data if request_data
scope[:person] = person_data if person_data
Rollbar.scoped(:request => request_data, :person => person_data) do
Rollbar.notifier.log(level, message, extra_data)
end
end
end
self.root_notifier = nil
end
Rollbar.plugins.require_all