diff --git a/v2/bugsnag.go b/v2/bugsnag.go index 05d2778..10d5995 100644 --- a/v2/bugsnag.go +++ b/v2/bugsnag.go @@ -244,18 +244,19 @@ func init() { Notify: "https://notify.bugsnag.com", Sessions: "https://sessions.bugsnag.com", }, - Hostname: device.GetHostname(), - AppType: "", - AppVersion: "", - AutoCaptureSessions: true, - ReleaseStage: "", - ParamsFilters: []string{"password", "secret", "authorization", "cookie", "access_token"}, - SourceRoot: sourceRoot, - ProjectPackages: []string{"main*"}, - NotifyReleaseStages: nil, - Logger: log.New(os.Stdout, log.Prefix(), log.Flags()), - PanicHandler: defaultPanicHandler, - Transport: http.DefaultTransport, + Hostname: device.GetHostname(), + AppType: "", + AppVersion: "", + AutoCaptureSessions: true, + ReleaseStage: "", + ParamsFilters: []string{"password", "secret", "authorization", "cookie", "access_token"}, + SourceRoot: sourceRoot, + ProjectPackages: []string{"main*"}, + EnabledReleaseStages: nil, + NotifyReleaseStages: nil, + Logger: log.New(os.Stdout, log.Prefix(), log.Flags()), + PanicHandler: defaultPanicHandler, + Transport: http.DefaultTransport, flushSessionsOnRepanic: true, }) @@ -271,17 +272,18 @@ func startSessionTracking() { func updateSessionConfig() { sessionTrackingConfig.Update(&sessions.SessionTrackingConfiguration{ - APIKey: Config.APIKey, - AutoCaptureSessions: Config.AutoCaptureSessions, - Endpoint: Config.Endpoints.Sessions, - Version: Version, - PublishInterval: DefaultSessionPublishInterval, - Transport: Config.Transport, - ReleaseStage: Config.ReleaseStage, - Hostname: Config.Hostname, - AppType: Config.AppType, - AppVersion: Config.AppVersion, - NotifyReleaseStages: Config.NotifyReleaseStages, - Logger: Config.Logger, + APIKey: Config.APIKey, + AutoCaptureSessions: Config.AutoCaptureSessions, + Endpoint: Config.Endpoints.Sessions, + Version: Version, + PublishInterval: DefaultSessionPublishInterval, + Transport: Config.Transport, + ReleaseStage: Config.ReleaseStage, + Hostname: Config.Hostname, + AppType: Config.AppType, + AppVersion: Config.AppVersion, + NotifyReleaseStages: Config.NotifyReleaseStages, + EnabledReleaseStages: Config.EnabledReleaseStages, + Logger: Config.Logger, }) } diff --git a/v2/configuration.go b/v2/configuration.go index 08ecb7e..b70ff1e 100644 --- a/v2/configuration.go +++ b/v2/configuration.go @@ -57,6 +57,11 @@ type Configuration struct { // os.Hostname() and is graphed in the Bugsnag dashboard. Hostname string + // The Release stages to notify in. If you set this then bugsnag-go will + // only send notifications to Bugsnag if the ReleaseStage is listed here. + EnabledReleaseStages []string + + // DEPRECATED - use EnabledReleaseStages instead. // The Release stages to notify in. If you set this then bugsnag-go will // only send notifications to Bugsnag if the ReleaseStage is listed here. NotifyReleaseStages []string @@ -155,6 +160,9 @@ func (config *Configuration) update(other *Configuration) *Configuration { if other.Logger != nil { config.Logger = other.Logger } + if other.EnabledReleaseStages != nil { + config.EnabledReleaseStages = other.EnabledReleaseStages + } if other.NotifyReleaseStages != nil { config.NotifyReleaseStages = other.NotifyReleaseStages } @@ -175,6 +183,12 @@ func (config *Configuration) update(other *Configuration) *Configuration { if other.AutoCaptureSessions != nil { config.AutoCaptureSessions = other.AutoCaptureSessions } + + // Prefer to use new EnabledReleaseStages over deprecated NotifyReleaseStages + if config.EnabledReleaseStages == nil { + config.EnabledReleaseStages = config.NotifyReleaseStages + } + config.updateEndpoints(&other.Endpoints) return config } @@ -269,13 +283,13 @@ func (config *Configuration) logf(fmt string, args ...interface{}) { } func (config *Configuration) notifyInReleaseStage() bool { - if config.NotifyReleaseStages == nil { + if config.EnabledReleaseStages == nil { return true } if config.ReleaseStage == "" { return true } - for _, r := range config.NotifyReleaseStages { + for _, r := range config.EnabledReleaseStages { if r == config.ReleaseStage { return true } @@ -309,6 +323,9 @@ func (config *Configuration) loadEnv() { if appType := os.Getenv("BUGSNAG_APP_TYPE"); appType != "" { envConfig.AppType = appType } + if enabledStages := os.Getenv("BUGSNAG_ENABLED_RELEASE_STAGES"); enabledStages != "" { + envConfig.EnabledReleaseStages = strings.Split(enabledStages, ",") + } if stages := os.Getenv("BUGSNAG_NOTIFY_RELEASE_STAGES"); stages != "" { envConfig.NotifyReleaseStages = strings.Split(stages, ",") } diff --git a/v2/configuration_test.go b/v2/configuration_test.go index 3035dd9..1aadc5a 100644 --- a/v2/configuration_test.go +++ b/v2/configuration_test.go @@ -39,7 +39,7 @@ func TestNotifyReleaseStages(t *testing.T) { for _, tc := range tt { rs, nrs, exp := tc.releaseStage, tc.notifyReleaseStages, tc.expected - config := &Configuration{ReleaseStage: rs, NotifyReleaseStages: nrs} + config := &Configuration{ReleaseStage: rs, EnabledReleaseStages: nrs} if config.notifyInReleaseStage() != exp { if !exp { notify = " not " @@ -249,10 +249,10 @@ func TestConfiguringCustomLogger(t *testing.T) { msg string }{ { - config: Configuration{ReleaseStage: "production", NotifyReleaseStages: []string{"development", "production"}, Logger: l1}, + config: Configuration{ReleaseStage: "production", EnabledReleaseStages: []string{"development", "production"}, Logger: l1}, }, { - config: Configuration{ReleaseStage: "production", NotifyReleaseStages: []string{"development", "production"}, Logger: l2}, + config: Configuration{ReleaseStage: "production", EnabledReleaseStages: []string{"development", "production"}, Logger: l2}, }, } diff --git a/v2/sessions/config.go b/v2/sessions/config.go index 1b083b5..7580832 100644 --- a/v2/sessions/config.go +++ b/v2/sessions/config.go @@ -45,6 +45,12 @@ type SessionTrackingConfiguration struct { // Transport defines the http.RoundTripper to be used for managing HTTP requests. Transport http.RoundTripper + // The release stages to notify about sessions in. If you set this then + // bugsnag-go will only send sessions to Bugsnag if the release stage + // is listed here. + EnabledReleaseStages []string + + // DEPRECATED - use EnabledReleaseStages instead. // The release stages to notify about sessions in. If you set this then // bugsnag-go will only send sessions to Bugsnag if the release stage // is listed here. @@ -95,12 +101,20 @@ func (c *SessionTrackingConfiguration) Update(config *SessionTrackingConfigurati if config.Logger != nil { c.Logger = config.Logger } + if config.EnabledReleaseStages != nil { + c.EnabledReleaseStages = config.EnabledReleaseStages + } if config.NotifyReleaseStages != nil { c.NotifyReleaseStages = config.NotifyReleaseStages } if config.AutoCaptureSessions != nil { c.AutoCaptureSessions = config.AutoCaptureSessions } + + // Prefer to use new EnabledReleaseStages over deprecated NotifyReleaseStages + if c.EnabledReleaseStages == nil { + c.EnabledReleaseStages = c.NotifyReleaseStages + } } func (c *SessionTrackingConfiguration) logf(fmt string, args ...interface{}) { diff --git a/v2/sessions/config_test.go b/v2/sessions/config_test.go index e898d97..b3c0c5a 100644 --- a/v2/sessions/config_test.go +++ b/v2/sessions/config_test.go @@ -25,7 +25,7 @@ func TestConfigDoesNotChangeGivenBlankValues(t *testing.T) { {"AppType", exp.AppType, c.AppType}, {"AppVersion", exp.AppVersion, c.AppVersion}, {"Transport", exp.Transport, c.Transport}, - {"NotifyReleaseStages", exp.NotifyReleaseStages, c.NotifyReleaseStages}, + {"EnabledReleaseStages", exp.EnabledReleaseStages, c.EnabledReleaseStages}, } for _, tc := range tt { if !reflect.DeepEqual(tc.got, tc.expected) { @@ -37,15 +37,15 @@ func TestConfigDoesNotChangeGivenBlankValues(t *testing.T) { func TestConfigUpdatesGivenNonDefaultValues(t *testing.T) { c := testConfig() exp := SessionTrackingConfiguration{ - PublishInterval: 40 * time.Second, - APIKey: "api234", - Endpoint: "https://docs.bugsnag.com/platforms/go/", - Version: "2.7.3", - ReleaseStage: "Production", - Hostname: "Brian's Surface", - AppType: "Revel API", - AppVersion: "6.3.9", - NotifyReleaseStages: []string{"staging", "production"}, + PublishInterval: 40 * time.Second, + APIKey: "api234", + Endpoint: "https://docs.bugsnag.com/platforms/go/", + Version: "2.7.3", + ReleaseStage: "Production", + Hostname: "Brian's Surface", + AppType: "Revel API", + AppVersion: "6.3.9", + EnabledReleaseStages: []string{"staging", "production"}, } c.Update(&exp) tt := []struct { @@ -61,7 +61,7 @@ func TestConfigUpdatesGivenNonDefaultValues(t *testing.T) { {"Hostname", exp.Hostname, c.Hostname}, {"AppType", exp.AppType, c.AppType}, {"AppVersion", exp.AppVersion, c.AppVersion}, - {"NotifyReleaseStages", exp.NotifyReleaseStages, c.NotifyReleaseStages}, + {"EnabledReleaseStages", exp.EnabledReleaseStages, c.EnabledReleaseStages}, } for _, tc := range tt { if !reflect.DeepEqual(tc.got, tc.expected) { @@ -72,15 +72,15 @@ func TestConfigUpdatesGivenNonDefaultValues(t *testing.T) { func testConfig() SessionTrackingConfiguration { return SessionTrackingConfiguration{ - PublishInterval: 20 * time.Second, - APIKey: "api123", - Endpoint: "https://bugsnag.com/jobs", //If you like what you see... ;) - Version: "1.6.2", - ReleaseStage: "Staging", - Hostname: "Russ's MacbookPro", - AppType: "Gin API", - AppVersion: "5.2.8", - NotifyReleaseStages: []string{"staging", "production"}, - Transport: http.DefaultTransport, + PublishInterval: 20 * time.Second, + APIKey: "api123", + Endpoint: "https://bugsnag.com/jobs", //If you like what you see... ;) + Version: "1.6.2", + ReleaseStage: "Staging", + Hostname: "Russ's MacbookPro", + AppType: "Gin API", + AppVersion: "5.2.8", + EnabledReleaseStages: []string{"staging", "production"}, + Transport: http.DefaultTransport, } } diff --git a/v2/sessions/publisher.go b/v2/sessions/publisher.go index 5d636f4..ba97c5c 100644 --- a/v2/sessions/publisher.go +++ b/v2/sessions/publisher.go @@ -41,7 +41,7 @@ func (p *publisher) publish(sessions []*Session) error { return fmt.Errorf("bugsnag/sessions/publisher.publish invalid API key: '%s'", apiKey) } - nrs, rs := p.config.NotifyReleaseStages, p.config.ReleaseStage + nrs, rs := p.config.EnabledReleaseStages, p.config.ReleaseStage if rs != "" && (nrs != nil && !contains(nrs, rs)) { // Always send sessions if the release stage is not set, but don't send any // sessions when notify release stages don't match the current release stage diff --git a/v2/sessions/publisher_test.go b/v2/sessions/publisher_test.go index c08c53f..2ac26a5 100644 --- a/v2/sessions/publisher_test.go +++ b/v2/sessions/publisher_test.go @@ -167,7 +167,7 @@ func TestNoSessionsOutsideNotifyReleaseStages(t *testing.T) { testClient := testHTTPClient{} config := makeHeavyConfig() - config.NotifyReleaseStages = []string{"staging", "production"} + config.EnabledReleaseStages = []string{"staging", "production"} publisher := publisher{ config: config, client: &testClient, @@ -187,7 +187,7 @@ func TestReleaseStageNotSetSendsSessionsRegardlessOfNotifyReleaseStages(t *testi testClient := testHTTPClient{} config := makeHeavyConfig() - config.NotifyReleaseStages = []string{"staging", "production"} + config.EnabledReleaseStages = []string{"staging", "production"} config.ReleaseStage = "" publisher := publisher{ config: config, @@ -205,15 +205,15 @@ func TestReleaseStageNotSetSendsSessionsRegardlessOfNotifyReleaseStages(t *testi func makeHeavyConfig() *SessionTrackingConfiguration { return &SessionTrackingConfiguration{ - AppType: "gin", - APIKey: testAPIKey, - AppVersion: "1.2.3-beta", - Version: "2.3.4-alpha", - Endpoint: sessionEndpoint, - Transport: http.DefaultTransport, - ReleaseStage: "development", - Hostname: "gce-1234-us-west-1", - NotifyReleaseStages: []string{"development"}, + AppType: "gin", + APIKey: testAPIKey, + AppVersion: "1.2.3-beta", + Version: "2.3.4-alpha", + Endpoint: sessionEndpoint, + Transport: http.DefaultTransport, + ReleaseStage: "development", + Hostname: "gce-1234-us-west-1", + EnabledReleaseStages: []string{"development"}, } }