Skip to content

Commit ffb125a

Browse files
Merge pull request #835 from bugsnag/tms/hub-api-keys
Set default endpoints based on API key
2 parents 06fcb49 + 9091093 commit ffb125a

File tree

8 files changed

+85
-34
lines changed

8 files changed

+85
-34
lines changed

.gitignore

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,13 @@ pkg
1818
*.gem
1919

2020
vendor
21-
# Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
22-
#
23-
# * Create a file at ~/.gitignore
24-
# * Include files you want ignored
25-
# * Run: git config --global core.excludesfile ~/.gitignore
26-
#
27-
# After doing this, these files will be ignored in all your git projects,
28-
# saving you from having to 'pollute' every project you touch with them
29-
#
30-
# Not sure what to needs to be ignored for particular editors/OSes? Here's some ideas to get you started. (Remember, remove the leading # of the line)
31-
#
32-
# For MacOS:
33-
#
34-
#.DS_Store
21+
22+
# IDEs
23+
.idea/
24+
*.iml
25+
26+
# macOS:
27+
.DS_Store
3528

3629
# For TextMate
3730
#*.tmproj
@@ -54,3 +47,4 @@ bin
5447

5548
# For Bugsnag-Maze-Runner:
5649
maze_output
50+
maze-runner.log

.rubocop_todo.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,7 @@ Style/RescueStandardError:
600600
Style/SafeNavigation:
601601
Exclude:
602602
- 'lib/bugsnag/middleware/callbacks.rb'
603+
- 'lib/bugsnag/configuration.rb'
603604

604605
# Offense count: 1
605606
# Cop supports --auto-correct.

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
Changelog
22
=========
33

4+
## v6.28.0 (** June 2025)
5+
6+
### Enhancements
7+
8+
* Set default endpoints based on API key
9+
| [#835](https://github.com/bugsnag/bugsnag-ruby/pull/835)
10+
411
## v6.27.1 (18 June 2024)
512

613
### Fixes

example/rack/Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ else
66
gem 'bugsnag'
77
end
88

9-
gem 'rack'
9+
gem 'rack', '~> 2.0'
1010
gem 'redcarpet'

lib/bugsnag.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ class << self
5454
def configure(validate_api_key=true)
5555
yield(configuration) if block_given?
5656

57+
configuration.set_default_endpoints
58+
5759
# Create the session tracker if sessions are enabled to avoid the overhead
5860
# of creating it on the first request. We skip this if we're not validating
5961
# the API key as we use this internally before the user's configure block
@@ -522,8 +524,12 @@ def check_key_valid
522524
# If only a notify_endpoint has been set, session tracking will be disabled
523525
# If only a session_endpoint has been set, and ArgumentError will be raised
524526
def check_endpoint_setup
525-
notify_set = configuration.notify_endpoint && configuration.notify_endpoint != Bugsnag::Configuration::DEFAULT_NOTIFY_ENDPOINT
526-
session_set = configuration.session_endpoint && configuration.session_endpoint != Bugsnag::Configuration::DEFAULT_SESSION_ENDPOINT
527+
notify_set = configuration.notify_endpoint &&
528+
configuration.notify_endpoint != Bugsnag::Configuration::DEFAULT_NOTIFY_ENDPOINT &&
529+
configuration.notify_endpoint != Bugsnag::Configuration::HUB_NOTIFY_ENDPOINT
530+
session_set = configuration.session_endpoint &&
531+
configuration.session_endpoint != Bugsnag::Configuration::DEFAULT_SESSION_ENDPOINT &&
532+
configuration.session_endpoint != Bugsnag::Configuration::HUB_SESSION_ENDPOINT
527533
if notify_set && !session_set
528534
configuration.warn("The session endpoint has not been set, all further session capturing will be disabled")
529535
configuration.disable_sessions

lib/bugsnag/configuration.rb

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,9 @@ class Configuration
199199

200200
DEFAULT_NOTIFY_ENDPOINT = "https://notify.bugsnag.com"
201201
DEFAULT_SESSION_ENDPOINT = "https://sessions.bugsnag.com"
202-
DEFAULT_ENDPOINT = DEFAULT_NOTIFY_ENDPOINT
202+
HUB_NOTIFY_ENDPOINT = "https://notify.insighthub.smartbear.com"
203+
HUB_SESSION_ENDPOINT = "https://sessions.insighthub.smartbear.com"
204+
HUB_PREFIX = "00000"
203205

204206
DEFAULT_META_DATA_FILTERS = [
205207
/authorization/i,
@@ -249,7 +251,7 @@ def initialize
249251
# to avoid infinite recursion when creating breadcrumb buffer
250252
@max_breadcrumbs = DEFAULT_MAX_BREADCRUMBS
251253

252-
@endpoints = EndpointConfiguration.new(DEFAULT_NOTIFY_ENDPOINT, DEFAULT_SESSION_ENDPOINT)
254+
@endpoints = EndpointConfiguration.new(nil, nil)
253255

254256
@enable_events = true
255257
@enable_sessions = true
@@ -540,6 +542,19 @@ def session_endpoint=(new_session_endpoint)
540542
set_endpoints(notify_endpoint, new_session_endpoint) # Pass the existing notify_endpoint through so it doesn't get overwritten
541543
end
542544

545+
##
546+
# Sets the notification and session endpoints to default values if neither have been set
547+
#
548+
def set_default_endpoints
549+
return unless @endpoints.notify.nil? && @endpoints.sessions.nil?
550+
551+
self.endpoints = if hub_api_key?
552+
EndpointConfiguration.new(HUB_NOTIFY_ENDPOINT, HUB_SESSION_ENDPOINT)
553+
else
554+
EndpointConfiguration.new(DEFAULT_NOTIFY_ENDPOINT, DEFAULT_SESSION_ENDPOINT)
555+
end
556+
end
557+
543558
##
544559
# Sets the notification and session endpoints
545560
#
@@ -753,5 +768,9 @@ def default_hostname
753768
# Send the heroku dyno name instead of hostname if available
754769
ENV["DYNO"] || Socket.gethostname;
755770
end
771+
772+
def hub_api_key?
773+
@api_key && @api_key.start_with?(HUB_PREFIX)
774+
end
756775
end
757776
end

spec/configuration_spec.rb

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@
107107

108108
describe "endpoint configuration" do
109109
describe "#notify_endpoint" do
110-
it "defaults to DEFAULT_NOTIFY_ENDPOINT" do
111-
expect(subject.notify_endpoint).to eq(Bugsnag::Configuration::DEFAULT_NOTIFY_ENDPOINT)
110+
it "defaults to nil" do
111+
expect(subject.notify_endpoint).to eq(nil)
112112
end
113113

114114
it "is readonly" do
@@ -121,11 +121,15 @@
121121
end
122122

123123
describe "#session_endpoint" do
124-
it "defaults to DEFAULT_SESSION_ENDPOINT" do
125-
expect(subject.session_endpoint).to eq(Bugsnag::Configuration::DEFAULT_SESSION_ENDPOINT)
124+
it "defaults to nil" do
125+
expect(subject.session_endpoint).to eq(nil)
126126
end
127127
end
128128

129+
describe "#set_default_endpoints" do
130+
131+
end
132+
129133
describe "#endpoint=" do
130134
let(:custom_notify_endpoint) { "My custom notify endpoint" }
131135
let(:session_endpoint) { "My session endpoint" }
@@ -171,11 +175,11 @@
171175
end
172176

173177
describe "#endpoints" do
174-
it "defaults to 'DEFAULT_NOTIFY_ENDPOINT' & 'DEFAULT_SESSION_ENDPOINT'" do
178+
it "defaults to nil & nil" do
175179
config = Bugsnag::Configuration.new
176180

177-
expect(config.endpoints.notify).to eq(Bugsnag::Configuration::DEFAULT_NOTIFY_ENDPOINT)
178-
expect(config.endpoints.sessions).to eq(Bugsnag::Configuration::DEFAULT_SESSION_ENDPOINT)
181+
expect(config.endpoints.notify).to eq(nil)
182+
expect(config.endpoints.sessions).to eq(nil)
179183
end
180184
end
181185

@@ -203,7 +207,7 @@
203207
expect(config.enable_sessions).to be(false)
204208
end
205209

206-
# TODO: this behaviour exists for backwards compatibilitiy
210+
# TODO: this behaviour exists for backwards compatibility
207211
# ideally we should not send events in this case
208212
it "warns and disables sessions if only notify URL is given" do
209213
config = Bugsnag::Configuration.new
@@ -427,6 +431,23 @@ def output_lines
427431
'[Bugsnag] WARN: No valid API key has been set, notifications will not be sent'
428432
)
429433
end
434+
435+
it "uses the default endpoints for non-hub API keys", :no_configure => true do
436+
Bugsnag.configure do |config|
437+
config.api_key = '00002472bd130ac0ab0f52715bbdc600'
438+
end
439+
440+
expect(Bugsnag.configuration.endpoints.notify).to eq(Bugsnag::Configuration::DEFAULT_NOTIFY_ENDPOINT)
441+
expect(Bugsnag.configuration.endpoints.sessions).to eq(Bugsnag::Configuration::DEFAULT_SESSION_ENDPOINT)
442+
end
443+
444+
it "uses the hub endpoints for hub API keys", :no_configure => true do
445+
Bugsnag.configure do |config|
446+
config.api_key = '00000472bd130ac0ab0f52715bbdc600'
447+
end
448+
expect(Bugsnag.configuration.endpoints.notify).to eq(Bugsnag::Configuration::HUB_NOTIFY_ENDPOINT)
449+
expect(Bugsnag.configuration.endpoints.sessions).to eq(Bugsnag::Configuration::HUB_SESSION_ENDPOINT)
450+
end
430451
end
431452
end
432453

spec/spec_helper.rb

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,12 @@ def ruby_version_greater_equal?(target_version)
4747
end
4848

4949
RSpec.configure do |config|
50+
5051
config.order = "random"
5152
config.example_status_persistence_file_path = "#{Dir.tmpdir}/rspec_status"
5253
config.filter_run_when_matching(:focus)
5354

54-
config.before(:each) do
55+
config.before(:each) do |example|
5556
WebMock.stub_request(:post, "https://notify.bugsnag.com/")
5657
WebMock.stub_request(:post, "https://sessions.bugsnag.com/")
5758

@@ -61,12 +62,14 @@ def ruby_version_greater_equal?(target_version)
6162

6263
Thread.current[Bugsnag::SessionTracker::THREAD_SESSION] = nil
6364

64-
Bugsnag.configure do |bugsnag|
65-
bugsnag.api_key = "c9d60ae4c7e70c4b6c4ebd3e8056d2b8"
66-
bugsnag.release_stage = "production"
67-
bugsnag.delivery_method = :synchronous
68-
# silence logger in tests
69-
bugsnag.logger = Logger.new(StringIO.new)
65+
unless example.metadata[:no_configure]
66+
Bugsnag.configure do |bugsnag|
67+
bugsnag.api_key = "c9d60ae4c7e70c4b6c4ebd3e8056d2b8"
68+
bugsnag.release_stage = "production"
69+
bugsnag.delivery_method = :synchronous
70+
# silence logger in tests
71+
bugsnag.logger = Logger.new(StringIO.new)
72+
end
7073
end
7174
end
7275

0 commit comments

Comments
 (0)