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

[WIP] Refactor transport.py #2660

Closed
wants to merge 2 commits into from
Closed
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: 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 @@ def capture_event(
):
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

# 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
Loading
Loading