sentry-ruby-core 4.4.0 → 5.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +2 -0
  3. data/CHANGELOG.md +12 -0
  4. data/Gemfile +9 -5
  5. data/LICENSE.txt +1 -1
  6. data/README.md +29 -175
  7. data/bin/console +5 -1
  8. data/lib/sentry/background_worker.rb +33 -3
  9. data/lib/sentry/backtrace.rb +1 -3
  10. data/lib/sentry/breadcrumb/sentry_logger.rb +3 -1
  11. data/lib/sentry/breadcrumb.rb +28 -2
  12. data/lib/sentry/breadcrumb_buffer.rb +16 -0
  13. data/lib/sentry/client.rb +66 -7
  14. data/lib/sentry/configuration.rb +156 -112
  15. data/lib/sentry/core_ext/object/deep_dup.rb +4 -0
  16. data/lib/sentry/core_ext/object/duplicable.rb +2 -0
  17. data/lib/sentry/dsn.rb +6 -1
  18. data/lib/sentry/envelope.rb +49 -0
  19. data/lib/sentry/event.rb +65 -23
  20. data/lib/sentry/exceptions.rb +2 -0
  21. data/lib/sentry/hub.rb +37 -6
  22. data/lib/sentry/integrable.rb +2 -0
  23. data/lib/sentry/interface.rb +3 -10
  24. data/lib/sentry/interfaces/exception.rb +13 -3
  25. data/lib/sentry/interfaces/request.rb +52 -21
  26. data/lib/sentry/interfaces/single_exception.rb +31 -0
  27. data/lib/sentry/interfaces/stacktrace.rb +14 -0
  28. data/lib/sentry/interfaces/stacktrace_builder.rb +39 -10
  29. data/lib/sentry/interfaces/threads.rb +12 -2
  30. data/lib/sentry/linecache.rb +3 -0
  31. data/lib/sentry/net/http.rb +79 -51
  32. data/lib/sentry/rack/capture_exceptions.rb +2 -0
  33. data/lib/sentry/rack.rb +2 -1
  34. data/lib/sentry/rake.rb +33 -9
  35. data/lib/sentry/redis.rb +88 -0
  36. data/lib/sentry/release_detector.rb +39 -0
  37. data/lib/sentry/scope.rb +76 -6
  38. data/lib/sentry/span.rb +84 -8
  39. data/lib/sentry/transaction.rb +50 -13
  40. data/lib/sentry/transaction_event.rb +19 -6
  41. data/lib/sentry/transport/configuration.rb +4 -2
  42. data/lib/sentry/transport/dummy_transport.rb +2 -0
  43. data/lib/sentry/transport/http_transport.rb +55 -42
  44. data/lib/sentry/transport.rb +101 -32
  45. data/lib/sentry/utils/argument_checking_helper.rb +2 -0
  46. data/lib/sentry/utils/custom_inspection.rb +14 -0
  47. data/lib/sentry/utils/exception_cause_chain.rb +10 -10
  48. data/lib/sentry/utils/logging_helper.rb +6 -4
  49. data/lib/sentry/utils/real_ip.rb +9 -1
  50. data/lib/sentry/utils/request_id.rb +2 -0
  51. data/lib/sentry/version.rb +3 -1
  52. data/lib/sentry-ruby.rb +247 -47
  53. data/sentry-ruby-core.gemspec +2 -3
  54. data/sentry-ruby.gemspec +2 -3
  55. metadata +10 -22
  56. data/.craft.yml +0 -29
  57. data/lib/sentry/benchmarks/benchmark_transport.rb +0 -14
  58. data/lib/sentry/rack/deprecations.rb +0 -19
data/lib/sentry/client.rb CHANGED
@@ -1,11 +1,22 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "sentry/transport"
2
4
 
3
5
  module Sentry
4
6
  class Client
5
7
  include LoggingHelper
6
8
 
7
- attr_reader :transport, :configuration, :logger
9
+ # The Transport object that'll send events for the client.
10
+ # @return [Transport]
11
+ attr_reader :transport
12
+
13
+ # @!macro configuration
14
+ attr_reader :configuration
8
15
 
16
+ # @deprecated Use Sentry.logger to retrieve the current logger instead.
17
+ attr_reader :logger
18
+
19
+ # @param configuration [Configuration]
9
20
  def initialize(configuration)
10
21
  @configuration = configuration
11
22
  @logger = configuration.logger
@@ -23,15 +34,33 @@ module Sentry
23
34
  end
24
35
  end
25
36
 
37
+ # Applies the given scope's data to the event and sends it to Sentry.
38
+ # @param event [Event] the event to be sent.
39
+ # @param scope [Scope] the scope with contextual data that'll be applied to the event before it's sent.
40
+ # @param hint [Hash] the hint data that'll be passed to `before_send` callback and the scope's event processors.
41
+ # @return [Event, nil]
26
42
  def capture_event(event, scope, hint = {})
27
43
  return unless configuration.sending_allowed?
28
44
 
29
- scope.apply_to_event(event, hint)
45
+ unless event.is_a?(TransactionEvent) || configuration.sample_allowed?
46
+ transport.record_lost_event(:sample_rate, 'event')
47
+ return
48
+ end
49
+
50
+ event_type = event.is_a?(Event) ? event.type : event["type"]
51
+ event = scope.apply_to_event(event, hint)
52
+
53
+ if event.nil?
54
+ log_info("Discarded event because one of the event processors returned nil")
55
+ transport.record_lost_event(:event_processor, event_type)
56
+ return
57
+ end
30
58
 
31
59
  if async_block = configuration.async
32
60
  dispatch_async_event(async_block, event, hint)
33
- elsif hint.fetch(:background, true)
34
- dispatch_background_event(event, hint)
61
+ elsif configuration.background_worker_threads != 0 && hint.fetch(:background, true)
62
+ queued = dispatch_background_event(event, hint)
63
+ transport.record_lost_event(:queue_overflow, event_type) unless queued
35
64
  else
36
65
  send_event(event, hint)
37
66
  end
@@ -42,9 +71,14 @@ module Sentry
42
71
  nil
43
72
  end
44
73
 
74
+ # Initializes an Event object with the given exception. Returns `nil` if the exception's class is excluded from reporting.
75
+ # @param exception [Exception] the exception to be reported.
76
+ # @param hint [Hash] the hint data that'll be passed to `before_send` callback and the scope's event processors.
77
+ # @return [Event, nil]
45
78
  def event_from_exception(exception, hint = {})
79
+ return unless @configuration.sending_allowed? && @configuration.exception_class_allowed?(exception)
80
+
46
81
  integration_meta = Sentry.integrations[hint[:integration]]
47
- return unless @configuration.exception_class_allowed?(exception)
48
82
 
49
83
  Event.new(configuration: configuration, integration_meta: integration_meta).tap do |event|
50
84
  event.add_exception_interface(exception)
@@ -52,25 +86,36 @@ module Sentry
52
86
  end
53
87
  end
54
88
 
55
- def event_from_message(message, hint = {})
89
+ # Initializes an Event object with the given message.
90
+ # @param message [String] the message to be reported.
91
+ # @param hint [Hash] the hint data that'll be passed to `before_send` callback and the scope's event processors.
92
+ # @return [Event]
93
+ def event_from_message(message, hint = {}, backtrace: nil)
94
+ return unless @configuration.sending_allowed?
95
+
56
96
  integration_meta = Sentry.integrations[hint[:integration]]
57
97
  event = Event.new(configuration: configuration, integration_meta: integration_meta, message: message)
58
- event.add_threads_interface(backtrace: caller)
98
+ event.add_threads_interface(backtrace: backtrace || caller)
59
99
  event
60
100
  end
61
101
 
102
+ # Initializes an Event object with the given Transaction object.
103
+ # @param transaction [Transaction] the transaction to be recorded.
104
+ # @return [TransactionEvent]
62
105
  def event_from_transaction(transaction)
63
106
  TransactionEvent.new(configuration: configuration).tap do |event|
64
107
  event.transaction = transaction.name
65
108
  event.contexts.merge!(trace: transaction.get_trace_context)
66
109
  event.timestamp = transaction.timestamp
67
110
  event.start_timestamp = transaction.start_timestamp
111
+ event.tags = transaction.tags
68
112
 
69
113
  finished_spans = transaction.span_recorder.spans.select { |span| span.timestamp && span != transaction }
70
114
  event.spans = finished_spans.map(&:to_hash)
71
115
  end
72
116
  end
73
117
 
118
+ # @!macro send_event
74
119
  def send_event(event, hint = nil)
75
120
  event_type = event.is_a?(Event) ? event.type : event["type"]
76
121
 
@@ -79,6 +124,7 @@ module Sentry
79
124
 
80
125
  if event.nil?
81
126
  log_info("Discarded event because before_send returned nil")
127
+ transport.record_lost_event(:before_send, 'event')
82
128
  return
83
129
  end
84
130
  end
@@ -92,9 +138,22 @@ module Sentry
92
138
 
93
139
  event_info = Event.get_log_message(event.to_hash)
94
140
  log_info("Unreported #{loggable_event_type}: #{event_info}")
141
+ transport.record_lost_event(:network_error, event_type)
95
142
  raise
96
143
  end
97
144
 
145
+ # Generates a Sentry trace for distribted tracing from the given Span.
146
+ # Returns `nil` if `config.propagate_traces` is `false`.
147
+ # @param span [Span] the span to generate trace from.
148
+ # @return [String, nil]
149
+ def generate_sentry_trace(span)
150
+ return unless configuration.propagate_traces
151
+
152
+ trace = span.to_sentry_trace
153
+ log_debug("[Tracing] Adding #{SENTRY_TRACE_HEADER_NAME} header to outgoing request: #{trace}")
154
+ trace
155
+ end
156
+
98
157
  private
99
158
 
100
159
  def dispatch_background_event(event, hint)
@@ -1,21 +1,31 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "concurrent/utility/processor_counter"
2
4
 
3
5
  require "sentry/utils/exception_cause_chain"
6
+ require 'sentry/utils/custom_inspection'
4
7
  require "sentry/dsn"
8
+ require "sentry/release_detector"
5
9
  require "sentry/transport/configuration"
6
10
  require "sentry/linecache"
7
11
  require "sentry/interfaces/stacktrace_builder"
8
12
 
9
13
  module Sentry
10
14
  class Configuration
15
+ include CustomInspection
11
16
  include LoggingHelper
12
17
  # Directories to be recognized as part of your app. e.g. if you
13
18
  # have an `engines` dir at the root of your project, you may want
14
19
  # to set this to something like /(app|config|engines|lib)/
20
+ #
21
+ # @return [Regexp, nil]
15
22
  attr_accessor :app_dirs_pattern
16
23
 
17
24
  # Provide an object that responds to `call` to send events asynchronously.
18
25
  # E.g.: lambda { |event| Thread.new { Sentry.send_event(event) } }
26
+ #
27
+ # @deprecated It will be removed in the next major release. Please read https://github.com/getsentry/sentry-ruby/issues/1522 for more information
28
+ # @return [Proc, nil]
19
29
  attr_reader :async
20
30
 
21
31
  # to send events in a non-blocking way, sentry-ruby has its own background worker
@@ -25,130 +35,180 @@ module Sentry
25
35
  #
26
36
  # if you want to send events synchronously, set the value to 0
27
37
  # E.g.: config.background_worker_threads = 0
38
+ # @return [Integer]
28
39
  attr_accessor :background_worker_threads
29
40
 
30
41
  # a proc/lambda that takes an array of stack traces
31
42
  # it'll be used to silence (reduce) backtrace of the exception
32
43
  #
33
- # for example:
34
- #
35
- # ```ruby
36
- # Sentry.configuration.backtrace_cleanup_callback = lambda do |backtrace|
37
- # Rails.backtrace_cleaner.clean(backtrace)
38
- # end
39
- # ```
44
+ # @example
45
+ # config.backtrace_cleanup_callback = lambda do |backtrace|
46
+ # Rails.backtrace_cleaner.clean(backtrace)
47
+ # end
40
48
  #
49
+ # @return [Proc, nil]
41
50
  attr_accessor :backtrace_cleanup_callback
42
51
 
43
52
  # Optional Proc, called before adding the breadcrumb to the current scope
44
- # E.g.: lambda { |breadcrumb, hint| breadcrumb }
45
- # E.g.: lambda { |breadcrumb, hint| nil }
46
- # E.g.: lambda { |breadcrumb, hint|
47
- # breadcrumb.message = 'a'
48
- # breadcrumb
49
- # }
53
+ # @example
54
+ # config.before = lambda do |breadcrumb, hint|
55
+ # breadcrumb.message = 'a'
56
+ # breadcrumb
57
+ # end
58
+ # @return [Proc]
50
59
  attr_reader :before_breadcrumb
51
60
 
52
- # Optional Proc, called before sending an event to the server/
53
- # E.g.: lambda { |event, hint| event }
54
- # E.g.: lambda { |event, hint| nil }
55
- # E.g.: lambda { |event, hint|
56
- # event[:message] = 'a'
57
- # event
58
- # }
61
+ # Optional Proc, called before sending an event to the server
62
+ # @example
63
+ # config.before_send = lambda do |event, hint|
64
+ # # skip ZeroDivisionError exceptions
65
+ # # note: hint[:exception] would be a String if you use async callback
66
+ # if hint[:exception].is_a?(ZeroDivisionError)
67
+ # nil
68
+ # else
69
+ # event
70
+ # end
71
+ # end
72
+ # @return [Proc]
59
73
  attr_reader :before_send
60
74
 
61
75
  # An array of breadcrumbs loggers to be used. Available options are:
62
76
  # - :sentry_logger
77
+ # - :http_logger
78
+ # - :redis_logger
79
+ #
80
+ # And if you also use sentry-rails:
63
81
  # - :active_support_logger
82
+ # - :monotonic_active_support_logger
83
+ #
84
+ # @return [Array<Symbol>]
64
85
  attr_reader :breadcrumbs_logger
65
86
 
87
+ # Whether to capture local variables from the raised exception's frame. Default is false.
88
+ # @return [Boolean]
89
+ attr_accessor :capture_exception_frame_locals
90
+
66
91
  # Max number of breadcrumbs a breadcrumb buffer can hold
92
+ # @return [Integer]
67
93
  attr_accessor :max_breadcrumbs
68
94
 
69
95
  # Number of lines of code context to capture, or nil for none
96
+ # @return [Integer, nil]
70
97
  attr_accessor :context_lines
71
98
 
72
99
  # RACK_ENV by default.
100
+ # @return [String]
73
101
  attr_reader :environment
74
102
 
75
103
  # Whether the SDK should run in the debugging mode. Default is false.
76
104
  # If set to true, SDK errors will be logged with backtrace
105
+ # @return [Boolean]
77
106
  attr_accessor :debug
78
107
 
79
108
  # the dsn value, whether it's set via `config.dsn=` or `ENV["SENTRY_DSN"]`
109
+ # @return [String]
80
110
  attr_reader :dsn
81
111
 
82
112
  # Whitelist of enabled_environments that will send notifications to Sentry. Array of Strings.
113
+ # @return [Array<String>]
83
114
  attr_accessor :enabled_environments
84
115
 
85
116
  # Logger 'progname's to exclude from breadcrumbs
117
+ # @return [Array<String>]
86
118
  attr_accessor :exclude_loggers
87
119
 
88
120
  # Array of exception classes that should never be sent. See IGNORE_DEFAULT.
89
121
  # You should probably append to this rather than overwrite it.
122
+ # @return [Array<String>]
90
123
  attr_accessor :excluded_exceptions
91
124
 
92
- # Boolean to check nested exceptions when deciding if to exclude. Defaults to false
125
+ # Boolean to check nested exceptions when deciding if to exclude. Defaults to true
126
+ # @return [Boolean]
93
127
  attr_accessor :inspect_exception_causes_for_exclusion
94
128
  alias inspect_exception_causes_for_exclusion? inspect_exception_causes_for_exclusion
95
129
 
96
130
  # You may provide your own LineCache for matching paths with source files.
97
- # This may be useful if you need to get source code from places other than
98
- # the disk. See Sentry::LineCache for the required interface you must implement.
131
+ # This may be useful if you need to get source code from places other than the disk.
132
+ # @see LineCache
133
+ # @return [LineCache]
99
134
  attr_accessor :linecache
100
135
 
101
136
  # Logger used by Sentry. In Rails, this is the Rails logger, otherwise
102
137
  # Sentry provides its own Sentry::Logger.
138
+ # @return [Logger]
103
139
  attr_accessor :logger
104
140
 
105
141
  # Project directory root for in_app detection. Could be Rails root, etc.
106
142
  # Set automatically for Rails.
107
- attr_reader :project_root
143
+ # @return [String]
144
+ attr_accessor :project_root
145
+
146
+ # Insert sentry-trace to outgoing requests' headers
147
+ # @return [Boolean]
148
+ attr_accessor :propagate_traces
108
149
 
109
150
  # Array of rack env parameters to be included in the event sent to sentry.
151
+ # @return [Array<String>]
110
152
  attr_accessor :rack_env_whitelist
111
153
 
112
154
  # Release tag to be passed with every event sent to Sentry.
113
155
  # We automatically try to set this to a git SHA or Capistrano release.
156
+ # @return [String]
114
157
  attr_accessor :release
115
158
 
116
159
  # The sampling factor to apply to events. A value of 0.0 will not send
117
160
  # any events, and a value of 1.0 will send 100% of events.
161
+ # @return [Float]
118
162
  attr_accessor :sample_rate
119
163
 
120
164
  # Include module versions in reports - boolean.
165
+ # @return [Boolean]
121
166
  attr_accessor :send_modules
122
167
 
123
168
  # When send_default_pii's value is false (default), sensitive information like
124
169
  # - user ip
125
170
  # - user cookie
126
171
  # - request body
172
+ # - query string
127
173
  # will not be sent to Sentry.
174
+ # @return [Boolean]
128
175
  attr_accessor :send_default_pii
129
176
 
177
+ # Allow to skip Sentry emails within rake tasks
178
+ # @return [Boolean]
179
+ attr_accessor :skip_rake_integration
180
+
130
181
  # IP ranges for trusted proxies that will be skipped when calculating IP address.
131
182
  attr_accessor :trusted_proxies
132
183
 
184
+ # @return [String]
133
185
  attr_accessor :server_name
134
186
 
135
187
  # Return a Transport::Configuration object for transport-related configurations.
188
+ # @return [Transport]
136
189
  attr_reader :transport
137
190
 
138
191
  # Take a float between 0.0 and 1.0 as the sample rate for tracing events (transactions).
192
+ # @return [Float]
139
193
  attr_accessor :traces_sample_rate
140
194
 
141
195
  # Take a Proc that controls the sample rate for every tracing event, e.g.
142
- # ```
143
- # lambda do |tracing_context|
144
- # # tracing_context[:transaction_context] contains the information about the transaction
145
- # # tracing_context[:parent_sampled] contains the transaction's parent's sample decision
146
- # true # return value can be a boolean or a float between 0.0 and 1.0
147
- # end
148
- # ```
196
+ # @example
197
+ # config.traces_sampler = lambda do |tracing_context|
198
+ # # tracing_context[:transaction_context] contains the information about the transaction
199
+ # # tracing_context[:parent_sampled] contains the transaction's parent's sample decision
200
+ # true # return value can be a boolean or a float between 0.0 and 1.0
201
+ # end
202
+ # @return [Proc]
149
203
  attr_accessor :traces_sampler
150
204
 
205
+ # Send diagnostic client reports about dropped events, true by default
206
+ # tries to attach to an existing envelope max once every 30s
207
+ # @return [Boolean]
208
+ attr_accessor :send_client_reports
209
+
151
210
  # these are not config options
211
+ # @!visibility private
152
212
  attr_reader :errors, :gem_specs
153
213
 
154
214
  # Most of these errors generate 4XX responses. In general, Sentry clients
@@ -171,17 +231,21 @@ module Sentry
171
231
 
172
232
  LOG_PREFIX = "** [Sentry] ".freeze
173
233
  MODULE_SEPARATOR = "::".freeze
234
+ SKIP_INSPECTION_ATTRIBUTES = [:@linecache, :@stacktrace_builder]
174
235
 
175
236
  # Post initialization callbacks are called at the end of initialization process
176
237
  # allowing extending the configuration of sentry-ruby by multiple extensions
177
238
  @@post_initialization_callbacks = []
178
239
 
179
240
  def initialize
241
+ self.app_dirs_pattern = nil
180
242
  self.debug = false
181
243
  self.background_worker_threads = Concurrent.processor_count
244
+ self.backtrace_cleanup_callback = nil
182
245
  self.max_breadcrumbs = BreadcrumbBuffer::DEFAULT_SIZE
183
246
  self.breadcrumbs_logger = []
184
247
  self.context_lines = 3
248
+ self.capture_exception_frame_locals = false
185
249
  self.environment = environment_from_env
186
250
  self.enabled_environments = []
187
251
  self.exclude_loggers = []
@@ -190,17 +254,21 @@ module Sentry
190
254
  self.linecache = ::Sentry::LineCache.new
191
255
  self.logger = ::Sentry::Logger.new(STDOUT)
192
256
  self.project_root = Dir.pwd
257
+ self.propagate_traces = true
193
258
 
194
- self.release = detect_release
195
259
  self.sample_rate = 1.0
196
260
  self.send_modules = true
197
261
  self.send_default_pii = false
262
+ self.skip_rake_integration = false
263
+ self.send_client_reports = true
198
264
  self.trusted_proxies = []
199
265
  self.dsn = ENV['SENTRY_DSN']
200
266
  self.server_name = server_name_from_env
201
267
 
202
- self.before_send = false
268
+ self.before_send = nil
203
269
  self.rack_env_whitelist = RACK_ENV_WHITELIST_DEFAULT
270
+ self.traces_sample_rate = nil
271
+ self.traces_sampler = nil
204
272
 
205
273
  @transport = Transport::Configuration.new
206
274
  @gem_specs = Hash[Gem::Specification.map { |spec| [spec.name, spec.version.to_s] }] if Gem::Specification.respond_to?(:map)
@@ -209,18 +277,13 @@ module Sentry
209
277
  end
210
278
 
211
279
  def dsn=(value)
212
- return if value.nil? || value.empty?
213
-
214
- @dsn = DSN.new(value)
280
+ @dsn = init_dsn(value)
215
281
  end
216
282
 
217
283
  alias server= dsn=
218
284
 
219
-
220
285
  def async=(value)
221
- if value && !value.respond_to?(:call)
222
- raise(ArgumentError, "async must be callable")
223
- end
286
+ check_callable!("async", value)
224
287
 
225
288
  @async = value
226
289
  end
@@ -239,17 +302,13 @@ module Sentry
239
302
  end
240
303
 
241
304
  def before_send=(value)
242
- unless value == false || value.respond_to?(:call)
243
- raise ArgumentError, "before_send must be callable (or false to disable)"
244
- end
305
+ check_callable!("before_send", value)
245
306
 
246
307
  @before_send = value
247
308
  end
248
309
 
249
310
  def before_breadcrumb=(value)
250
- unless value.nil? || value.respond_to?(:call)
251
- raise ArgumentError, "before_breadcrumb must be callable (or nil to disable)"
252
- end
311
+ check_callable!("before_breadcrumb", value)
253
312
 
254
313
  @before_breadcrumb = value
255
314
  end
@@ -261,18 +320,13 @@ module Sentry
261
320
  def sending_allowed?
262
321
  @errors = []
263
322
 
264
- valid? &&
265
- capture_in_environment? &&
266
- sample_allowed?
323
+ valid? && capture_in_environment?
267
324
  end
268
325
 
269
- def error_messages
270
- @errors = [@errors[0]] + @errors[1..-1].map(&:downcase) # fix case of all but first
271
- @errors.join(", ")
272
- end
326
+ def sample_allowed?
327
+ return true if sample_rate == 1.0
273
328
 
274
- def project_root=(root_dir)
275
- @project_root = root_dir
329
+ Random.rand < sample_rate
276
330
  end
277
331
 
278
332
  def exception_class_allowed?(exc)
@@ -296,6 +350,17 @@ module Sentry
296
350
  !!((@traces_sample_rate && @traces_sample_rate >= 0.0 && @traces_sample_rate <= 1.0) || @traces_sampler) && sending_allowed?
297
351
  end
298
352
 
353
+ # @return [String, nil]
354
+ def csp_report_uri
355
+ if dsn && dsn.valid?
356
+ uri = dsn.csp_report_uri
357
+ uri += "&sentry_release=#{CGI.escape(release)}" if release && !release.empty?
358
+ uri += "&sentry_environment=#{CGI.escape(environment)}" if environment && !environment.empty?
359
+ uri
360
+ end
361
+ end
362
+
363
+ # @api private
299
364
  def stacktrace_builder
300
365
  @stacktrace_builder ||= StacktraceBuilder.new(
301
366
  project_root: @project_root.to_s,
@@ -306,17 +371,39 @@ module Sentry
306
371
  )
307
372
  end
308
373
 
309
- private
310
-
374
+ # @api private
311
375
  def detect_release
312
- detect_release_from_env ||
313
- detect_release_from_git ||
314
- detect_release_from_capistrano ||
315
- detect_release_from_heroku
376
+ return unless sending_allowed?
377
+
378
+ self.release ||= ReleaseDetector.detect_release(project_root: project_root, running_on_heroku: running_on_heroku?)
379
+
380
+ if running_on_heroku? && release.nil?
381
+ log_warn(HEROKU_DYNO_METADATA_MESSAGE)
382
+ end
316
383
  rescue => e
317
384
  log_error("Error detecting release", e, debug: debug)
318
385
  end
319
386
 
387
+ # @api private
388
+ def error_messages
389
+ @errors = [@errors[0]] + @errors[1..-1].map(&:downcase) # fix case of all but first
390
+ @errors.join(", ")
391
+ end
392
+
393
+ private
394
+
395
+ def check_callable!(name, value)
396
+ unless value == nil || value.respond_to?(:call)
397
+ raise ArgumentError, "#{name} must be callable (or nil to disable)"
398
+ end
399
+ end
400
+
401
+ def init_dsn(dsn_string)
402
+ return if dsn_string.nil? || dsn_string.empty?
403
+
404
+ DSN.new(dsn_string)
405
+ end
406
+
320
407
  def excluded_exception?(incoming_exception)
321
408
  excluded_exception_classes.any? do |excluded_exception|
322
409
  matches_exception?(excluded_exception, incoming_exception)
@@ -346,37 +433,6 @@ module Sentry
346
433
  nil
347
434
  end
348
435
 
349
- def detect_release_from_heroku
350
- return unless running_on_heroku?
351
- return if ENV['CI']
352
- log_warn(HEROKU_DYNO_METADATA_MESSAGE) && return unless ENV['HEROKU_SLUG_COMMIT']
353
-
354
- ENV['HEROKU_SLUG_COMMIT']
355
- end
356
-
357
- def running_on_heroku?
358
- File.directory?("/etc/heroku")
359
- end
360
-
361
- def detect_release_from_capistrano
362
- revision_file = File.join(project_root, 'REVISION')
363
- revision_log = File.join(project_root, '..', 'revisions.log')
364
-
365
- if File.exist?(revision_file)
366
- File.read(revision_file).strip
367
- elsif File.exist?(revision_log)
368
- File.open(revision_log).to_a.last.strip.sub(/.*as release ([0-9]+).*/, '\1')
369
- end
370
- end
371
-
372
- def detect_release_from_git
373
- Sentry.sys_command("git rev-parse --short HEAD") if File.directory?(".git")
374
- end
375
-
376
- def detect_release_from_env
377
- ENV['SENTRY_RELEASE']
378
- end
379
-
380
436
  def capture_in_environment?
381
437
  return true if enabled_in_current_env?
382
438
 
@@ -393,36 +449,24 @@ module Sentry
393
449
  end
394
450
  end
395
451
 
396
- def sample_allowed?
397
- return true if sample_rate == 1.0
398
-
399
- if Random::DEFAULT.rand >= sample_rate
400
- @errors << "Excluded by random sample"
401
- false
402
- else
403
- true
404
- end
405
- end
406
-
407
- # Try to resolve the hostname to an FQDN, but fall back to whatever
408
- # the load name is.
409
- def resolve_hostname
410
- Socket.gethostname ||
411
- Socket.gethostbyname(hostname).first rescue server_name
412
- end
413
-
414
452
  def environment_from_env
415
- ENV['SENTRY_CURRENT_ENV'] || ENV['SENTRY_ENVIRONMENT'] || ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'default'
453
+ ENV['SENTRY_CURRENT_ENV'] || ENV['SENTRY_ENVIRONMENT'] || ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development'
416
454
  end
417
455
 
418
456
  def server_name_from_env
419
457
  if running_on_heroku?
420
458
  ENV['DYNO']
421
459
  else
422
- resolve_hostname
460
+ # Try to resolve the hostname to an FQDN, but fall back to whatever
461
+ # the load name is.
462
+ Socket.gethostname || Socket.gethostbyname(hostname).first rescue server_name
423
463
  end
424
464
  end
425
465
 
466
+ def running_on_heroku?
467
+ File.directory?("/etc/heroku") && !ENV["CI"]
468
+ end
469
+
426
470
  def run_post_initialization_callbacks
427
471
  self.class.post_initialization_callbacks.each do |hook|
428
472
  instance_eval(&hook)
@@ -1,3 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ return if Object.method_defined?(:deep_dup)
4
+
1
5
  require 'sentry/core_ext/object/duplicable'
2
6
 
3
7
  #########################################
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ return if Object.method_defined?(:duplicable?)
4
+
3
5
  #########################################
4
6
  # This file was copied from Rails 5.2 #
5
7
  #########################################
data/lib/sentry/dsn.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "uri"
2
4
 
3
5
  module Sentry
@@ -37,10 +39,13 @@ module Sentry
37
39
  def server
38
40
  server = "#{scheme}://#{host}"
39
41
  server += ":#{port}" unless port == PORT_MAP[scheme]
40
- server += path
41
42
  server
42
43
  end
43
44
 
45
+ def csp_report_uri
46
+ "#{server}/api/#{project_id}/security/?sentry_key=#{public_key}"
47
+ end
48
+
44
49
  def envelope_endpoint
45
50
  "#{path}/api/#{project_id}/envelope/"
46
51
  end