Skip to content

Commit

Permalink
Add "replay" context to event payload (#2234)
Browse files Browse the repository at this point in the history
If we receive a replay_id in the incoming baggage header alsways add this replay_id in the replay context to the payload of events.
  • Loading branch information
antonpirker committed Jul 7, 2023
1 parent 0271219 commit d26fe80
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
10 changes: 10 additions & 0 deletions sentry_sdk/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,16 @@ def _drop(cause, ty):
else:
contexts["trace"] = self.get_trace_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,
}

exc_info = hint.get("exc_info")
if exc_info is not None:
for error_processor in self._error_processors:
Expand Down
31 changes: 31 additions & 0 deletions tests/integrations/flask/test_flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -875,3 +875,34 @@ def index():

assert event["request"]["data"]["password"] == "[Filtered]"
assert event["request"]["headers"]["Authorization"] == "[Filtered]"


@pytest.mark.parametrize("traces_sample_rate", [None, 1.0])
def test_replay_event_context(sentry_init, capture_events, app, traces_sample_rate):
"""
Tests that the replay context is added to the event context.
This is not strictly a Flask integration test, but it's the easiest way to test this.
"""
sentry_init(traces_sample_rate=traces_sample_rate)

@app.route("/error")
def error():
return 1 / 0

events = capture_events()

client = app.test_client()
headers = {
"baggage": "other-vendor-value-1=foo;bar;baz,sentry-trace_id=771a43a4192642f0b136d5159a501700,sentry-public_key=49d0f7386ad645858ae85020e393bef3, sentry-sample_rate=0.01337,sentry-user_id=Am%C3%A9lie,other-vendor-value-2=foo;bar,sentry-replay_id=12312012123120121231201212312012",
"sentry-trace": "771a43a4192642f0b136d5159a501700-1234567890abcdef-1",
}
with pytest.raises(ZeroDivisionError):
client.get("/error", headers=headers)

event = events[0]

assert event["contexts"]
assert event["contexts"]["replay"]
assert (
event["contexts"]["replay"]["replay_id"] == "12312012123120121231201212312012"
)

0 comments on commit d26fe80

Please sign in to comment.