Skip to content

Commit 39ad79e

Browse files
Merge pull request #399 from bugsnag/tms/hub-api-key
Set default endpoints based on API key
2 parents 8940cb6 + 46a749c commit 39ad79e

File tree

6 files changed

+72
-17
lines changed

6 files changed

+72
-17
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ Changelog
55

66
### Enhancements
77

8-
* Remove depricated `datetime.utcnow()` method call from utils class
8+
* Remove deprecated `datetime.utcnow()` method call from utils class
99
[#394](https://github.com/bugsnag/bugsnag-python/pull/394).
10+
* Set default endpoints based on API key
11+
[#399](https://github.com/bugsnag/bugsnag-php/pull/399)
1012

1113
## v4.7.1 (2024-05-22)
1214

bugsnag/client.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@ class Client:
3232
"""
3333

3434
def __init__(self, configuration: Optional[Configuration] = None,
35-
install_sys_hook=True, **kwargs):
35+
install_sys_hook=True, configure=True, **kwargs):
3636
self.configuration = configuration or Configuration() # type: Configuration # noqa: E501
3737
self.session_tracker = SessionTracker(self.configuration)
38-
self.configuration.configure(**kwargs)
38+
if configure:
39+
self.configuration.configure(**kwargs)
3940
self._context = ContextLocalState(self)
4041
self._request_tracker = RequestTracker()
4142

bugsnag/configuration.py

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@
3030
validate_int_setter,
3131
validate_path_setter
3232
)
33-
from bugsnag.delivery import (create_default_delivery, DEFAULT_ENDPOINT,
34-
DEFAULT_SESSIONS_ENDPOINT)
33+
from bugsnag.delivery import (create_default_delivery,
34+
DEFAULT_ENDPOINT,
35+
DEFAULT_SESSIONS_ENDPOINT,
36+
HUB_ENDPOINT,
37+
HUB_SESSIONS_ENDPOINT)
3538
from bugsnag.uwsgi import warn_if_running_uwsgi_without_threads
3639
from bugsnag.error import Error
3740

@@ -55,6 +58,11 @@
5558
_sentinel = object()
5659

5760

61+
def _is_hub_api_key(api_key: str) -> bool:
62+
hub_prefix = "00000"
63+
return api_key is not None and api_key.startswith(hub_prefix)
64+
65+
5866
class Configuration:
5967
"""
6068
Global app-level Bugsnag configuration settings.
@@ -83,8 +91,8 @@ def __init__(self, logger=_sentinel):
8391
"django.http.Http404",
8492
"django.http.response.Http404",
8593
]
86-
self.endpoint = DEFAULT_ENDPOINT
87-
self.session_endpoint = DEFAULT_SESSIONS_ENDPOINT
94+
self.endpoint = None
95+
self.session_endpoint = None
8896
self.auto_capture_sessions = True
8997
self.traceback_exclude_modules = []
9098

@@ -126,8 +134,6 @@ def configure(self, api_key=None, app_type=None, app_version=None,
126134
Validate and set configuration options. Will warn if an option is of an
127135
incorrect type.
128136
"""
129-
if api_key is not None:
130-
self.api_key = api_key
131137
if app_type is not None:
132138
self.app_type = app_type
133139
if app_version is not None:
@@ -140,8 +146,6 @@ def configure(self, api_key=None, app_type=None, app_version=None,
140146
self.auto_capture_sessions = auto_capture_sessions
141147
if delivery is not None:
142148
self.delivery = delivery
143-
if endpoint is not None:
144-
self.endpoint = endpoint
145149
if hostname is not None:
146150
self.hostname = hostname
147151
if ignore_classes is not None:
@@ -162,8 +166,6 @@ def configure(self, api_key=None, app_type=None, app_version=None,
162166
self.send_code = send_code
163167
if send_environment is not None:
164168
self.send_environment = send_environment
165-
if session_endpoint is not None:
166-
self.session_endpoint = session_endpoint
167169
if traceback_exclude_modules is not None:
168170
self.traceback_exclude_modules = traceback_exclude_modules
169171
if logger is not _sentinel:
@@ -175,6 +177,11 @@ def configure(self, api_key=None, app_type=None, app_version=None,
175177
if max_breadcrumbs is not None:
176178
self.max_breadcrumbs = max_breadcrumbs
177179

180+
# Default endpoints depend on the API key
181+
if api_key is not None:
182+
self.api_key = api_key
183+
self._initialize_endpoints(endpoint, session_endpoint, self.api_key)
184+
178185
return self
179186

180187
def get(self, name):
@@ -584,6 +591,26 @@ def _create_null_logger(self) -> logging.Logger:
584591

585592
return logger
586593

594+
def _initialize_endpoints(self, endpoint, session_endpoint, api_key):
595+
# Default endpoints depending on the API key, if not already set
596+
if (
597+
endpoint is None and
598+
session_endpoint is None and
599+
self.endpoint is None and
600+
self.session_endpoint is None
601+
):
602+
if _is_hub_api_key(api_key):
603+
self.endpoint = HUB_ENDPOINT
604+
self.session_endpoint = HUB_SESSIONS_ENDPOINT
605+
else:
606+
self.endpoint = DEFAULT_ENDPOINT
607+
self.session_endpoint = DEFAULT_SESSIONS_ENDPOINT
608+
# Do set endpoints if explicitly provided
609+
if endpoint is not None:
610+
self.endpoint = endpoint
611+
if session_endpoint is not None:
612+
self.session_endpoint = session_endpoint
613+
587614

588615
class RequestConfiguration:
589616
"""

bugsnag/delivery.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

2929
DEFAULT_ENDPOINT = 'https://notify.bugsnag.com'
3030
DEFAULT_SESSIONS_ENDPOINT = 'https://sessions.bugsnag.com'
31+
HUB_ENDPOINT = 'https://notify.insighthub.smartbear.com'
32+
HUB_SESSIONS_ENDPOINT = 'https://sessions.insighthub.smartbear.com'
3133

3234
__all__ = ('default_headers', 'Delivery')
3335

@@ -82,8 +84,10 @@ def deliver_sessions(self, config, payload: Any, options=None):
8284
"""
8385
Sends sessions to Bugsnag
8486
"""
85-
if (config.endpoint != DEFAULT_ENDPOINT and config.session_endpoint ==
86-
DEFAULT_SESSIONS_ENDPOINT):
87+
if ((config.endpoint != DEFAULT_ENDPOINT and
88+
config.session_endpoint == DEFAULT_SESSIONS_ENDPOINT) or
89+
(config.endpoint != HUB_ENDPOINT and
90+
config.session_endpoint == HUB_SESSIONS_ENDPOINT)):
8791
if not self.sent_session_warning:
8892
warnings.warn('The session endpoint has not been configured. '
8993
'No sessions will be sent to Bugsnag.')

bugsnag/legacy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from bugsnag.configuration import RequestConfiguration
88
from bugsnag.client import Client
99

10-
default_client = Client()
10+
default_client = Client(configure=False)
1111
configuration = default_client.configuration
1212
logger = configuration.logger
1313
ExcInfoType = Tuple[Type, Exception, types.TracebackType]

tests/test_configuration.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def test_hostname(self):
8383
def test_session_tracking_defaults(self):
8484
c = Configuration()
8585
self.assertTrue(c.auto_capture_sessions)
86-
self.assertEqual(c.session_endpoint, "https://sessions.bugsnag.com")
86+
self.assertEqual(c.session_endpoint, None)
8787

8888
def test_default_middleware_location(self):
8989
c = Configuration()
@@ -114,6 +114,16 @@ def test_validate_endpoint(self):
114114
c.configure(endpoint='https://notify.example.com')
115115
assert c.endpoint == 'https://notify.example.com'
116116

117+
def test_validate_endpoint_bugsnag_api_key(self):
118+
c = Configuration()
119+
c.configure(api_key='12312312312312312312312312321312')
120+
assert c.endpoint == 'https://notify.bugsnag.com'
121+
122+
def test_validate_endpoint_hub_api_key(self):
123+
c = Configuration()
124+
c.configure(api_key='00000312312312312312312312321312')
125+
assert c.endpoint == 'https://notify.insighthub.smartbear.com'
126+
117127
def test_validate_app_type(self):
118128
c = Configuration()
119129
assert c.app_type is None
@@ -410,6 +420,17 @@ def test_validate_session_endpoint(self):
410420
c.configure(session_endpoint='https://sessions.example.com')
411421
assert c.session_endpoint == 'https://sessions.example.com'
412422

423+
def test_validate_session_endpoint_bugsnag_api_key(self):
424+
c = Configuration()
425+
c.configure(api_key='12312312312312312312312312321312')
426+
assert c.session_endpoint == 'https://sessions.bugsnag.com'
427+
428+
def test_validate_session_endpoint_hub_api_key(self):
429+
c = Configuration()
430+
c.configure(api_key='00000312312312312312312312321312')
431+
assert (c.session_endpoint ==
432+
'https://sessions.insighthub.smartbear.com')
433+
413434
def test_validate_traceback_exclude_modules(self):
414435
c = Configuration()
415436
with pytest.warns(RuntimeWarning) as record:

0 commit comments

Comments
 (0)