Skip to content

Commit d26fe80

Browse files
authored
Add "replay" context to event payload (#2234)
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.
1 parent 0271219 commit d26fe80

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

sentry_sdk/scope.py

+10
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,16 @@ def _drop(cause, ty):
614614
else:
615615
contexts["trace"] = self.get_trace_context()
616616

617+
try:
618+
replay_id = contexts["trace"]["dynamic_sampling_context"]["replay_id"]
619+
except (KeyError, TypeError):
620+
replay_id = None
621+
622+
if replay_id is not None:
623+
contexts["replay"] = {
624+
"replay_id": replay_id,
625+
}
626+
617627
exc_info = hint.get("exc_info")
618628
if exc_info is not None:
619629
for error_processor in self._error_processors:

tests/integrations/flask/test_flask.py

+31
Original file line numberDiff line numberDiff line change
@@ -875,3 +875,34 @@ def index():
875875

876876
assert event["request"]["data"]["password"] == "[Filtered]"
877877
assert event["request"]["headers"]["Authorization"] == "[Filtered]"
878+
879+
880+
@pytest.mark.parametrize("traces_sample_rate", [None, 1.0])
881+
def test_replay_event_context(sentry_init, capture_events, app, traces_sample_rate):
882+
"""
883+
Tests that the replay context is added to the event context.
884+
This is not strictly a Flask integration test, but it's the easiest way to test this.
885+
"""
886+
sentry_init(traces_sample_rate=traces_sample_rate)
887+
888+
@app.route("/error")
889+
def error():
890+
return 1 / 0
891+
892+
events = capture_events()
893+
894+
client = app.test_client()
895+
headers = {
896+
"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",
897+
"sentry-trace": "771a43a4192642f0b136d5159a501700-1234567890abcdef-1",
898+
}
899+
with pytest.raises(ZeroDivisionError):
900+
client.get("/error", headers=headers)
901+
902+
event = events[0]
903+
904+
assert event["contexts"]
905+
assert event["contexts"]["replay"]
906+
assert (
907+
event["contexts"]["replay"]["replay_id"] == "12312012123120121231201212312012"
908+
)

0 commit comments

Comments
 (0)