Skip to content

Commit dd25d15

Browse files
author
Steve Kirkland
committed
WIP
1 parent f771d0b commit dd25d15

File tree

4 files changed

+69
-12
lines changed

4 files changed

+69
-12
lines changed

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 2024)
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

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
530+
session_set = configuration.session_endpoint &&
531+
configuration.session_endpoint != Bugsnag::Configuration::DEFAULT_SESSION_ENDPOINT &&
532+
configuration.session_endpoint != Bugsnag::Configuration::HUB_SESSION
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: 24 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 = "https://notify.insighthub.smartbear.com"
203+
HUB_SESSION = "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,22 @@ 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+
550+
puts @endpoints.inspect
551+
552+
if @endpoints.notify.nil? && @endpoints.sessions.nil?
553+
if self.hub_api_key?
554+
self.endpoints = EndpointConfiguration.new(HUB_NOTIFY, HUB_SESSION)
555+
else
556+
self.endpoints = EndpointConfiguration.new(DEFAULT_NOTIFY_ENDPOINT, DEFAULT_SESSION_ENDPOINT)
557+
end
558+
end
559+
end
560+
543561
##
544562
# Sets the notification and session endpoints
545563
#
@@ -753,5 +771,9 @@ def default_hostname
753771
# Send the heroku dyno name instead of hostname if available
754772
ENV["DYNO"] || Socket.gethostname;
755773
end
774+
775+
def hub_api_key?
776+
@api_key && @api_key.start_with?(HUB_PREFIX)
777+
end
756778
end
757779
end

spec/configuration_spec.rb

Lines changed: 30 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,24 @@ 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" do
436+
puts "test 1"
437+
Bugsnag.configure do |config|
438+
config.api_key = '00002472bd130ac0ab0f52715bbdc600'
439+
end
440+
expect(subject.endpoints.notify).to eq(Bugsnag::Configuration::DEFAULT_NOTIFY_ENDPOINT)
441+
expect(subject.endpoints.sessions).to eq(Bugsnag::Configuration::DEFAULT_SESSION_ENDPOINT)
442+
end
443+
444+
it "uses the hub endpoints for hub API keys" do
445+
puts "test 2"
446+
Bugsnag.configure do |config|
447+
config.api_key = '00000472bd130ac0ab0f52715bbdc600'
448+
end
449+
expect(subject.endpoints.notify).to eq(Bugsnag::Configuration::HUB_NOTIFY)
450+
expect(subject.endpoints.sessions).to eq(Bugsnag::Configuration::HUB_SESSION)
451+
end
430452
end
431453
end
432454

0 commit comments

Comments
 (0)