Skip to content
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

ref(api): Remove store endpoint #2656

Merged
merged 31 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
b4d5997
Remove store endpoint
szokeasaurusrex Jan 18, 2024
9335bd0
Fix linter error
szokeasaurusrex Jan 19, 2024
df4bdc8
Add stacklevel to warn call
szokeasaurusrex Jan 19, 2024
f5fb97d
Remove `store_api_url` test, update `get_api_url` test
szokeasaurusrex Jan 19, 2024
8f5871a
Fix mypy
szokeasaurusrex Jan 19, 2024
9a7904d
Correct import
szokeasaurusrex Jan 19, 2024
805d849
Use `Enum` instead of `StrEnum`
szokeasaurusrex Jan 19, 2024
e1e6437
Update `envelope.py`
szokeasaurusrex Jan 22, 2024
addd2a8
Remove `Envelope.events` calls
szokeasaurusrex Jan 22, 2024
5b547d1
Fix `capture_events_forksafe`
szokeasaurusrex Jan 22, 2024
7105849
Hopefully fix circular import
szokeasaurusrex Jan 22, 2024
65d830b
Manually set TestTransport
szokeasaurusrex Jan 22, 2024
e681bdb
Fix circular import
szokeasaurusrex Jan 22, 2024
ba02bc4
Revert "Fix circular import"
szokeasaurusrex Jan 22, 2024
e4b9e99
Revert "Hopefully fix circular import"
szokeasaurusrex Jan 22, 2024
bc4627f
Move EndpointType to top of file
szokeasaurusrex Jan 22, 2024
61db076
Fix AWS tests
szokeasaurusrex Jan 22, 2024
607c7a9
Remove TODO comment
szokeasaurusrex Jan 22, 2024
a93d435
Undo ABC change
szokeasaurusrex Jan 22, 2024
18b13ca
Update
szokeasaurusrex Jan 23, 2024
1ac13a5
Rename envelope_item to envelope_items
szokeasaurusrex Jan 24, 2024
df28d81
Remove unneeded import statement
szokeasaurusrex Jan 24, 2024
7dd6a2a
Updated migration guide
szokeasaurusrex Jan 24, 2024
ff425fa
Merge branch 'sentry-sdk-2.0' into szokeasaurusrex/remove-store-endpoint
szokeasaurusrex Jan 24, 2024
cf3b0c2
Put back `has_tracing_enabled` check
szokeasaurusrex Jan 25, 2024
c419c78
Remove test for replay context
szokeasaurusrex Jan 25, 2024
6ee30a4
Merge branch 'sentry-sdk-2.0' into szokeasaurusrex/remove-store-endpoint
szokeasaurusrex Jan 29, 2024
2a15dc5
Update MIGRATION_GUIDE.md
szokeasaurusrex Jan 29, 2024
2ab3ef4
Auto-enable more integrations (#2671)
sentrivana Jan 29, 2024
1e65a98
Remove deprecated code (#2666)
szokeasaurusrex Jan 29, 2024
184dd9c
Merge branch 'sentry-sdk-2.0' into szokeasaurusrex/remove-store-endpoint
szokeasaurusrex Jan 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion sentry_sdk/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
"monitor",
]
SessionStatus = Literal["ok", "exited", "crashed", "abnormal"]
EndpointType = Literal["store", "envelope"]

DurationUnit = Literal[
"nanosecond",
Expand Down
66 changes: 24 additions & 42 deletions sentry_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
logger,
)
from sentry_sdk.serializer import serialize
from sentry_sdk.tracing import trace, has_tracing_enabled
from sentry_sdk.tracing import trace
from sentry_sdk.transport import make_transport
from sentry_sdk.consts import (
DEFAULT_MAX_VALUE_LENGTH,
Expand Down Expand Up @@ -603,58 +603,40 @@
):
return None

tracing_enabled = has_tracing_enabled(self.options)
attachments = hint.get("attachments")

trace_context = event_opt.get("contexts", {}).get("trace") or {}
dynamic_sampling_context = trace_context.pop("dynamic_sampling_context", {})

# If tracing is enabled all events should go to /envelope endpoint.
# If no tracing is enabled only transactions, events with attachments, and checkins should go to the /envelope endpoint.
should_use_envelope_endpoint = (
tracing_enabled
or is_transaction
or is_checkin
or bool(attachments)
or bool(self.spotlight)
)
if should_use_envelope_endpoint:
headers = {
"event_id": event_opt["event_id"],
"sent_at": format_timestamp(datetime.now(timezone.utc)),
}

if dynamic_sampling_context:
headers["trace"] = dynamic_sampling_context

envelope = Envelope(headers=headers)

if is_transaction:
if profile is not None:
envelope.add_profile(profile.to_json(event_opt, self.options))
envelope.add_transaction(event_opt)
elif is_checkin:
envelope.add_checkin(event_opt)
else:
envelope.add_event(event_opt)
headers = {
"event_id": event_opt["event_id"],
"sent_at": format_timestamp(datetime.now(timezone.utc)),
}

for attachment in attachments or ():
envelope.add_item(attachment.to_envelope_item())
if dynamic_sampling_context:
headers["trace"] = dynamic_sampling_context

if self.spotlight:
self.spotlight.capture_envelope(envelope)
envelope = Envelope(headers=headers)

if self.transport is None:
return None
if is_transaction:
if profile is not None:
envelope.add_profile(profile.to_json(event_opt, self.options))
envelope.add_transaction(event_opt)
elif is_checkin:
envelope.add_checkin(event_opt)

Check warning on line 626 in sentry_sdk/client.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/client.py#L626

Added line #L626 was not covered by tests
else:
envelope.add_event(event_opt)

self.transport.capture_envelope(envelope)
for attachment in attachments or ():
envelope.add_item(attachment.to_envelope_item())

Check warning on line 631 in sentry_sdk/client.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/client.py#L631

Added line #L631 was not covered by tests

else:
if self.transport is None:
return None
if self.spotlight:
self.spotlight.capture_envelope(envelope)

Check warning on line 634 in sentry_sdk/client.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/client.py#L634

Added line #L634 was not covered by tests

if self.transport is None:
return None

Check warning on line 637 in sentry_sdk/client.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/client.py#L637

Added line #L637 was not covered by tests

# All other events go to the legacy /store/ endpoint (will be removed in the future).
self.transport.capture_event(event_opt)
self.transport.capture_envelope(envelope)

return event_id

Expand Down
11 changes: 11 additions & 0 deletions sentry_sdk/consts.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from enum import StrEnum
from sentry_sdk._types import TYPE_CHECKING

# up top to prevent circular import due to integration import
Expand Down Expand Up @@ -236,6 +237,16 @@ class OP:
SOCKET_DNS = "socket.dns"


class EndpointType(StrEnum):
"""
The type of an endpoint. This is an enum, rather than a constant, for historical reasons
(the old /store endpoint). The enum also preserve future compatibility, in case we ever
have a new endpoint.
"""

ENVELOPE = "envelope"


# This type exists to trick mypy and PyCharm into thinking `init` and `Client`
# take these arguments (even though they take opaque **kwargs)
class ClientConstructor:
Expand Down
11 changes: 6 additions & 5 deletions sentry_sdk/envelope.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,12 @@ def add_item(

def get_event(self):
# type: (...) -> Optional[Event]
for items in self.items:
event = items.get_event()
if event is not None:
return event
return None
return next(self.events, None)

@property
def events(self):
# type: () -> Iterator[Event]
return (item.get_event() for item in self.items if item.get_event() is not None)

def get_transaction_event(self):
# type: (...) -> Optional[Event]
Expand Down
123 changes: 52 additions & 71 deletions sentry_sdk/transport.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
from __future__ import print_function
Copy link
Contributor

Choose a reason for hiding this comment

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

We don't need this on the 2.0 branch because there's no Python 2 support anymore

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch – I think Git somehow put this line in when I was splitting the Transport ABC stuff into a separate PR


from abc import ABC, abstractmethod
import io
import warnings
import urllib3
import certifi
import gzip
import time
from datetime import datetime, timedelta, timezone
from collections import defaultdict

from sentry_sdk.utils import Dsn, logger, capture_internal_exceptions, json_dumps
from sentry_sdk.consts import EndpointType
from sentry_sdk.utils import Dsn, logger, capture_internal_exceptions
from sentry_sdk.worker import BackgroundWorker
from sentry_sdk.envelope import Envelope, Item, PayloadRef
from sentry_sdk._types import TYPE_CHECKING
Expand All @@ -25,7 +30,7 @@
from urllib3.poolmanager import PoolManager
from urllib3.poolmanager import ProxyManager

from sentry_sdk._types import Event, EndpointType
from sentry_sdk._types import Event

Check warning on line 33 in sentry_sdk/transport.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/transport.py#L33

Added line #L33 was not covered by tests

DataCategory = Optional[str]

Expand All @@ -35,12 +40,18 @@
from urllib import getproxies # type: ignore


class Transport:
class Transport(ABC):
"""Baseclass for all transports.

A transport is used to send an event to sentry.
"""

# TODO: For the items without default implementations, we may wish to mark them as abstract methods,
# or provide a default implementation. We could probably move implementations of some of the
# methods (such as `record_lost_event`) from the HttpTransport class to this class.
# However, marking methods as abstract methods is a breaking change, so we should consider
# whether we would like to make the change for the SDK 2.0 release.

szokeasaurusrex marked this conversation as resolved.
Show resolved Hide resolved
parsed_dsn = None # type: Optional[Dsn]

def __init__(
Expand All @@ -58,11 +69,22 @@
):
# type: (...) -> None
"""
DEPRECATED: Please use capture_envelope instead.

This gets invoked with the event dictionary when an event should
be sent to sentry.
"""
raise NotImplementedError()

warnings.warn(

Check warning on line 78 in sentry_sdk/transport.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/transport.py#L78

Added line #L78 was not covered by tests
"capture_event is deprecated, please use capture_envelope instead!",
DeprecationWarning,
)

envelope = Envelope()
envelope.add_event(event)
self.capture_envelope(envelope)

Check warning on line 85 in sentry_sdk/transport.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/transport.py#L83-L85

Added lines #L83 - L85 were not covered by tests

@abstractmethod
def capture_envelope(
self, envelope # type: Envelope
):
Expand All @@ -71,11 +93,10 @@
Send an envelope to Sentry.

Envelopes are a data container format that can hold any type of data
submitted to Sentry. We use it for transactions and sessions, but
regular "error" events should go through `capture_event` for backwards
compat.
submitted to Sentry. We use it to send all event data (including errors,
transactions, crons checkins, etc.) to Sentry.
"""
raise NotImplementedError()
pass

Check warning on line 99 in sentry_sdk/transport.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/transport.py#L99

Added line #L99 was not covered by tests

def flush(
self,
Expand Down Expand Up @@ -216,7 +237,7 @@
self,
body, # type: bytes
headers, # type: Dict[str, str]
endpoint_type="store", # type: EndpointType
endpoint_type=EndpointType.ENVELOPE, # type: EndpointType
envelope=None, # type: Optional[Envelope]
):
# type: (...) -> None
Expand Down Expand Up @@ -333,46 +354,6 @@
# type: () -> bool
return not (self._is_worker_full() or self._is_rate_limited())

def _send_event(
self, event # type: Event
):
# type: (...) -> None

if self._check_disabled("error"):
self.on_dropped_event("self_rate_limits")
self.record_lost_event("ratelimit_backoff", data_category="error")
return None

body = io.BytesIO()
if self._compresslevel == 0:
body.write(json_dumps(event))
else:
with gzip.GzipFile(
fileobj=body, mode="w", compresslevel=self._compresslevel
) as f:
f.write(json_dumps(event))

assert self.parsed_dsn is not None
logger.debug(
"Sending event, type:%s level:%s event_id:%s project:%s host:%s"
% (
event.get("type") or "null",
event.get("level") or "null",
event.get("event_id") or "null",
self.parsed_dsn.project_id,
self.parsed_dsn.host,
)
)

headers = {
"Content-Type": "application/json",
}
if self._compresslevel > 0:
headers["Content-Encoding"] = "gzip"

self._send_request(body.getvalue(), headers=headers)
return None

def _send_envelope(
self, envelope # type: Envelope
):
Expand Down Expand Up @@ -430,7 +411,7 @@
self._send_request(
body.getvalue(),
headers=headers,
endpoint_type="envelope",
endpoint_type=EndpointType.ENVELOPE,
envelope=envelope,
)
return None
Expand Down Expand Up @@ -501,23 +482,6 @@
else:
return urllib3.PoolManager(**opts)

def capture_event(
self, event # type: Event
):
# type: (...) -> None
hub = self.hub_cls.current

def send_event_wrapper():
# type: () -> None
with hub:
with capture_internal_exceptions():
self._send_event(event)
self._flush_client_reports()

if not self._worker.submit(send_event_wrapper):
self.on_dropped_event("full_queue")
self.record_lost_event("queue_overflow", data_category="error")

def capture_envelope(
self, envelope # type: Envelope
):
Expand Down Expand Up @@ -555,6 +519,11 @@


class _FunctionTransport(Transport):
"""
DEPRECATED: Users wishing to provide a custom transport should subclass
the Transport class, rather than providing a function.
"""

def __init__(
self, func # type: Callable[[Event], None]
):
Expand All @@ -569,19 +538,31 @@
self._func(event)
return None

def capture_envelope(self, envelope: Envelope) -> None:
# Since function transports expect to be called with an event, we need
# to iterate over the envelope and call the function for each event, via
# the deprecated capture_event method.
for event in envelope.events:
self.capture_event(event)

Check warning on line 546 in sentry_sdk/transport.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/transport.py#L546

Added line #L546 was not covered by tests


def make_transport(options):
# type: (Dict[str, Any]) -> Optional[Transport]
ref_transport = options["transport"]

# If no transport is given, we use the http transport class
if ref_transport is None:
transport_cls = HttpTransport # type: Type[Transport]
elif isinstance(ref_transport, Transport):
# By default, we use the http transport class
transport_cls = HttpTransport # type: Type[Transport]

if isinstance(ref_transport, Transport):
return ref_transport
elif isinstance(ref_transport, type) and issubclass(ref_transport, Transport):
transport_cls = ref_transport
elif callable(ref_transport):
warnings.warn(

Check warning on line 561 in sentry_sdk/transport.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/transport.py#L561

Added line #L561 was not covered by tests
"Function transports are deprecated and will be removed in a future release."
"Please provide a Transport instance or subclass, instead.",
DeprecationWarning,
)
return _FunctionTransport(ref_transport)

# if a transport class is given only instantiate it if the dsn is not
Expand Down
15 changes: 3 additions & 12 deletions sentry_sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import sentry_sdk
from sentry_sdk._compat import PY37
from sentry_sdk._types import TYPE_CHECKING
from sentry_sdk.consts import DEFAULT_MAX_VALUE_LENGTH
from sentry_sdk.consts import DEFAULT_MAX_VALUE_LENGTH, EndpointType

if TYPE_CHECKING:
from types import FrameType, TracebackType
Expand All @@ -46,7 +46,7 @@
Union,
)

from sentry_sdk._types import EndpointType, ExcInfo
from sentry_sdk._types import ExcInfo

Check warning on line 49 in sentry_sdk/utils.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/utils.py#L49

Added line #L49 was not covered by tests


epoch = datetime(1970, 1, 1)
Expand Down Expand Up @@ -305,17 +305,8 @@
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
self, type=EndpointType.ENVELOPE # type: EndpointType
):
# type: (...) -> str
"""Returns the API url for storing events."""
Expand Down