relay_event_schema/protocol/contexts/
replay.rs

1use relay_protocol::{Annotated, Empty, FromValue, IntoValue, Object, Value};
2
3use crate::processor::ProcessValue;
4use crate::protocol::EventId;
5
6/// Replay context.
7///
8/// The replay context contains the replay_id of the session replay if the event
9/// occurred during a replay. The replay_id is added onto the dynamic sampling context
10/// on the javascript SDK which propagates it through the trace. In relay, we take
11/// this value from the DSC and create a context which contains only the replay_id
12/// This context is never set on the client for events, only on relay.
13#[derive(Clone, Debug, Default, PartialEq, Empty, FromValue, IntoValue, ProcessValue)]
14pub struct ReplayContext {
15    /// The replay ID.
16    pub replay_id: Annotated<EventId>,
17    /// Additional arbitrary fields for forwards compatibility.
18    #[metastructure(additional_properties, retain = true)]
19    pub other: Object<Value>,
20}
21
22impl super::DefaultContext for ReplayContext {
23    fn default_key() -> &'static str {
24        "replay"
25    }
26
27    fn from_context(context: super::Context) -> Option<Self> {
28        match context {
29            super::Context::Replay(c) => Some(*c),
30            _ => None,
31        }
32    }
33
34    fn cast(context: &super::Context) -> Option<&Self> {
35        match context {
36            super::Context::Replay(c) => Some(c),
37            _ => None,
38        }
39    }
40
41    fn cast_mut(context: &mut super::Context) -> Option<&mut Self> {
42        match context {
43            super::Context::Replay(c) => Some(c),
44            _ => None,
45        }
46    }
47
48    fn into_context(self) -> super::Context {
49        super::Context::Replay(Box::new(self))
50    }
51}
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56    use crate::protocol::Context;
57
58    #[test]
59    fn test_replay_context() {
60        let json = r#"{
61  "replay_id": "4c79f60c11214eb38604f4ae0781bfb2",
62  "type": "replay"
63}"#;
64        let context = Annotated::new(Context::Replay(Box::new(ReplayContext {
65            replay_id: Annotated::new(EventId("4c79f60c11214eb38604f4ae0781bfb2".parse().unwrap())),
66            other: Object::default(),
67        })));
68
69        assert_eq!(context, Annotated::from_json(json).unwrap());
70        assert_eq!(json, context.to_json_pretty().unwrap());
71    }
72
73    #[test]
74    fn test_replay_context_normalization() {
75        let json = r#"{
76  "replay_id": "4C79F60C11214EB38604F4AE0781BFB2",
77  "type": "replay"
78}"#;
79        let context = Annotated::new(Context::Replay(Box::new(ReplayContext {
80            replay_id: Annotated::new(EventId("4c79f60c11214eb38604f4ae0781bfb2".parse().unwrap())),
81            other: Object::default(),
82        })));
83
84        assert_eq!(context, Annotated::from_json(json).unwrap());
85    }
86}