Skip to content

Commit

Permalink
Consolidate Go/Go-Perf environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
DariaKunoichi committed Nov 22, 2024
1 parent cf523d1 commit eb2943a
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 62 deletions.
50 changes: 26 additions & 24 deletions v2/bugsnag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
Expand All @@ -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,
})
}
21 changes: 19 additions & 2 deletions v2/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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, ",")
}
Expand Down
6 changes: 3 additions & 3 deletions v2/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 "
Expand Down Expand Up @@ -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},
},
}

Expand Down
14 changes: 14 additions & 0 deletions v2/sessions/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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{}) {
Expand Down
42 changes: 21 additions & 21 deletions v2/sessions/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 {
Expand All @@ -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) {
Expand All @@ -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,
}
}
2 changes: 1 addition & 1 deletion v2/sessions/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 11 additions & 11 deletions v2/sessions/publisher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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"},
}
}

Expand Down

0 comments on commit eb2943a

Please sign in to comment.