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 20 commits
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)
else:
envelope.add_event(event_opt)

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

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

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
13 changes: 13 additions & 0 deletions sentry_sdk/consts.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
from enum import Enum
from sentry_sdk._types import TYPE_CHECKING

# up top to prevent circular import due to integration import
DEFAULT_MAX_VALUE_LENGTH = 1024


# Also needs to be at the top to prevent circular import
class EndpointType(Enum):
"""
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"


if TYPE_CHECKING:
import sentry_sdk

Expand Down
6 changes: 6 additions & 0 deletions sentry_sdk/envelope.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ def parse_json(data):


class Envelope:
"""
Represents a Sentry Envelope. The calling code is responsible for adhering to the constraints
documented in the Sentry docs: https://develop.sentry.dev/sdk/envelopes/#data-model. In particular,
each envelope may have at most one Item with type "event" or "transaction" (but not both).
"""

def __init__(
self,
headers=None, # type: Optional[Dict[str, Any]]
Expand Down
14 changes: 1 addition & 13 deletions sentry_sdk/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from sentry_sdk.tracing_utils import (
Baggage,
extract_sentrytrace_data,
has_tracing_enabled,
normalize_incoming_data,
)
from sentry_sdk.tracing import (
Expand Down Expand Up @@ -612,22 +611,11 @@ def _apply_contexts_to_event(self, event, hint, options):

# Add "trace" context
if contexts.get("trace") is None:
if has_tracing_enabled(options) and self._span is not None:
if self._span is not None:
contexts["trace"] = self._span.get_trace_context()
else:
contexts["trace"] = self.get_trace_context()

# Add "reply_id" context
try:
replay_id = contexts["trace"]["dynamic_sampling_context"]["replay_id"]
except (KeyError, TypeError):
replay_id = None

if replay_id is not None:
contexts["replay"] = {
"replay_id": replay_id,
}

szokeasaurusrex marked this conversation as resolved.
Show resolved Hide resolved
@_disable_capture
def apply_to_event(
self,
Expand Down
132 changes: 59 additions & 73 deletions sentry_sdk/transport.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
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


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 +29,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 32 in sentry_sdk/transport.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/transport.py#L32

Added line #L32 was not covered by tests

DataCategory = Optional[str]

Expand Down Expand Up @@ -58,10 +62,21 @@
):
# 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 71 in sentry_sdk/transport.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/transport.py#L71

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

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

Check warning on line 79 in sentry_sdk/transport.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/transport.py#L77-L79

Added lines #L77 - L79 were not covered by tests

def capture_envelope(
self, envelope # type: Envelope
Expand All @@ -71,9 +86,8 @@
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()

Expand All @@ -83,13 +97,23 @@
callback=None, # type: Optional[Any]
):
# type: (...) -> None
"""Wait `timeout` seconds for the current events to be sent out."""
pass
"""
Wait `timeout` seconds for the current events to be sent out.

The default implementation is a no-op, since this method may only be relevant to some transports.
Subclasses should override this method if necessary.
"""
return None

def kill(self):
# type: () -> None
"""Forcefully kills the transport."""
pass
"""
Forcefully kills the transport.

The default implementation is a no-op, since this method may only be relevant to some transports.
Subclasses should override this method if necessary.
"""
return None

def record_lost_event(
self,
Expand Down Expand Up @@ -216,7 +240,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 +357,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 +414,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 +485,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 +522,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 +541,33 @@
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.
event = envelope.get_event()
if event is not None:
self.capture_event(event)


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(
"Function transports are deprecated and will be removed in a future release."
"Please provide a Transport instance or subclass, instead.",
DeprecationWarning,
stacklevel=2,
)
return _FunctionTransport(ref_transport)

# if a transport class is given only instantiate it if the dsn is not
Expand Down