config.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package sessions
  2. import (
  3. "log"
  4. "net/http"
  5. "sync"
  6. "time"
  7. )
  8. // SessionTrackingConfiguration defines the configuration options relevant for session tracking.
  9. // These are likely a subset of the global bugsnag.Configuration. Users should
  10. // not modify this struct directly but rather call
  11. // `bugsnag.Configure(bugsnag.Configuration)` which will update this configuration in return.
  12. type SessionTrackingConfiguration struct {
  13. // PublishInterval defines how often the sessions are sent off to the session server.
  14. PublishInterval time.Duration
  15. // AutoCaptureSessions can be set to false to disable automatic session
  16. // tracking. If you want control over what is deemed a session, you can
  17. // switch off automatic session tracking with this configuration, and call
  18. // bugsnag.StartSession() when appropriate for your application. See the
  19. // official docs for instructions and examples of associating handled
  20. // errors with sessions and ensuring error rate accuracy on the Bugsnag
  21. // dashboard. This will default to true, but is stored as an interface to enable
  22. // us to detect when this option has not been set.
  23. AutoCaptureSessions interface{}
  24. // APIKey defines the API key for the Bugsnag project. Same value as for reporting errors.
  25. APIKey string
  26. // Endpoint is the URI of the session server to receive session payloads.
  27. Endpoint string
  28. // Version defines the current version of the notifier.
  29. Version string
  30. // ReleaseStage defines the release stage, e.g. "production" or "staging",
  31. // that this session occurred in. The release stage, in combination with
  32. // the app version make up the release that Bugsnag tracks.
  33. ReleaseStage string
  34. // Hostname defines the host of the server this application is running on.
  35. Hostname string
  36. // AppType defines the type of the application.
  37. AppType string
  38. // AppVersion defines the version of the application.
  39. AppVersion string
  40. // Transport defines the http.RoundTripper to be used for managing HTTP requests.
  41. Transport http.RoundTripper
  42. // The release stages to notify about sessions in. If you set this then
  43. // bugsnag-go will only send sessions to Bugsnag if the release stage
  44. // is listed here.
  45. NotifyReleaseStages []string
  46. // Logger is the logger that Bugsnag should log to. Uses the same defaults
  47. // as go's builtin logging package. This logger gets invoked when any error
  48. // occurs inside the library itself.
  49. Logger interface {
  50. Printf(format string, v ...interface{})
  51. }
  52. mutex sync.Mutex
  53. }
  54. // Update modifies the values inside the receiver to match the non-default properties of the given config.
  55. // Existing properties will not be cleared when given empty fields.
  56. func (c *SessionTrackingConfiguration) Update(config *SessionTrackingConfiguration) {
  57. c.mutex.Lock()
  58. defer c.mutex.Unlock()
  59. if config.PublishInterval != 0 {
  60. c.PublishInterval = config.PublishInterval
  61. }
  62. if config.APIKey != "" {
  63. c.APIKey = config.APIKey
  64. }
  65. if config.Endpoint != "" {
  66. c.Endpoint = config.Endpoint
  67. }
  68. if config.Version != "" {
  69. c.Version = config.Version
  70. }
  71. if config.ReleaseStage != "" {
  72. c.ReleaseStage = config.ReleaseStage
  73. }
  74. if config.Hostname != "" {
  75. c.Hostname = config.Hostname
  76. }
  77. if config.AppType != "" {
  78. c.AppType = config.AppType
  79. }
  80. if config.AppVersion != "" {
  81. c.AppVersion = config.AppVersion
  82. }
  83. if config.Transport != nil {
  84. c.Transport = config.Transport
  85. }
  86. if config.Logger != nil {
  87. c.Logger = config.Logger
  88. }
  89. if config.NotifyReleaseStages != nil {
  90. c.NotifyReleaseStages = config.NotifyReleaseStages
  91. }
  92. if config.AutoCaptureSessions != nil {
  93. c.AutoCaptureSessions = config.AutoCaptureSessions
  94. }
  95. }
  96. func (c *SessionTrackingConfiguration) logf(fmt string, args ...interface{}) {
  97. if c != nil && c.Logger != nil {
  98. c.Logger.Printf(fmt, args...)
  99. } else {
  100. log.Printf(fmt, args...)
  101. }
  102. }
  103. // IsAutoCaptureSessions identifies whether or not the notifier should
  104. // automatically capture sessions as requests come in. It's a convenience
  105. // wrapper that allows automatic session capturing to be enabled by default.
  106. func (c *SessionTrackingConfiguration) IsAutoCaptureSessions() bool {
  107. if c.AutoCaptureSessions == nil {
  108. return true // enabled by default
  109. }
  110. if val, ok := c.AutoCaptureSessions.(bool); ok {
  111. return val
  112. }
  113. // It has been configured to *something* (although not a valid value)
  114. // assume the user wanted to disable this option.
  115. return false
  116. }