From a22941d2ecf61611b856102cfa1ec13a56350c3c Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Tue, 23 Jun 2020 09:43:04 +0200 Subject: [PATCH 1/4] feat: Send envelopes to the envelope endpoint --- sentry_sdk/transport.py | 4 +++- sentry_sdk/utils.py | 6 +++--- tests/utils/test_general.py | 2 +- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/sentry_sdk/transport.py b/sentry_sdk/transport.py index c6f926a353..08b8d361f9 100644 --- a/sentry_sdk/transport.py +++ b/sentry_sdk/transport.py @@ -163,6 +163,7 @@ def _send_request( self, body, # type: bytes headers, # type: Dict[str, str] + endpoint_type="store", ): # type: (...) -> None headers.update( @@ -172,7 +173,7 @@ def _send_request( } ) response = self._pool.request( - "POST", str(self._auth.store_api_url), body=body, headers=headers + "POST", str(self._auth.get_api_url(endpoint_type)), body=body, headers=headers ) try: @@ -258,6 +259,7 @@ def _send_envelope( "Content-Type": "application/x-sentry-envelope", "Content-Encoding": "gzip", }, + endpoint_type="envelope", ) return None diff --git a/sentry_sdk/utils.py b/sentry_sdk/utils.py index fef96adcf6..795d73cfe4 100644 --- a/sentry_sdk/utils.py +++ b/sentry_sdk/utils.py @@ -197,15 +197,15 @@ def __init__( self.version = version self.client = client - @property - def store_api_url(self): + def get_api_url(self, type='store'): # type: () -> str """Returns the API url for storing events.""" - return "%s://%s%sapi/%s/store/" % ( + return "%s://%s%sapi/%s/%s/" % ( self.scheme, self.host, self.path, self.project_id, + type, ) def to_header(self, timestamp=None): diff --git a/tests/utils/test_general.py b/tests/utils/test_general.py index ff6e5f5430..a203a91b5a 100644 --- a/tests/utils/test_general.py +++ b/tests/utils/test_general.py @@ -97,7 +97,7 @@ def test_filename(): def test_parse_dsn_paths(given, expected): dsn = Dsn(given) auth = dsn.to_auth() - assert auth.store_api_url == expected + assert auth.get_api_url() == expected @pytest.mark.parametrize( From 105149bf4d6a0545ff150b9c6e0797191a762448 Mon Sep 17 00:00:00 2001 From: Markus Unterwaditzer Date: Tue, 23 Jun 2020 10:19:40 +0200 Subject: [PATCH 2/4] add typing --- sentry_sdk/_types.py | 1 + sentry_sdk/transport.py | 9 ++++++--- sentry_sdk/utils.py | 8 +++++--- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/sentry_sdk/_types.py b/sentry_sdk/_types.py index 74020aea57..7b727422a1 100644 --- a/sentry_sdk/_types.py +++ b/sentry_sdk/_types.py @@ -35,3 +35,4 @@ "default", "error", "crash", "transaction", "security", "attachment", "session" ] SessionStatus = Literal["ok", "exited", "crashed", "abnormal"] + EndpointType = Literal["store", "envelope"] diff --git a/sentry_sdk/transport.py b/sentry_sdk/transport.py index 08b8d361f9..449a84532f 100644 --- a/sentry_sdk/transport.py +++ b/sentry_sdk/transport.py @@ -27,7 +27,7 @@ from urllib3.poolmanager import PoolManager # type: ignore from urllib3.poolmanager import ProxyManager - from sentry_sdk._types import Event + from sentry_sdk._types import Event, EndpointType DataCategory = Optional[str] @@ -163,7 +163,7 @@ def _send_request( self, body, # type: bytes headers, # type: Dict[str, str] - endpoint_type="store", + endpoint_type="store", # type: EndpointType ): # type: (...) -> None headers.update( @@ -173,7 +173,10 @@ def _send_request( } ) response = self._pool.request( - "POST", str(self._auth.get_api_url(endpoint_type)), body=body, headers=headers + "POST", + str(self._auth.get_api_url(endpoint_type)), + body=body, + headers=headers, ) try: diff --git a/sentry_sdk/utils.py b/sentry_sdk/utils.py index 795d73cfe4..1c0143332f 100644 --- a/sentry_sdk/utils.py +++ b/sentry_sdk/utils.py @@ -25,7 +25,7 @@ from typing import Union from typing import Type - from sentry_sdk._types import ExcInfo + from sentry_sdk._types import ExcInfo, EndpointType epoch = datetime(1970, 1, 1) @@ -197,8 +197,10 @@ def __init__( self.version = version self.client = client - def get_api_url(self, type='store'): - # type: () -> str + def get_api_url( + self, type="store" # type: EndpointType + ): + # type: (...) -> str """Returns the API url for storing events.""" return "%s://%s%sapi/%s/%s/" % ( self.scheme, From 8441a747403c8988ed6e7e2fdb794615401b7532 Mon Sep 17 00:00:00 2001 From: Rodolfo Carvalho Date: Thu, 25 Jun 2020 14:56:42 +0200 Subject: [PATCH 3/4] fix: Re-add Auth.store_api_url While we don't consider Auth to be part of the public API, the contract is not very clear. This change is to prevent unnecessarily breaking downstream uses of the Auth.store_api_url property. Since we don't have any existing use of Python's DeprecationWarning, nor any other system in place to communicate deprecation, we start with just a note in the docstring. --- sentry_sdk/utils.py | 9 +++++++++ tests/utils/test_general.py | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/sentry_sdk/utils.py b/sentry_sdk/utils.py index 1c0143332f..74bbc5576a 100644 --- a/sentry_sdk/utils.py +++ b/sentry_sdk/utils.py @@ -197,6 +197,15 @@ def __init__( self.version = version self.client = client + @property + def store_api_url(self): + # type: () -> str + """Returns the API url for storing events. + + Deprecated: use get_api_url instead. + """ + return self.get_api_url(type="store") + def get_api_url( self, type="store" # type: EndpointType ): diff --git a/tests/utils/test_general.py b/tests/utils/test_general.py index a203a91b5a..ff6e5f5430 100644 --- a/tests/utils/test_general.py +++ b/tests/utils/test_general.py @@ -97,7 +97,7 @@ def test_filename(): def test_parse_dsn_paths(given, expected): dsn = Dsn(given) auth = dsn.to_auth() - assert auth.get_api_url() == expected + assert auth.store_api_url == expected @pytest.mark.parametrize( From ad8464cfe05a547a887e88fff9e64fdee2c60627 Mon Sep 17 00:00:00 2001 From: Rodolfo Carvalho Date: Thu, 25 Jun 2020 15:24:10 +0200 Subject: [PATCH 4/4] test: Auth.get_api_url --- tests/utils/test_general.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/tests/utils/test_general.py b/tests/utils/test_general.py index ff6e5f5430..b80e47859a 100644 --- a/tests/utils/test_general.py +++ b/tests/utils/test_general.py @@ -84,20 +84,31 @@ def test_filename(): @pytest.mark.parametrize( - "given,expected", + "given,expected_store,expected_envelope", [ - ("https://foobar@sentry.io/123", "https://sentry.io/api/123/store/"), - ("https://foobar@sentry.io/bam/123", "https://sentry.io/bam/api/123/store/"), + ( + "https://foobar@sentry.io/123", + "https://sentry.io/api/123/store/", + "https://sentry.io/api/123/envelope/", + ), + ( + "https://foobar@sentry.io/bam/123", + "https://sentry.io/bam/api/123/store/", + "https://sentry.io/bam/api/123/envelope/", + ), ( "https://foobar@sentry.io/bam/baz/123", "https://sentry.io/bam/baz/api/123/store/", + "https://sentry.io/bam/baz/api/123/envelope/", ), ], ) -def test_parse_dsn_paths(given, expected): +def test_parse_dsn_paths(given, expected_store, expected_envelope): dsn = Dsn(given) auth = dsn.to_auth() - assert auth.store_api_url == expected + assert auth.store_api_url == expected_store + assert auth.get_api_url("store") == expected_store + assert auth.get_api_url("envelope") == expected_envelope @pytest.mark.parametrize(