-
Notifications
You must be signed in to change notification settings - Fork 145
/
config.rb
494 lines (401 loc) · 12.8 KB
/
config.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
require 'pathname'
require 'delegate'
require 'logger'
require 'fileutils'
require 'openssl'
require 'honeybadger/version'
require 'honeybadger/logging'
require 'honeybadger/backend'
require 'honeybadger/config/defaults'
require 'honeybadger/util/http'
require 'honeybadger/util/revision'
require 'honeybadger/logging'
module Honeybadger
# @api private
# The Config class is used to manage Honeybadger's initialization and
# configuration.
class Config
extend Forwardable
include Logging::Helper
class ConfigError < StandardError; end
# Config subclasses have circular dependencies, so they must be loaded
# after constants are defined.
autoload :Env, 'honeybadger/config/env'
autoload :Yaml, 'honeybadger/config/yaml'
autoload :Ruby, 'honeybadger/config/ruby'
KEY_REPLACEMENT = Regexp.new('[^a-z\d_]', Regexp::IGNORECASE).freeze
DOTTED_KEY = Regexp.new('\A([^\.]+)\.(.+)\z').freeze
NOT_BLANK = Regexp.new('\S').freeze
IVARS = [:@ruby, :@env, :@yaml, :@framework].freeze
def initialize(opts = {})
@ruby = opts.freeze
@env = {}.freeze
@yaml = {}.freeze
@framework = {}.freeze
end
attr_accessor :ruby, :env, :yaml, :framework
# Called by framework (see lib/honeybadger/init/) at the point of
# initialization. This is not required for the notifier to work (i.e. with
# `require 'honeybadger/ruby'`).
def init!(opts = {}, env = ENV)
load!(framework: opts, env: env)
init_logging!
init_backend!
logger.debug(sprintf('Initializing Honeybadger Error Tracker for Ruby. Ship it! version=%s framework=%s', Honeybadger::VERSION, detected_framework))
logger.warn('Development mode is enabled. Data will not be reported until you deploy your app.') if warn_development?
self
end
def load!(framework: {}, env: ENV)
return self if @loaded
self.framework = framework.freeze
self.env = Env.new(env).freeze
load_config_from_disk {|yaml| self.yaml = yaml.freeze }
detect_revision!
@loaded = true
self
end
def configure
new_ruby = Ruby.new(self)
yield(new_ruby)
self.ruby = ruby.merge(new_ruby).freeze
@logger = @backend = nil
self
end
def backtrace_filter(&block)
if block_given?
warn('DEPRECATED: backtrace_filter is deprecated. Please use before_notify instead. See https://docs.honeybadger.io/ruby/support/v4-upgrade#backtrace_filter')
self[:backtrace_filter] = block
end
self[:backtrace_filter]
end
def before_notify_hooks
(ruby[:before_notify] || []).clone
end
def before_event_hooks
(ruby[:before_event] || []).clone
end
def exception_filter(&block)
if block_given?
warn('DEPRECATED: exception_filter is deprecated. Please use before_notify instead. See https://docs.honeybadger.io/ruby/support/v4-upgrade#exception_filter')
self[:exception_filter] = block
end
self[:exception_filter]
end
def exception_fingerprint(&block)
if block_given?
warn('DEPRECATED: exception_fingerprint is deprecated. Please use before_notify instead. See https://docs.honeybadger.io/ruby/support/v4-upgrade#exception_fingerprint')
self[:exception_fingerprint] = block
end
self[:exception_fingerprint]
end
def get(key)
IVARS.each do |var|
source = instance_variable_get(var)
if source.has_key?(key)
return source[key]
end
end
DEFAULTS[key]
end
alias [] :get
def set(key, value)
self.ruby = ruby.merge(key => value).freeze
@logger = @backend = nil
end
alias []= :set
def to_hash(defaults = false)
hash = [:@ruby, :@env, :@yaml, :@framework].reverse.reduce({}) do |a,e|
a.merge!(instance_variable_get(e))
end
hash = DEFAULTS.merge(hash) if defaults
undotify_keys(hash.select {|k,v| DEFAULTS.has_key?(k) })
end
alias to_h to_hash
# Internal Helpers
def logger
init_logging! unless @logger
@logger
end
def backend
init_backend! unless @backend
@backend
end
def backend=(backend)
set(:backend, backend)
@backend = nil
end
def dev?
self[:env] && Array(self[:development_environments]).include?(self[:env])
end
def warn_development?
dev? && backend.kind_of?(Backend::Null)
end
def public?
return true if self[:report_data]
return false if self[:report_data] == false
!self[:env] || !dev?
end
def debug?
!!self[:debug]
end
def log_debug?
return debug? if self[:'logging.debug'].nil?
!!self[:'logging.debug']
end
def ignored_classes
ignore_only = get(:'exceptions.ignore_only')
return ignore_only if ignore_only
return DEFAULTS[:'exceptions.ignore'] unless ignore = get(:'exceptions.ignore')
DEFAULTS[:'exceptions.ignore'] | Array(ignore)
end
def raw_ignored_events
ignore_only = get(:'events.ignore_only')
return ignore_only if ignore_only
return DEFAULTS[:'events.ignore'] unless ignore = get(:'events.ignore')
DEFAULTS[:'events.ignore'] | Array(ignore)
end
def ignored_events
@ignored_events ||= raw_ignored_events.map do |check|
if check.is_a?(String) || check.is_a?(Regexp)
{ [:event_type] => check }
elsif check.is_a?(Hash)
flat_hash(check).transform_keys! { |key_array| key_array.map(&:to_sym) }
end
end.compact
end
def ca_bundle_path
if self[:'connection.system_ssl_cert_chain'] && File.exist?(OpenSSL::X509::DEFAULT_CERT_FILE)
OpenSSL::X509::DEFAULT_CERT_FILE
elsif self[:'connection.ssl_ca_bundle_path']
self[:'connection.ssl_ca_bundle_path']
else
local_cert_path
end
end
def local_cert_path
File.expand_path(File.join('..', '..', '..', 'resources', 'ca-bundle.crt'), __FILE__)
end
def connection_port
if self[:'connection.port']
self[:'connection.port']
elsif self[:'connection.secure']
443
else
80
end
end
def connection_protocol
if self[:'connection.secure']
'https'
else
'http'
end
end
def max_queue_size
self[:max_queue_size]
end
def events_max_queue_size
self[:'events.max_queue_size']
end
def events_batch_size
self[:'events.batch_size']
end
def events_timeout
self[:'events.timeout']
end
def params_filters
Array(self[:'request.filter_keys'])
end
def excluded_request_keys
[].tap do |keys|
keys << :session if self[:'request.disable_session']
keys << :params if self[:'request.disable_params']
keys << :cgi_data if self[:'request.disable_environment']
keys << :url if self[:'request.disable_url']
end
end
def log_level(key = :'logging.level')
case self[key].to_s
when /\A(0|debug)\z/i then Logger::DEBUG
when /\A(1|info)\z/i then Logger::INFO
when /\A(2|warn)\z/i then Logger::WARN
when /\A(3|error)\z/i then Logger::ERROR
else
Logger::INFO
end
end
def load_plugin?(name)
return false if includes_token?(self[:'skipped_plugins'], name)
return true unless self[:plugins].kind_of?(Array)
includes_token?(self[:plugins], name)
end
def insights_enabled?
public? && !!self[:'insights.enabled']
end
def cluster_collection?(name)
return false unless insights_enabled?
return true if self[:"#{name}.insights.cluster_collection"].nil?
!!self[:"#{name}.insights.cluster_collection"]
end
def collection_interval(name)
return nil unless insights_enabled?
self[:"#{name}.insights.collection_interval"]
end
def load_plugin_insights?(name)
return false unless insights_enabled?
return true if self[:"#{name}.insights.enabled"].nil?
!!self[:"#{name}.insights.enabled"]
end
def load_plugin_insights_events?(name)
return false unless insights_enabled?
return false unless load_plugin_insights?(name)
return true if self[:"#{name}.insights.events"].nil?
!!self[:"#{name}.insights.events"]
end
def load_plugin_insights_metrics?(name)
return false unless insights_enabled?
return false unless load_plugin_insights?(name)
return true if self[:"#{name}.insights.metrics"].nil?
!!self[:"#{name}.insights.metrics"]
end
def root_regexp
return @root_regexp if @root_regexp
return nil if @no_root
root = get(:root).to_s
@no_root = true and return nil unless root =~ NOT_BLANK
@root_regexp = Regexp.new("^#{ Regexp.escape(root) }")
end
def detected_framework
if self[:framework] =~ NOT_BLANK
self[:framework].to_sym
elsif defined?(::Rails::VERSION) && ::Rails::VERSION::STRING > '3.0'
:rails
elsif defined?(::Sinatra::VERSION)
:sinatra
elsif defined?(::Hanami::VERSION) && ::Hanami::VERSION >= '2.0'
:hanami
elsif defined?(::Rack.release)
:rack
else
:ruby
end
end
def framework_name
case detected_framework
when :rails then "Rails #{::Rails::VERSION::STRING}"
when :sinatra then "Sinatra #{::Sinatra::VERSION}"
when :hanami then "Hanami #{::Hanami::VERSION}"
when :rack then "Rack #{::Rack.release}"
else
"Ruby #{RUBY_VERSION}"
end
end
private
def detect_revision!
return if self[:revision]
set(:revision, Util::Revision.detect(self[:root]))
end
def log_path
return if log_stdout?
return if !self[:'logging.path']
locate_absolute_path(self[:'logging.path'], self[:root])
end
def config_path
config_paths.first
end
def config_paths
Array(ENV['HONEYBADGER_CONFIG_PATH'] || get(:'config.path')).map do |c|
locate_absolute_path(c, self[:root])
end
end
def default_backend
return Backend::Server.new(self) if public?
Backend::Null.new(self)
end
def init_backend!
if self[:backend].is_a?(String) || self[:backend].is_a?(Symbol)
@backend = Backend.for(self[:backend].to_sym).new(self)
return
end
if ruby[:backend].respond_to?(:notify)
@backend = ruby[:backend]
return
end
if ruby[:backend]
logger.warn(sprintf('Unknown backend: %p; default will be used. Backend must respond to #notify', self[:backend]))
end
@backend = default_backend
end
def build_stdout_logger
logger = Logger.new($stdout)
logger.formatter = lambda do |severity, datetime, progname, msg|
"#{msg}\n"
end
logger.level = log_level
Logging::FormattedLogger.new(logger)
end
def build_file_logger(path)
Logger.new(path).tap do |logger|
logger.level = log_level
logger.formatter = Logger::Formatter.new
end
end
def log_stdout?
self[:'logging.path'] && self[:'logging.path'].to_s.downcase == 'stdout'
end
def build_logger
return ruby[:logger] if ruby[:logger]
return build_stdout_logger if log_stdout?
if path = log_path
FileUtils.mkdir_p(path.dirname) unless path.dirname.writable?
return build_file_logger(path)
end
return framework[:logger] if framework[:logger]
Logger.new(nil)
end
def init_logging!
@logger = Logging::ConfigLogger.new(self, build_logger)
end
# Takes an Array and a value and returns true if the value exists in the
# array in String or Symbol form, otherwise false.
def includes_token?(obj, value)
return false unless obj.kind_of?(Array)
obj.map(&:to_sym).include?(value.to_sym)
end
def locate_absolute_path(path, root)
path = Pathname.new(path.to_s)
if path.absolute?
path
else
Pathname.new(root.to_s).join(path.to_s)
end
end
def load_config_from_disk
if (path = config_paths.find(&:exist?)) && path.file?
Yaml.new(path, self[:env]).tap do |yml|
yield(yml) if block_given?
end
end
end
def undotify_keys(hash)
{}.tap do |new_hash|
hash.each_pair do |k,v|
if k.to_s =~ DOTTED_KEY
new_hash[$1] ||= {}
new_hash[$1] = undotify_keys(new_hash[$1].merge({$2 => v}))
else
new_hash[k.to_s] = v
end
end
end
end
# Converts a nested hash into a single layer where keys become arrays:
# ex: > flat_hash({ :nested => { :hash => "value" }})
# > { [:nested, :hash] => "value" }
def flat_hash(h,f=[],g={})
return g.update({ f=>h }) unless h.is_a? Hash
h.each { |k,r| flat_hash(r,f+[k],g) }
g
end
end
end