8000 feat: Send envelopes to the envelope endpoint (#730) · adamchainz/sentry-python@e083488 · GitHub
[go: up one dir, main page]

Skip to content

Commit e083488

Browse files
mitsuhikountitakerrhcarvalho
authored
feat: Send envelopes to the envelope endpoint (getsentry#730)
Add Auth.get_api_url and keep Auth.store_api_url, with a deprecation notice. While we don't consider Auth to be part of the public API, the contract is not very clear. Auth.store_api_url is kept to prevent unnecessarily breaking downstream uses. 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. Co-authored-by: Markus Unterwaditzer <markus@unterwaditzer.net> Co-authored-by: Rodolfo Carvalho <rodolfo.carvalho@sentry.io>
1 parent e062181 commit e083488

File tree

4 files changed

+37
-9
lines changed

4 files changed

+37
-9
lines changed

sentry_sdk/_types.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,4 @@
3535
"default", "error", "crash", "transaction", "security", "attachment", "session"
3636
]
3737
SessionStatus = Literal["ok", "exited", "crashed", "abnormal"]
38+
EndpointType = Literal["store", "envelope"]

sentry_sdk/transport.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from urllib3.poolmanager import PoolManager # type: ignore
2828
from urllib3.poolmanager import ProxyManager
2929

30-
from sentry_sdk._types import Event
30+
from sentry_sdk._types import Event, EndpointType
3131

3232
DataCategory = Optional[str]
3333

@@ -163,6 +163,7 @@ def _send_request(
163163
self,
164164
body, # type: bytes
165165
headers, # type: Dict[str, str]
166+
endpoint_type="store", # type: EndpointType
166167
):
167168
# type: (...) -> None
168169
headers.update(
@@ -172,7 +173,10 @@ def _send_request(
172173
}
173174
)
174175
response = self._pool.request(
175-
"POST", str(self._auth.store_api_url), body=body, headers=headers
176+
"POST",
177+
str(self._auth.get_api_url(endpoint_type)),
178+
body=body,
179+
headers=headers,
176180
)
177181

178182
try:
@@ -258,6 +262,7 @@ def _send_envelope(
258262
"Content-Type": "application/x-sentry-envelope",
259263
"Content-Encoding": "gzip",
260264
},
265+
endpoint_type="envelope",
261266
)
262267
return None
263268

sentry_sdk/utils.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from typing import Union
2626
from typing import Type
2727

28-
from sentry_sdk._types import ExcInfo
28+
from sentry_sdk._types import ExcInfo, EndpointType
2929

3030
epoch = datetime(1970, 1, 1)
3131

@@ -200,12 +200,23 @@ def __init__(
200200
@property
201201
def store_api_url(self):
202202
# type: () -> str
203+
"""Returns the API url for storing events.
204+
205+
Deprecated: use get_api_url instead.
206+
"""
207+
return self.get_api_url(type="store")
208+
209+
def get_api_url(
210+
self, type="store" # type: EndpointType
211+
):
212+
# type: (...) -> str
203213
"""Returns the API url for storing events."""
204-
return "%s://%s%sapi/%s/store/" % (
214+
return "%s://%s%sapi/%s/%s/" % (
205215
self.scheme,
206216
self.host,
207217
self.path,
208218
self.project_id,
219+
type,
209220
)
210221

211222
def to_header(self, timestamp=None):

tests/utils/test_general.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,20 +84,31 @@ def test_filename():
8484

8585

8686
@pytest.mark.parametrize(
87-
"given,expected",
87+
"given,expected_store,expected_envelope",
8888
[
89-
("https://foobar@sentry.io/123", "https://sentry.io/api/123/store/"),
90-
("https://foobar@sentry.io/bam/123", "https://sentry.io/bam/api/123/store/"),
89+
(
90+
"https://foobar@sentry.io/123",
91+
"https://sentry.io/api/123/store/",
92+
"https://sentry.io/api/123/envelope/",
93+
),
94+
(
95+
"https://foobar@sentry.io/bam/123",
96+
"https://sentry.io/bam/api/123/store/",
97+
"https://sentry.io/bam/api/123/envelope/",
98+
),
9199
(
92100
"https://foobar@sentry.io/bam/baz/123",
93101
"https://sentry.io/bam/baz/api/123/store/",
102+
"https://sentry.io/bam/baz/api/123/envelope/",
94103
),
95104
],
96105
)
97-
def test_parse_dsn_paths(given, expected):
106+
def test_parse_dsn_paths(given, expected_store, expected_envelope):
98107
dsn = Dsn(given)
99108
auth = dsn.to_auth()
100-
assert auth.store_api_url == expected
109+
assert auth.store_api_url == expected_store
110+
assert auth.get_api_url("store") == expected_store
111+
assert auth.get_api_url("envelope") == expected_envelope
101112

102113

103114
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)
0