-
Notifications
You must be signed in to change notification settings - Fork 174
/
Copy pathconfiguration.rb
302 lines (257 loc) · 9.52 KB
/
configuration.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
require "set"
require "socket"
require "logger"
require "bugsnag/middleware_stack"
require "bugsnag/middleware/callbacks"
require "bugsnag/middleware/exception_meta_data"
require "bugsnag/middleware/ignore_error_class"
require "bugsnag/middleware/suggestion_data"
require "bugsnag/middleware/classify_error"
require "bugsnag/middleware/session_data"
require "bugsnag/middleware/breadcrumbs"
require "bugsnag/utility/circular_buffer"
require "bugsnag/breadcrumbs/breadcrumbs"
module Bugsnag
class Configuration
attr_accessor :api_key
attr_accessor :release_stage
attr_accessor :notify_release_stages
attr_accessor :auto_notify
attr_accessor :ca_file
attr_accessor :send_environment
attr_accessor :send_code
attr_accessor :project_root
attr_accessor :app_version
attr_accessor :app_type
attr_accessor :meta_data_filters
attr_accessor :logger
attr_accessor :middleware
attr_accessor :internal_middleware
attr_accessor :proxy_host
attr_accessor :proxy_port
attr_accessor :proxy_user
attr_accessor :proxy_password
attr_accessor :timeout
attr_accessor :hostname
attr_accessor :runtime_versions
attr_accessor :ignore_classes
attr_accessor :auto_capture_sessions
##
# @return [String] URL error notifications will be delivered to
attr_reader :notify_endpoint
alias :endpoint :notify_endpoint
##
# @return [String] URL session notifications will be delivered to
attr_reader :session_endpoint
##
# @return [Boolean] whether any sessions types will be delivered
attr_reader :enable_sessions
##
# @return [Array<String>] strings indicating allowable automatic breadcrumb types
attr_accessor :enabled_automatic_breadcrumb_types
##
# @return [Array<#call>] callables to be run before a breadcrumb is logged
attr_accessor :before_breadcrumb_callbacks
##
# @return [Integer] the maximum allowable amount of breadcrumbs per thread
attr_reader :max_breadcrumbs
API_KEY_REGEX = /[0-9a-f]{32}/i
THREAD_LOCAL_NAME = "bugsnag_req_data"
DEFAULT_NOTIFY_ENDPOINT = "https://notify.bugsnag.com"
DEFAULT_SESSION_ENDPOINT = "https://sessions.bugsnag.com"
DEFAULT_ENDPOINT = DEFAULT_NOTIFY_ENDPOINT
DEFAULT_META_DATA_FILTERS = [
/authorization/i,
/cookie/i,
/password/i,
/secret/i,
/warden\.user\.([^.]+)\.key/,
"rack.request.form_vars"
].freeze
DEFAULT_MAX_BREADCRUMBS = 25
alias :track_sessions :auto_capture_sessions
alias :track_sessions= :auto_capture_sessions=
def initialize
@mutex = Mutex.new
# Set up the defaults
self.auto_notify = true
self.send_environment = false
self.send_code = true
self.meta_data_filters = Set.new(DEFAULT_META_DATA_FILTERS)
self.hostname = default_hostname
self.runtime_versions = {}
self.runtime_versions["ruby"] = RUBY_VERSION
self.runtime_versions["jruby"] = JRUBY_VERSION if defined?(JRUBY_VERSION)
self.timeout = 15
self.notify_release_stages = nil
self.auto_capture_sessions = true
# All valid breadcrumb types should be allowable initially
self.enabled_automatic_breadcrumb_types = Bugsnag::Breadcrumbs::VALID_BREADCRUMB_TYPES.dup
self.before_breadcrumb_callbacks = []
# Store max_breadcrumbs here instead of outputting breadcrumbs.max_items
# to avoid infinite recursion when creating breadcrumb buffer
@max_breadcrumbs = DEFAULT_MAX_BREADCRUMBS
# These are set exclusively using the "set_endpoints" method
@notify_endpoint = DEFAULT_NOTIFY_ENDPOINT
@session_endpoint = DEFAULT_SESSION_ENDPOINT
@enable_sessions = true
# SystemExit and SignalException are common Exception types seen with
# successful exits and are not automatically reported to Bugsnag
self.ignore_classes = Set.new([SystemExit, SignalException])
# Read the API key from the environment
self.api_key = ENV["BUGSNAG_API_KEY"]
# Read NET::HTTP proxy environment variables
if (proxy_uri = ENV["https_proxy"] || ENV['http_proxy'])
parse_proxy(proxy_uri)
end
# Set up logging
self.logger = Logger.new(STDOUT)
self.logger.level = Logger::INFO
self.logger.formatter = proc do |severity, datetime, progname, msg|
"** #{progname} #{datetime}: #{msg}\n"
end
# Configure the bugsnag middleware stack
self.internal_middleware = Bugsnag::MiddlewareStack.new
self.internal_middleware.use Bugsnag::Middleware::ExceptionMetaData
self.internal_middleware.use Bugsnag::Middleware::IgnoreErrorClass
self.internal_middleware.use Bugsnag::Middleware::SuggestionData
self.internal_middleware.use Bugsnag::Middleware::ClassifyError
self.internal_middleware.use Bugsnag::Middleware::SessionData
self.internal_middleware.use Bugsnag::Middleware::Breadcrumbs
self.middleware = Bugsnag::MiddlewareStack.new
self.middleware.use Bugsnag::Middleware::Callbacks
end
##
# Gets the delivery_method that Bugsnag will use to communicate with the
# notification endpoint.
#
def delivery_method
@delivery_method || @default_delivery_method || :thread_queue
end
##
# Sets the delivery_method that Bugsnag will use to communicate with the
# notification endpoint.
#
def delivery_method=(delivery_method)
@delivery_method = delivery_method
end
##
# Used to set a new default delivery method that will be used if one is not
# set with #delivery_method.
#
def default_delivery_method=(delivery_method)
@default_delivery_method = delivery_method
end
##
# Indicates whether the notifier should send a notification based on the
# configured release stage.
def should_notify_release_stage?
@release_stage.nil? || @notify_release_stages.nil? || @notify_release_stages.include?(@release_stage)
end
##
# Tests whether the configured API key is valid.
def valid_api_key?
!api_key.nil? && api_key =~ API_KEY_REGEX
end
##
# Returns the array of data that will be automatically attached to every
# error notification.
def request_data
Thread.current[THREAD_LOCAL_NAME] ||= {}
end
##
# Sets an entry in the array of data attached to every error notification.
def set_request_data(key, value)
self.request_data[key] = value
end
##
# Unsets an entry in the array of data attached to every error notification.
def unset_request_data(key, value)
self.request_data.delete(key)
end
##
# Clears the array of data attached to every error notification.
def clear_request_data
Thread.current[THREAD_LOCAL_NAME] = nil
end
##
# Logs an info level message
def info(message)
logger.info(PROG_NAME) { message }
end
##
# Logs a warning level message
def warn(message)
logger.warn(PROG_NAME) { message }
end
##
# Logs a debug level message
def debug(message)
logger.debug(PROG_NAME) { message }
end
##
# Parses and sets proxy from a uri
def parse_proxy(uri)
proxy = URI.parse(uri)
self.proxy_host = proxy.host
self.proxy_port = proxy.port
self.proxy_user = proxy.user
self.proxy_password = proxy.password
end
##
# Sets the maximum allowable amount of breadcrumbs
#
# @param [Integer] the new maximum breadcrumb limit
def max_breadcrumbs=(new_max_breadcrumbs)
@max_breadcrumbs = new_max_breadcrumbs
breadcrumbs.max_items = new_max_breadcrumbs
end
##
# Returns the breadcrumb circular buffer
#
# @return [Bugsnag::Utility::CircularBuffer] a thread based circular buffer containing breadcrumbs
def breadcrumbs
request_data[:breadcrumbs] ||= Bugsnag::Utility::CircularBuffer.new(@max_breadcrumbs)
end
# Sets the notification endpoint
#
# @param new_notify_endpoint [String] The URL to deliver error notifications to
#
# @deprecated Use {#set_endpoints} instead
def endpoint=(new_notify_endpoint)
warn("The 'endpoint' configuration option is deprecated. The 'set_endpoints' method should be used instead")
set_endpoints(new_notify_endpoint, session_endpoint) # Pass the existing session_endpoint through so it doesn't get overwritten
end
##
# Sets the sessions endpoint
#
# @param new_session_endpoint [String] The URL to deliver session notifications to
#
# @deprecated Use {#set_endpoints} instead
def session_endpoint=(new_session_endpoint)
warn("The 'session_endpoint' configuration option is deprecated. The 'set_endpoints' method should be used instead")
set_endpoints(notify_endpoint, new_session_endpoint) # Pass the existing notify_endpoint through so it doesn't get overwritten
end
##
# Sets the notification and session endpoints
#
# @param new_notify_endpoint [String] The URL to deliver error notifications to
# @param new_session_endpoint [String] The URL to deliver session notifications to
def set_endpoints(new_notify_endpoint, new_session_endpoint)
@notify_endpoint = new_notify_endpoint
@session_endpoint = new_session_endpoint
end
##
# Disables session tracking and delivery. Cannot be undone
def disable_sessions
self.auto_capture_sessions = false
@enable_sessions = false
end
private
PROG_NAME = "[Bugsnag]"
def default_hostname
# Send the heroku dyno name instead of hostname if available
ENV["DYNO"] || Socket.gethostname;
end
end
end