8000 feat: Send envelopes to the envelope endpoint by mitsuhiko · Pull Request #730 · getsentry/sentry-python · GitHub
[go: up one dir, main page]

Skip to content

feat: Send envelopes to the envelope endpoint #730

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sentry_sdk/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@
"default", "error", "crash", "transaction", "security", "attachment", "session"
]
SessionStatus = Literal["ok", "exited", "crashed", "abnormal"]
EndpointType = Literal["store", "envelope"]
9 changes: 7 additions & 2 deletions sentry_sdk/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down Expand Up @@ -163,6 +163,7 @@ def _send_request(
self,
body, # type: bytes
headers, # type: Dict[str, str]
endpoint_type="store", # type: EndpointType
):
# type: (...) -> None
headers.update(
Expand All @@ -172,7 +173,10 @@ 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)),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
str(self._auth.get_api_url(endpoint_type)),
self._auth.get_api_url(endpoint_type),

Why do we need str here? If we need some Py2/3 compatibility and requests limits what it takes, we could handle this in get_api_url itself, otherwise all callers would need to make adjustments (and this seems to be the only caller other than test code).

body=body,
headers=headers,
)

try:
Expand Down Expand Up @@ -258,6 +262,7 @@ def _send_envelope(
"Content-Type": "application/x-sentry-envelope",
"Content-Encoding": "gzip",
},
endpoint_type="envelope",
)
return None

Expand Down
15 changes: 13 additions & 2 deletions sentry_sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -200,12 +200,23 @@ def __init__(
@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
):
# 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):
Expand Down
21 changes: 16 additions & 5 deletions tests/utils/test_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
0