sentry-ruby 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.craft.yml +18 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +6 -0
- data/CHANGELOG.md +10 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +11 -0
- data/LICENSE.txt +21 -0
- data/README.md +44 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/sentry.rb +97 -0
- data/lib/sentry/backtrace.rb +128 -0
- data/lib/sentry/breadcrumb.rb +25 -0
- data/lib/sentry/breadcrumb/sentry_logger.rb +103 -0
- data/lib/sentry/breadcrumb_buffer.rb +50 -0
- data/lib/sentry/client.rb +85 -0
- data/lib/sentry/configuration.rb +401 -0
- data/lib/sentry/core_ext/object/deep_dup.rb +57 -0
- data/lib/sentry/core_ext/object/duplicable.rb +153 -0
- data/lib/sentry/dsn.rb +45 -0
- data/lib/sentry/event.rb +175 -0
- data/lib/sentry/event/options.rb +31 -0
- data/lib/sentry/hub.rb +126 -0
- data/lib/sentry/interface.rb +22 -0
- data/lib/sentry/interfaces/exception.rb +11 -0
- data/lib/sentry/interfaces/request.rb +104 -0
- data/lib/sentry/interfaces/single_exception.rb +14 -0
- data/lib/sentry/interfaces/stacktrace.rb +57 -0
- data/lib/sentry/linecache.rb +44 -0
- data/lib/sentry/logger.rb +20 -0
- data/lib/sentry/rack.rb +4 -0
- data/lib/sentry/rack/capture_exception.rb +45 -0
- data/lib/sentry/ruby.rb +1 -0
- data/lib/sentry/scope.rb +192 -0
- data/lib/sentry/transport.rb +110 -0
- data/lib/sentry/transport/configuration.rb +28 -0
- data/lib/sentry/transport/dummy_transport.rb +14 -0
- data/lib/sentry/transport/http_transport.rb +62 -0
- data/lib/sentry/transport/state.rb +40 -0
- data/lib/sentry/utils/deep_merge.rb +22 -0
- data/lib/sentry/utils/exception_cause_chain.rb +20 -0
- data/lib/sentry/utils/real_ip.rb +70 -0
- data/lib/sentry/version.rb +3 -0
- data/sentry-ruby.gemspec +26 -0
- metadata +107 -0
@@ -0,0 +1,25 @@
|
|
1
|
+
module Sentry
|
2
|
+
class Breadcrumb
|
3
|
+
attr_accessor :category, :data, :message, :level, :timestamp, :type
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@category = nil
|
7
|
+
@data = {}
|
8
|
+
@level = nil
|
9
|
+
@message = nil
|
10
|
+
@timestamp = Time.now.to_i
|
11
|
+
@type = nil
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_hash
|
15
|
+
{
|
16
|
+
:category => @category,
|
17
|
+
:data => @data,
|
18
|
+
:level => @level,
|
19
|
+
:message => @message,
|
20
|
+
:timestamp => @timestamp,
|
21
|
+
:type => @type
|
22
|
+
}
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
require 'logger'
|
2
|
+
|
3
|
+
module Sentry
|
4
|
+
class Breadcrumb
|
5
|
+
module SentryLogger
|
6
|
+
LEVELS = {
|
7
|
+
::Logger::DEBUG => 'debug',
|
8
|
+
::Logger::INFO => 'info',
|
9
|
+
::Logger::WARN => 'warn',
|
10
|
+
::Logger::ERROR => 'error',
|
11
|
+
::Logger::FATAL => 'fatal'
|
12
|
+
}.freeze
|
13
|
+
|
14
|
+
def add(*args, &block)
|
15
|
+
super
|
16
|
+
add_breadcrumb(*args, &block)
|
17
|
+
end
|
18
|
+
|
19
|
+
def add_breadcrumb(severity, message = nil, progname = nil)
|
20
|
+
# because the breadcrumbs now belongs to different Hub's Scope in different threads
|
21
|
+
# we need to make sure the current thread's Hub has been set before adding breadcrumbs
|
22
|
+
return unless Sentry.get_current_hub
|
23
|
+
|
24
|
+
category = "logger"
|
25
|
+
|
26
|
+
# this is because the nature of Ruby Logger class:
|
27
|
+
#
|
28
|
+
# when given 1 argument, the argument will become both message and progname
|
29
|
+
#
|
30
|
+
# ```
|
31
|
+
# logger.info("foo")
|
32
|
+
# # message == progname == "foo"
|
33
|
+
# ```
|
34
|
+
#
|
35
|
+
# and to specify progname with a different message,
|
36
|
+
# we need to pass the progname as the argument and pass the message as a proc
|
37
|
+
#
|
38
|
+
# ```
|
39
|
+
# logger.info("progname") { "the message" }
|
40
|
+
# ```
|
41
|
+
#
|
42
|
+
# so the condition below is to replicate the similar behavior
|
43
|
+
if message.nil?
|
44
|
+
if block_given?
|
45
|
+
message = yield
|
46
|
+
category = progname
|
47
|
+
else
|
48
|
+
message = progname
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
return if ignored_logger?(progname) || message.empty?
|
53
|
+
|
54
|
+
# some loggers will add leading/trailing space as they (incorrectly, mind you)
|
55
|
+
# think of logging as a shortcut to std{out,err}
|
56
|
+
message = message.to_s.strip
|
57
|
+
|
58
|
+
last_crumb = current_breadcrumbs.peek
|
59
|
+
# try to avoid dupes from logger broadcasts
|
60
|
+
if last_crumb.nil? || last_crumb.message != message
|
61
|
+
current_breadcrumbs.record do |crumb|
|
62
|
+
crumb.level = Sentry::Breadcrumb::SentryLogger::LEVELS.fetch(severity, nil)
|
63
|
+
crumb.category = category
|
64
|
+
crumb.message = message
|
65
|
+
crumb.type =
|
66
|
+
if severity >= 3
|
67
|
+
"error"
|
68
|
+
else
|
69
|
+
crumb.level
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
private
|
76
|
+
|
77
|
+
def ignored_logger?(progname)
|
78
|
+
progname == LOGGER_PROGNAME ||
|
79
|
+
Sentry.configuration.exclude_loggers.include?(progname)
|
80
|
+
end
|
81
|
+
|
82
|
+
def current_breadcrumbs
|
83
|
+
Sentry.breadcrumbs
|
84
|
+
end
|
85
|
+
end
|
86
|
+
module OldBreadcrumbsSentryLogger
|
87
|
+
def self.included(base)
|
88
|
+
base.class_eval do
|
89
|
+
include Sentry::Breadcrumbs::SentryLogger
|
90
|
+
alias_method :add_without_sentry, :add
|
91
|
+
alias_method :add, :add_with_sentry
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def add_with_sentry(*args)
|
96
|
+
add_breadcrumb(*args)
|
97
|
+
add_without_sentry(*args)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
::Logger.send(:prepend, Sentry::Breadcrumb::SentryLogger)
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require "sentry/breadcrumb"
|
2
|
+
|
3
|
+
module Sentry
|
4
|
+
class BreadcrumbBuffer
|
5
|
+
include Enumerable
|
6
|
+
|
7
|
+
attr_accessor :buffer
|
8
|
+
|
9
|
+
def initialize(size = 100)
|
10
|
+
@buffer = Array.new(size)
|
11
|
+
end
|
12
|
+
|
13
|
+
def record(crumb = nil)
|
14
|
+
if block_given?
|
15
|
+
crumb = Breadcrumb.new if crumb.nil?
|
16
|
+
yield(crumb)
|
17
|
+
end
|
18
|
+
@buffer.slice!(0)
|
19
|
+
@buffer << crumb
|
20
|
+
end
|
21
|
+
|
22
|
+
def members
|
23
|
+
@buffer.compact
|
24
|
+
end
|
25
|
+
|
26
|
+
def peek
|
27
|
+
members.last
|
28
|
+
end
|
29
|
+
|
30
|
+
def each(&block)
|
31
|
+
members.each(&block)
|
32
|
+
end
|
33
|
+
|
34
|
+
def empty?
|
35
|
+
members.none?
|
36
|
+
end
|
37
|
+
|
38
|
+
def to_hash
|
39
|
+
{
|
40
|
+
:values => members.map(&:to_hash)
|
41
|
+
}
|
42
|
+
end
|
43
|
+
|
44
|
+
def dup
|
45
|
+
copy = super
|
46
|
+
copy.buffer = buffer.deep_dup
|
47
|
+
copy
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
require "sentry/transport"
|
2
|
+
require 'sentry/utils/deep_merge'
|
3
|
+
|
4
|
+
module Sentry
|
5
|
+
class Client
|
6
|
+
attr_reader :transport, :configuration
|
7
|
+
|
8
|
+
def initialize(configuration)
|
9
|
+
@configuration = configuration
|
10
|
+
|
11
|
+
if transport_class = configuration.transport.transport_class
|
12
|
+
@transport = transport_class.new(configuration)
|
13
|
+
else
|
14
|
+
@transport =
|
15
|
+
case configuration.dsn&.scheme
|
16
|
+
when 'http', 'https'
|
17
|
+
HTTPTransport.new(configuration)
|
18
|
+
else
|
19
|
+
DummyTransport.new(configuration)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def capture_exception(exception, scope:, **options, &block)
|
25
|
+
event = event_from_exception(exception, **options)
|
26
|
+
return unless event
|
27
|
+
|
28
|
+
block.call(event) if block
|
29
|
+
capture_event(event, scope)
|
30
|
+
end
|
31
|
+
|
32
|
+
def capture_message(message, scope:, **options, &block)
|
33
|
+
event = event_from_message(message, **options)
|
34
|
+
block.call(event) if block
|
35
|
+
capture_event(event, scope)
|
36
|
+
end
|
37
|
+
|
38
|
+
def capture_event(event, scope)
|
39
|
+
scope.apply_to_event(event)
|
40
|
+
send_event(event)
|
41
|
+
event
|
42
|
+
end
|
43
|
+
|
44
|
+
def event_from_exception(exception, **options)
|
45
|
+
exception_context =
|
46
|
+
if exception.instance_variable_defined?(:@__sentry_context)
|
47
|
+
exception.instance_variable_get(:@__sentry_context)
|
48
|
+
elsif exception.respond_to?(:sentry_context)
|
49
|
+
exception.sentry_context
|
50
|
+
else
|
51
|
+
{}
|
52
|
+
end
|
53
|
+
|
54
|
+
options = Utils::DeepMergeHash.deep_merge(exception_context, options)
|
55
|
+
|
56
|
+
return unless @configuration.exception_class_allowed?(exception)
|
57
|
+
|
58
|
+
options = Event::Options.new(**options)
|
59
|
+
|
60
|
+
Event.new(configuration: configuration, options: options).tap do |event|
|
61
|
+
event.add_exception_interface(exception)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def event_from_message(message, **options)
|
66
|
+
options.merge!(message: message)
|
67
|
+
options = Event::Options.new(options)
|
68
|
+
Event.new(configuration: configuration, options: options)
|
69
|
+
end
|
70
|
+
|
71
|
+
def send_event(event, hint = nil)
|
72
|
+
return false unless configuration.sending_allowed?(event)
|
73
|
+
|
74
|
+
event = configuration.before_send.call(event, hint) if configuration.before_send
|
75
|
+
if event.nil?
|
76
|
+
configuration.logger.info(LOGGER_PROGNAME) { "Discarded event because before_send returned nil" }
|
77
|
+
return
|
78
|
+
end
|
79
|
+
|
80
|
+
transport.send_event(event)
|
81
|
+
|
82
|
+
event
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,401 @@
|
|
1
|
+
require "sentry/utils/exception_cause_chain"
|
2
|
+
require "sentry/dsn"
|
3
|
+
require "sentry/transport/configuration"
|
4
|
+
require "sentry/linecache"
|
5
|
+
|
6
|
+
module Sentry
|
7
|
+
class Configuration
|
8
|
+
# Directories to be recognized as part of your app. e.g. if you
|
9
|
+
# have an `engines` dir at the root of your project, you may want
|
10
|
+
# to set this to something like /(app|config|engines|lib)/
|
11
|
+
attr_accessor :app_dirs_pattern
|
12
|
+
|
13
|
+
# Provide an object that responds to `call` to send events asynchronously.
|
14
|
+
# E.g.: lambda { |event| Thread.new { Sentry.send_event(event) } }
|
15
|
+
attr_reader :async
|
16
|
+
alias async? async
|
17
|
+
|
18
|
+
# An array of breadcrumbs loggers to be used. Available options are:
|
19
|
+
# - :sentry_logger
|
20
|
+
# - :active_support_logger
|
21
|
+
attr_reader :breadcrumbs_logger
|
22
|
+
|
23
|
+
# Number of lines of code context to capture, or nil for none
|
24
|
+
attr_accessor :context_lines
|
25
|
+
|
26
|
+
# RACK_ENV by default.
|
27
|
+
attr_reader :current_environment
|
28
|
+
|
29
|
+
# Whitelist of environments that will send notifications to Sentry. Array of Strings.
|
30
|
+
attr_accessor :environments
|
31
|
+
|
32
|
+
# Logger 'progname's to exclude from breadcrumbs
|
33
|
+
attr_accessor :exclude_loggers
|
34
|
+
|
35
|
+
# Array of exception classes that should never be sent. See IGNORE_DEFAULT.
|
36
|
+
# You should probably append to this rather than overwrite it.
|
37
|
+
attr_accessor :excluded_exceptions
|
38
|
+
|
39
|
+
# Boolean to check nested exceptions when deciding if to exclude. Defaults to false
|
40
|
+
attr_accessor :inspect_exception_causes_for_exclusion
|
41
|
+
alias inspect_exception_causes_for_exclusion? inspect_exception_causes_for_exclusion
|
42
|
+
|
43
|
+
# You may provide your own LineCache for matching paths with source files.
|
44
|
+
# This may be useful if you need to get source code from places other than
|
45
|
+
# the disk. See Sentry::LineCache for the required interface you must implement.
|
46
|
+
attr_accessor :linecache
|
47
|
+
|
48
|
+
# Logger used by Sentry. In Rails, this is the Rails logger, otherwise
|
49
|
+
# Sentry provides its own Sentry::Logger.
|
50
|
+
attr_accessor :logger
|
51
|
+
|
52
|
+
# Project directory root for in_app detection. Could be Rails root, etc.
|
53
|
+
# Set automatically for Rails.
|
54
|
+
attr_reader :project_root
|
55
|
+
|
56
|
+
# Array of rack env parameters to be included in the event sent to sentry.
|
57
|
+
attr_accessor :rack_env_whitelist
|
58
|
+
|
59
|
+
# Release tag to be passed with every event sent to Sentry.
|
60
|
+
# We automatically try to set this to a git SHA or Capistrano release.
|
61
|
+
attr_accessor :release
|
62
|
+
|
63
|
+
# The sampling factor to apply to events. A value of 0.0 will not send
|
64
|
+
# any events, and a value of 1.0 will send 100% of events.
|
65
|
+
attr_accessor :sample_rate
|
66
|
+
|
67
|
+
# a proc/lambda that takes an array of stack traces
|
68
|
+
# it'll be used to silence (reduce) backtrace of the exception
|
69
|
+
#
|
70
|
+
# for example:
|
71
|
+
#
|
72
|
+
# ```ruby
|
73
|
+
# Sentry.configuration.backtrace_cleanup_callback = lambda do |backtrace|
|
74
|
+
# Rails.backtrace_cleaner.clean(backtrace)
|
75
|
+
# end
|
76
|
+
# ```
|
77
|
+
#
|
78
|
+
attr_accessor :backtrace_cleanup_callback
|
79
|
+
|
80
|
+
# Include module versions in reports - boolean.
|
81
|
+
attr_accessor :send_modules
|
82
|
+
|
83
|
+
attr_accessor :send_default_pii
|
84
|
+
|
85
|
+
attr_accessor :server_name
|
86
|
+
|
87
|
+
# Provide a configurable callback to determine event capture.
|
88
|
+
# Note that the object passed into the block will be a String (messages) or
|
89
|
+
# an exception.
|
90
|
+
# e.g. lambda { |exc_or_msg| exc_or_msg.some_attr == false }
|
91
|
+
attr_reader :should_capture
|
92
|
+
|
93
|
+
# Silences ready message when true.
|
94
|
+
attr_accessor :silence_ready
|
95
|
+
|
96
|
+
# Default tags for events. Hash.
|
97
|
+
attr_accessor :tags
|
98
|
+
|
99
|
+
attr_reader :transport
|
100
|
+
|
101
|
+
# Optional Proc, called before sending an event to the server/
|
102
|
+
# E.g.: lambda { |event, hint| event }
|
103
|
+
# E.g.: lambda { |event, hint| nil }
|
104
|
+
# E.g.: lambda { |event, hint|
|
105
|
+
# event[:message] = 'a'
|
106
|
+
# event
|
107
|
+
# }
|
108
|
+
attr_reader :before_send
|
109
|
+
|
110
|
+
# Errors object - an Array that contains error messages. See #
|
111
|
+
attr_reader :errors
|
112
|
+
|
113
|
+
# the dsn value, whether it's set via `config.dsn=` or `ENV["SENTRY_DSN"]`
|
114
|
+
attr_reader :dsn
|
115
|
+
|
116
|
+
# Most of these errors generate 4XX responses. In general, Sentry clients
|
117
|
+
# only automatically report 5xx responses.
|
118
|
+
IGNORE_DEFAULT = [
|
119
|
+
'CGI::Session::CookieStore::TamperedWithCookie',
|
120
|
+
'Mongoid::Errors::DocumentNotFound',
|
121
|
+
'Rack::QueryParser::InvalidParameterError',
|
122
|
+
'Rack::QueryParser::ParameterTypeError',
|
123
|
+
'Sinatra::NotFound'
|
124
|
+
].freeze
|
125
|
+
|
126
|
+
RACK_ENV_WHITELIST_DEFAULT = %w(
|
127
|
+
REMOTE_ADDR
|
128
|
+
SERVER_NAME
|
129
|
+
SERVER_PORT
|
130
|
+
).freeze
|
131
|
+
|
132
|
+
HEROKU_DYNO_METADATA_MESSAGE = "You are running on Heroku but haven't enabled Dyno Metadata. For Sentry's "\
|
133
|
+
"release detection to work correctly, please run `heroku labs:enable runtime-dyno-metadata`".freeze
|
134
|
+
|
135
|
+
LOG_PREFIX = "** [Sentry] ".freeze
|
136
|
+
MODULE_SEPARATOR = "::".freeze
|
137
|
+
|
138
|
+
AVAILABLE_BREADCRUMBS_LOGGERS = [:sentry_logger, :active_support_logger].freeze
|
139
|
+
|
140
|
+
def initialize
|
141
|
+
self.async = false
|
142
|
+
self.breadcrumbs_logger = []
|
143
|
+
self.context_lines = 3
|
144
|
+
self.current_environment = current_environment_from_env
|
145
|
+
self.environments = []
|
146
|
+
self.exclude_loggers = []
|
147
|
+
self.excluded_exceptions = IGNORE_DEFAULT.dup
|
148
|
+
self.inspect_exception_causes_for_exclusion = false
|
149
|
+
self.linecache = ::Sentry::LineCache.new
|
150
|
+
self.logger = ::Sentry::Logger.new(STDOUT)
|
151
|
+
self.project_root = detect_project_root
|
152
|
+
|
153
|
+
self.release = detect_release
|
154
|
+
self.sample_rate = 1.0
|
155
|
+
self.send_modules = true
|
156
|
+
self.send_default_pii = false
|
157
|
+
self.dsn = ENV['SENTRY_DSN']
|
158
|
+
self.server_name = server_name_from_env
|
159
|
+
self.should_capture = false
|
160
|
+
self.tags = {}
|
161
|
+
@transport = Transport::Configuration.new
|
162
|
+
self.before_send = false
|
163
|
+
self.rack_env_whitelist = RACK_ENV_WHITELIST_DEFAULT
|
164
|
+
post_initialization_callback
|
165
|
+
end
|
166
|
+
|
167
|
+
def dsn=(value)
|
168
|
+
return if value.nil? || value.empty?
|
169
|
+
|
170
|
+
@dsn = DSN.new(value)
|
171
|
+
end
|
172
|
+
|
173
|
+
alias server= dsn=
|
174
|
+
|
175
|
+
|
176
|
+
def async=(value)
|
177
|
+
unless value == false || value.respond_to?(:call)
|
178
|
+
raise(ArgumentError, "async must be callable (or false to disable)")
|
179
|
+
end
|
180
|
+
|
181
|
+
@async = value
|
182
|
+
end
|
183
|
+
|
184
|
+
def breadcrumbs_logger=(logger)
|
185
|
+
loggers =
|
186
|
+
if logger.is_a?(Array)
|
187
|
+
logger
|
188
|
+
else
|
189
|
+
unless AVAILABLE_BREADCRUMBS_LOGGERS.include?(logger)
|
190
|
+
raise Sentry::Error, "Unsupported breadcrumbs logger. Supported loggers: #{AVAILABLE_BREADCRUMBS_LOGGERS}"
|
191
|
+
end
|
192
|
+
|
193
|
+
Array(logger)
|
194
|
+
end
|
195
|
+
|
196
|
+
require "sentry/breadcrumb/sentry_logger" if loggers.include?(:sentry_logger)
|
197
|
+
|
198
|
+
@breadcrumbs_logger = logger
|
199
|
+
end
|
200
|
+
|
201
|
+
def should_capture=(value)
|
202
|
+
unless value == false || value.respond_to?(:call)
|
203
|
+
raise ArgumentError, "should_capture must be callable (or false to disable)"
|
204
|
+
end
|
205
|
+
|
206
|
+
@should_capture = value
|
207
|
+
end
|
208
|
+
|
209
|
+
def before_send=(value)
|
210
|
+
unless value == false || value.respond_to?(:call)
|
211
|
+
raise ArgumentError, "before_send must be callable (or false to disable)"
|
212
|
+
end
|
213
|
+
|
214
|
+
@before_send = value
|
215
|
+
end
|
216
|
+
|
217
|
+
# Allows config options to be read like a hash
|
218
|
+
#
|
219
|
+
# @param [Symbol] option Key for a given attribute
|
220
|
+
def [](option)
|
221
|
+
public_send(option)
|
222
|
+
end
|
223
|
+
|
224
|
+
def current_environment=(environment)
|
225
|
+
@current_environment = environment.to_s
|
226
|
+
end
|
227
|
+
|
228
|
+
def capture_allowed?(message_or_exc = nil)
|
229
|
+
@errors = []
|
230
|
+
|
231
|
+
valid? &&
|
232
|
+
capture_in_current_environment? &&
|
233
|
+
capture_allowed_by_callback?(message_or_exc) &&
|
234
|
+
sample_allowed?
|
235
|
+
end
|
236
|
+
# If we cannot capture, we cannot send.
|
237
|
+
alias sending_allowed? capture_allowed?
|
238
|
+
|
239
|
+
def error_messages
|
240
|
+
@errors = [errors[0]] + errors[1..-1].map(&:downcase) # fix case of all but first
|
241
|
+
errors.join(", ")
|
242
|
+
end
|
243
|
+
|
244
|
+
def project_root=(root_dir)
|
245
|
+
@project_root = root_dir
|
246
|
+
end
|
247
|
+
|
248
|
+
def exception_class_allowed?(exc)
|
249
|
+
if exc.is_a?(Sentry::Error)
|
250
|
+
# Try to prevent error reporting loops
|
251
|
+
logger.debug(LOGGER_PROGNAME) { "Refusing to capture Sentry error: #{exc.inspect}" }
|
252
|
+
false
|
253
|
+
elsif excluded_exception?(exc)
|
254
|
+
logger.debug(LOGGER_PROGNAME) { "User excluded error: #{exc.inspect}" }
|
255
|
+
false
|
256
|
+
else
|
257
|
+
true
|
258
|
+
end
|
259
|
+
end
|
260
|
+
|
261
|
+
def enabled_in_current_env?
|
262
|
+
environments.empty? || environments.include?(current_environment)
|
263
|
+
end
|
264
|
+
|
265
|
+
private
|
266
|
+
|
267
|
+
def detect_project_root
|
268
|
+
if defined? Rails.root # we are in a Rails application
|
269
|
+
Rails.root.to_s
|
270
|
+
else
|
271
|
+
Dir.pwd
|
272
|
+
end
|
273
|
+
end
|
274
|
+
|
275
|
+
def detect_release
|
276
|
+
detect_release_from_env ||
|
277
|
+
detect_release_from_git ||
|
278
|
+
detect_release_from_capistrano ||
|
279
|
+
detect_release_from_heroku
|
280
|
+
rescue => e
|
281
|
+
logger.error(LOGGER_PROGNAME) { "Error detecting release: #{e.message}" }
|
282
|
+
end
|
283
|
+
|
284
|
+
def excluded_exception?(incoming_exception)
|
285
|
+
excluded_exceptions.any? do |excluded_exception|
|
286
|
+
matches_exception?(get_exception_class(excluded_exception), incoming_exception)
|
287
|
+
end
|
288
|
+
end
|
289
|
+
|
290
|
+
def get_exception_class(x)
|
291
|
+
x.is_a?(Module) ? x : qualified_const_get(x)
|
292
|
+
end
|
293
|
+
|
294
|
+
def matches_exception?(excluded_exception_class, incoming_exception)
|
295
|
+
if inspect_exception_causes_for_exclusion?
|
296
|
+
Sentry::Utils::ExceptionCauseChain.exception_to_array(incoming_exception).any? { |cause| excluded_exception_class === cause }
|
297
|
+
else
|
298
|
+
excluded_exception_class === incoming_exception
|
299
|
+
end
|
300
|
+
end
|
301
|
+
|
302
|
+
# In Ruby <2.0 const_get can't lookup "SomeModule::SomeClass" in one go
|
303
|
+
def qualified_const_get(x)
|
304
|
+
x = x.to_s
|
305
|
+
if !x.match(/::/)
|
306
|
+
Object.const_get(x)
|
307
|
+
else
|
308
|
+
x.split(MODULE_SEPARATOR).reject(&:empty?).inject(Object) { |a, e| a.const_get(e) }
|
309
|
+
end
|
310
|
+
rescue NameError # There's no way to safely ask if a constant exist for an unknown string
|
311
|
+
nil
|
312
|
+
end
|
313
|
+
|
314
|
+
def detect_release_from_heroku
|
315
|
+
return unless running_on_heroku?
|
316
|
+
return if ENV['CI']
|
317
|
+
logger.warn(LOGGER_PROGNAME) { HEROKU_DYNO_METADATA_MESSAGE } && return unless ENV['HEROKU_SLUG_COMMIT']
|
318
|
+
|
319
|
+
ENV['HEROKU_SLUG_COMMIT']
|
320
|
+
end
|
321
|
+
|
322
|
+
def running_on_heroku?
|
323
|
+
File.directory?("/etc/heroku")
|
324
|
+
end
|
325
|
+
|
326
|
+
def detect_release_from_capistrano
|
327
|
+
revision_file = File.join(project_root, 'REVISION')
|
328
|
+
revision_log = File.join(project_root, '..', 'revisions.log')
|
329
|
+
|
330
|
+
if File.exist?(revision_file)
|
331
|
+
File.read(revision_file).strip
|
332
|
+
elsif File.exist?(revision_log)
|
333
|
+
File.open(revision_log).to_a.last.strip.sub(/.*as release ([0-9]+).*/, '\1')
|
334
|
+
end
|
335
|
+
end
|
336
|
+
|
337
|
+
def detect_release_from_git
|
338
|
+
Sentry.sys_command("git rev-parse --short HEAD") if File.directory?(".git")
|
339
|
+
end
|
340
|
+
|
341
|
+
def detect_release_from_env
|
342
|
+
ENV['SENTRY_RELEASE']
|
343
|
+
end
|
344
|
+
|
345
|
+
def capture_in_current_environment?
|
346
|
+
return true if enabled_in_current_env?
|
347
|
+
|
348
|
+
@errors << "Not configured to send/capture in environment '#{current_environment}'"
|
349
|
+
false
|
350
|
+
end
|
351
|
+
|
352
|
+
def capture_allowed_by_callback?(message_or_exc)
|
353
|
+
return true if !should_capture || message_or_exc.nil? || should_capture.call(message_or_exc)
|
354
|
+
|
355
|
+
@errors << "should_capture returned false"
|
356
|
+
false
|
357
|
+
end
|
358
|
+
|
359
|
+
def valid?
|
360
|
+
if @dsn&.valid?
|
361
|
+
true
|
362
|
+
else
|
363
|
+
@errors << "DSN not set or not valid"
|
364
|
+
false
|
365
|
+
end
|
366
|
+
end
|
367
|
+
|
368
|
+
def sample_allowed?
|
369
|
+
return true if sample_rate == 1.0
|
370
|
+
|
371
|
+
if Random::DEFAULT.rand >= sample_rate
|
372
|
+
@errors << "Excluded by random sample"
|
373
|
+
false
|
374
|
+
else
|
375
|
+
true
|
376
|
+
end
|
377
|
+
end
|
378
|
+
|
379
|
+
# Try to resolve the hostname to an FQDN, but fall back to whatever
|
380
|
+
# the load name is.
|
381
|
+
def resolve_hostname
|
382
|
+
Socket.gethostname ||
|
383
|
+
Socket.gethostbyname(hostname).first rescue server_name
|
384
|
+
end
|
385
|
+
|
386
|
+
def current_environment_from_env
|
387
|
+
ENV['SENTRY_CURRENT_ENV'] || ENV['SENTRY_ENVIRONMENT'] || ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'default'
|
388
|
+
end
|
389
|
+
|
390
|
+
def server_name_from_env
|
391
|
+
if running_on_heroku?
|
392
|
+
ENV['DYNO']
|
393
|
+
else
|
394
|
+
resolve_hostname
|
395
|
+
end
|
396
|
+
end
|
397
|
+
|
398
|
+
# allow extensions to extend the Configuration class
|
399
|
+
def post_initialization_callback; end
|
400
|
+
end
|
401
|
+
end
|