tracker.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package sessions
  2. import (
  3. "context"
  4. "net/http"
  5. "os"
  6. "os/signal"
  7. "sync"
  8. "syscall"
  9. "time"
  10. )
  11. const (
  12. //contextSessionKey is a unique key for accessing and setting Bugsnag
  13. //session data on a context.Context object
  14. contextSessionKey ctxKey = 1
  15. )
  16. // ctxKey is a type alias that ensures uniqueness as a context.Context key
  17. type ctxKey int
  18. // SessionTracker exposes a method for starting sessions that are used for
  19. // gauging your application's health
  20. type SessionTracker interface {
  21. StartSession(context.Context) context.Context
  22. FlushSessions()
  23. }
  24. type sessionTracker struct {
  25. sessionChannel chan *Session
  26. sessions []*Session
  27. config *SessionTrackingConfiguration
  28. publisher sessionPublisher
  29. sessionsMutex sync.Mutex
  30. }
  31. // NewSessionTracker creates a new SessionTracker based on the provided config,
  32. func NewSessionTracker(config *SessionTrackingConfiguration) SessionTracker {
  33. publisher := publisher{
  34. config: config,
  35. client: &http.Client{Transport: config.Transport},
  36. }
  37. st := sessionTracker{
  38. sessionChannel: make(chan *Session, 1),
  39. sessions: []*Session{},
  40. config: config,
  41. publisher: &publisher,
  42. }
  43. go st.processSessions()
  44. return &st
  45. }
  46. // IncrementEventCountAndGetSession extracts a Bugsnag session from the given
  47. // context and increments the event count of unhandled or handled events and
  48. // returns the session
  49. func IncrementEventCountAndGetSession(ctx context.Context, unhandled bool) *Session {
  50. if s := ctx.Value(contextSessionKey); s != nil {
  51. if session, ok := s.(*Session); ok && !session.StartedAt.IsZero() {
  52. // It is not just getting back a default value
  53. ec := session.EventCounts
  54. if unhandled {
  55. ec.Unhandled++
  56. } else {
  57. ec.Handled++
  58. }
  59. return session
  60. }
  61. }
  62. return nil
  63. }
  64. func (s *sessionTracker) StartSession(ctx context.Context) context.Context {
  65. session := newSession()
  66. s.sessionChannel <- session
  67. return context.WithValue(ctx, contextSessionKey, session)
  68. }
  69. func (s *sessionTracker) interval() time.Duration {
  70. s.config.mutex.Lock()
  71. defer s.config.mutex.Unlock()
  72. return s.config.PublishInterval
  73. }
  74. func (s *sessionTracker) processSessions() {
  75. tic := time.Tick(s.interval())
  76. shutdown := shutdownSignals()
  77. for {
  78. select {
  79. case session := <-s.sessionChannel:
  80. s.appendSession(session)
  81. case <-tic:
  82. s.publishCollectedSessions()
  83. case sig := <-shutdown:
  84. s.flushSessionsAndRepeatSignal(shutdown, sig.(syscall.Signal))
  85. }
  86. }
  87. }
  88. func (s *sessionTracker) appendSession(session *Session) {
  89. s.sessionsMutex.Lock()
  90. defer s.sessionsMutex.Unlock()
  91. s.sessions = append(s.sessions, session)
  92. }
  93. func (s *sessionTracker) publishCollectedSessions() {
  94. s.sessionsMutex.Lock()
  95. defer s.sessionsMutex.Unlock()
  96. oldSessions := s.sessions
  97. s.sessions = nil
  98. if len(oldSessions) > 0 {
  99. go func(s *sessionTracker) {
  100. err := s.publisher.publish(oldSessions)
  101. if err != nil {
  102. s.config.logf("%v", err)
  103. }
  104. }(s)
  105. }
  106. }
  107. func (s *sessionTracker) flushSessionsAndRepeatSignal(shutdown chan<- os.Signal, sig syscall.Signal) {
  108. s.sessionsMutex.Lock()
  109. defer s.sessionsMutex.Unlock()
  110. signal.Stop(shutdown)
  111. if len(s.sessions) > 0 {
  112. err := s.publisher.publish(s.sessions)
  113. if err != nil {
  114. s.config.logf("%v", err)
  115. }
  116. }
  117. syscall.Kill(syscall.Getpid(), sig)
  118. }
  119. func (s *sessionTracker) FlushSessions() {
  120. s.sessionsMutex.Lock()
  121. defer s.sessionsMutex.Unlock()
  122. sessions := s.sessions
  123. s.sessions = nil
  124. if len(sessions) != 0 {
  125. if err := s.publisher.publish(sessions); err != nil {
  126. s.config.logf("%v", err)
  127. }
  128. }
  129. }
  130. func shutdownSignals() chan os.Signal {
  131. c := make(chan os.Signal, 1)
  132. signal.Notify(c, syscall.SIGTERM, syscall.SIGINT)
  133. return c
  134. }