Class: Bugsnag::SessionTracker

Inherits:
Object
  • Object
show all
Defined in:
lib/bugsnag/session_tracker.rb

Constant Summary collapse

THREAD_SESSION =
"bugsnag_session"
SESSION_PAYLOAD_VERSION =
"1.0"
MUTEX =
Mutex.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSessionTracker

Initializes the session tracker.



27
28
29
30
31
# File 'lib/bugsnag/session_tracker.rb', line 27

def initialize
  require 'concurrent'

  @session_counts = Concurrent::Hash.new(0)
end

Instance Attribute Details

#session_countsObject (readonly)

Returns the value of attribute session_counts.



11
12
13
# File 'lib/bugsnag/session_tracker.rb', line 11

def session_counts
  @session_counts
end

Class Method Details

.get_current_sessionObject

Returns the session information for this thread.



21
22
23
# File 'lib/bugsnag/session_tracker.rb', line 21

def self.get_current_session
  Thread.current[THREAD_SESSION]
end

.set_current_session(session) ⇒ Object

Sets the session information for this thread.



15
16
17
# File 'lib/bugsnag/session_tracker.rb', line 15

def self.set_current_session(session)
  Thread.current[THREAD_SESSION] = session
end

Instance Method Details

#send_sessionsObject

Delivers the current session_counts lists to the session endpoint.



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/bugsnag/session_tracker.rb', line 57

def send_sessions
  sessions = []
  counts = @session_counts
  @session_counts = Concurrent::Hash.new(0)
  counts.each do |min, count|
    sessions << {
      :startedAt => min,
      :sessionsStarted => count
    }
  end
  deliver(sessions)
end

#start_sessionObject Also known as: create_session

Starts a new session, storing it on the current thread.

This allows Bugsnag to track error rates for a release.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/bugsnag/session_tracker.rb', line 37

def start_session
  return unless Bugsnag.configuration.enable_sessions
  start_delivery_thread
  start_time = Time.now().utc().strftime('%Y-%m-%dT%H:%M:00')
  new_session = {
    :id => SecureRandom.uuid,
    :startedAt => start_time,
    :events => {
      :handled => 0,
      :unhandled => 0
    }
  }
  SessionTracker.set_current_session(new_session)
  add_session(start_time)
end