|
17 | 17 | require "sentry/transaction"
|
18 | 18 | require "sentry/hub"
|
19 | 19 | require "sentry/background_worker"
|
| 20 | +require "sentry/session_flusher" |
20 | 21 |
|
21 | 22 | [
|
22 | 23 | "sentry/rake",
|
@@ -61,6 +62,10 @@ def exception_locals_tp
|
61 | 62 | # @return [BackgroundWorker]
|
62 | 63 | attr_accessor :background_worker
|
63 | 64 |
|
| 65 | + # @!attribute [r] session_flusher |
| 66 | + # @return [SessionFlusher] |
| 67 | + attr_reader :session_flusher |
| 68 | + |
64 | 69 | ##### Patch Registration #####
|
65 | 70 |
|
66 | 71 | # @!visibility private
|
@@ -189,11 +194,18 @@ def init(&block)
|
189 | 194 | @main_hub = hub
|
190 | 195 | @background_worker = Sentry::BackgroundWorker.new(config)
|
191 | 196 |
|
| 197 | + @session_flusher = if config.auto_session_tracking |
| 198 | + Sentry::SessionFlusher.new(config, client) |
| 199 | + else |
| 200 | + nil |
| 201 | + end |
| 202 | + |
192 | 203 | if config.capture_exception_frame_locals
|
193 | 204 | exception_locals_tp.enable
|
194 | 205 | end
|
195 | 206 |
|
196 | 207 | at_exit do
|
| 208 | + @session_flusher&.kill |
197 | 209 | @background_worker.shutdown
|
198 | 210 | end
|
199 | 211 | end
|
@@ -310,6 +322,26 @@ def with_scope(&block)
|
310 | 322 | get_current_hub.with_scope(&block)
|
311 | 323 | end
|
312 | 324 |
|
| 325 | + # Wrap a given block with session tracking. |
| 326 | + # Aggregate sessions in minutely buckets will be recorded |
| 327 | + # around this block and flushed every minute. |
| 328 | + # |
| 329 | + # @example |
| 330 | + # Sentry.with_session_tracking do |
| 331 | + # a = 1 + 1 # new session recorded with :exited status |
| 332 | + # end |
| 333 | + # |
| 334 | + # Sentry.with_session_tracking do |
| 335 | + # 1 / 0 |
| 336 | + # rescue => e |
| 337 | + # Sentry.capture_exception(e) # new session recorded with :errored status |
| 338 | + # end |
| 339 | + # @return [void] |
| 340 | + def with_session_tracking(&block) |
| 341 | + return yield unless initialized? |
| 342 | + get_current_hub.with_session_tracking(&block) |
| 343 | + end |
| 344 | + |
313 | 345 | # Takes an exception and reports it to Sentry via the currently active hub.
|
314 | 346 | #
|
315 | 347 | # @yieldparam scope [Scope]
|
|
0 commit comments