Skip to content

feat: Send transactions in envelopes #729

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 1 commit 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
24 changes: 20 additions & 4 deletions sentry_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@

from sentry_sdk._compat import string_types, text_type, iteritems
from sentry_sdk.utils import (
handle_in_app,
get_type_name,
capture_internal_exceptions,
current_stacktrace,
disable_capture_event,
format_timestamp,
get_type_name,
handle_in_app,
logger,
)
from sentry_sdk.serializer import serialize
Expand All @@ -20,7 +21,7 @@
from sentry_sdk.integrations import setup_integrations
from sentry_sdk.utils import ContextVar
from sentry_sdk.sessions import SessionFlusher
from sentry_sdk.envelope import Envelope
from sentry_sdk.envelope import Envelope, Item, PayloadRef

from sentry_sdk._types import MYPY

Expand Down Expand Up @@ -334,7 +335,22 @@ def capture_event(
if session:
self._update_session_from_event(session, event)

self.transport.capture_event(event_opt)
if event_opt.get("type") == "transaction":
# Transactions should go to the /envelope/ endpoint.
self.transport.capture_envelope(
Envelope(
headers={
"event_id": event_opt["event_id"],
"sent_at": format_timestamp(datetime.utcnow()),
},
items=[
Item(payload=PayloadRef(json=event_opt), type="transaction"),
],
)
)
else:
# All other events go to the /store/ endpoint.
self.transport.capture_event(event_opt)
return event_id

def capture_session(
Expand Down
12 changes: 10 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,20 @@ def inner():
events = []
test_client = sentry_sdk.Hub.current.client
old_capture_event = test_client.transport.capture_event
old_capture_envelope = test_client.transport.capture_envelope

def append(event):
def append_event(event):
events.append(event)
return old_capture_event(event)

monkeypatch.setattr(test_client.transport, "capture_event", append)
def append_envelope(envelope):
for item in envelope:
if item.headers.get("type") in ("event", "transaction"):
events.append(item.payload.json)
return old_capture_envelope(envelope)

monkeypatch.setattr(test_client.transport, "capture_event", append_event)
monkeypatch.setattr(test_client.transport, "capture_envelope", append_envelope)
return events

return inner
Expand Down
3 changes: 2 additions & 1 deletion tests/test_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ def test_basic(sentry_init, capture_events, sample_rate):
pass

if sample_rate:
(event,) = events
Copy link
Member

Choose a reason for hiding this comment

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

I really like this pattern! :P

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't dislike it. The problem was the kind of output we were getting when the test fails "not enough values to unpack" or "too many values to unpack", no clue what the events were.

The fix is adding an assert so that pytest handles printing the contents of events. Could have kept the unpacking, but now I don't want to trigger CI again.

Copy link
Member

Choose a reason for hiding this comment

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

that's also been a complaint from @mitsuhiko. I might make a pytest plugin that does something similar to assertion rewrites

assert len(events) == 1
event = events[0]

span1, span2 = event["spans"]
parent_span = event
Expand Down