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

#pause_sessionvoid

This method returns an undefined value.

Stop any events being attributed to the current session until it is resumed or a new session is started

See Also:



66
67
68
69
70
71
72
# File 'lib/bugsnag/session_tracker.rb', line 66

def pause_session
  current_session = SessionTracker.get_current_session

  return unless current_session

  current_session[:paused?] = true
end

#resume_sessionBoolean

Resume the current session if it was previously paused. If there is no current session, a new session will be started

Returns:

  • (Boolean)

    true if a paused session was resumed

See Also:



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/bugsnag/session_tracker.rb', line 81

def resume_session
  current_session = SessionTracker.get_current_session

  if current_session
    # if the session is paused then resume it, otherwise we don't need to
    # do anything
    if current_session[:paused?]
      current_session[:paused?] = false

      return true
    end
  else
    # if there's no current session, start a new one
    start_session
  end

  false
end

#send_sessionsObject

Delivers the current session_counts lists to the session endpoint.



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/bugsnag/session_tracker.rb', line 102

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_sessionvoid Also known as: create_session

This method returns an undefined value.

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

This allows Bugsnag to track error rates for a release.



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

def start_session
  return unless Bugsnag.configuration.enable_sessions && Bugsnag.configuration.should_notify_release_stage?

  start_delivery_thread
  start_time = Time.now().utc().strftime('%Y-%m-%dT%H:%M:00')
  new_session = {
    id: SecureRandom.uuid,
    startedAt: start_time,
    paused?: false,
    events: {
      handled: 0,
      unhandled: 0
    }
  }
  SessionTracker.set_current_session(new_session)
  add_session(start_time)
end