relay_event_normalization/
timestamp.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
use relay_event_schema::processor::{
    ProcessValue, ProcessingAction, ProcessingResult, ProcessingState, Processor,
};
use relay_event_schema::protocol::{Breadcrumb, Event, Span};
use relay_protocol::{Error, Meta};

/// Ensures an event's timestamps are not stale.
///
/// Stale timestamps are those that happened before January 1, 1970 UTC. The
/// processor validates the start and end timestamps of an event and returns an
/// error if any of these are stale. Additionally, spans and breadcrumbs with
/// stale timestamps are removed from the event.
///
/// The processor checks the timestamps individually and it's not responsible
/// for decisions that relate timestamps together, including but not limited to:
/// - Ensuring the start timestamp is not later than the end timestamp.
/// - The event finished in the last X days.
pub struct TimestampProcessor;

impl Processor for TimestampProcessor {
    fn process_event(
        &mut self,
        event: &mut Event,
        _: &mut Meta,
        state: &ProcessingState,
    ) -> ProcessingResult {
        if let Some(end_timestamp) = event.timestamp.value() {
            if end_timestamp.into_inner().timestamp_millis() < 0 {
                return Err(ProcessingAction::InvalidTransaction(
                    "timestamp is too stale",
                ));
            }
        }
        if let Some(start_timestamp) = event.start_timestamp.value() {
            if start_timestamp.into_inner().timestamp_millis() < 0 {
                return Err(ProcessingAction::InvalidTransaction(
                    "timestamp is too stale",
                ));
            }
        }

        event.process_child_values(self, state)?;

        Ok(())
    }

    fn process_span(
        &mut self,
        span: &mut Span,
        meta: &mut Meta,
        _: &ProcessingState<'_>,
    ) -> ProcessingResult {
        if let Some(start_timestamp) = span.start_timestamp.value() {
            if start_timestamp.into_inner().timestamp_millis() < 0 {
                meta.add_error(Error::invalid(format!(
                    "start_timestamp is too stale: {}",
                    start_timestamp
                )));
                return Err(ProcessingAction::DeleteValueHard);
            }
        }
        if let Some(end_timestamp) = span.timestamp.value() {
            if end_timestamp.into_inner().timestamp_millis() < 0 {
                meta.add_error(Error::invalid(format!(
                    "timestamp is too stale: {}",
                    end_timestamp
                )));
                return Err(ProcessingAction::DeleteValueHard);
            }
        }

        Ok(())
    }

    fn process_breadcrumb(
        &mut self,
        breadcrumb: &mut Breadcrumb,
        meta: &mut Meta,
        _: &ProcessingState<'_>,
    ) -> ProcessingResult where {
        if let Some(timestamp) = breadcrumb.timestamp.value() {
            if timestamp.into_inner().timestamp_millis() < 0 {
                meta.add_error(Error::invalid(format!(
                    "timestamp is too stale: {}",
                    timestamp
                )));
                return Err(ProcessingAction::DeleteValueHard);
            }
        }

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use relay_event_schema::processor::{process_value, ProcessingState};
    use relay_event_schema::protocol::{Breadcrumb, Event, Span};
    use relay_protocol::{assert_annotated_snapshot, get_value, Annotated};

    use crate::timestamp::TimestampProcessor;

    #[test]
    fn test_accept_recent_errors() {
        let json = r#"{
  "event_id": "52df9022835246eeb317dbd739ccd059",
  "timestamp": 1
}"#;
        let mut error = Annotated::<Event>::from_json(json).unwrap();
        assert!(
            process_value(&mut error, &mut TimestampProcessor, ProcessingState::root()).is_ok()
        );
        assert_eq!(get_value!(error.timestamp!).into_inner().timestamp(), 1);
    }

    #[test]
    fn test_reject_stale_errors() {
        let json = r#"{
  "event_id": "52df9022835246eeb317dbd739ccd059",
  "timestamp": -1
}"#;
        let mut error = Annotated::<Event>::from_json(json).unwrap();
        assert_eq!(
            process_value(&mut error, &mut TimestampProcessor, ProcessingState::root())
                .unwrap_err()
                .to_string(),
            "invalid transaction event: timestamp is too stale"
        );
    }

    #[test]
    fn test_accept_recent_transactions() {
        let json = r#"{
  "event_id": "52df9022835246eeb317dbd739ccd059",
  "start_timestamp": 1,
  "timestamp": 2
}"#;
        let mut transaction = Annotated::<Event>::from_json(json).unwrap();
        assert!(process_value(
            &mut transaction,
            &mut TimestampProcessor,
            ProcessingState::root()
        )
        .is_ok());
    }

    #[test]
    fn test_reject_stale_transactions() {
        let json = r#"{
  "event_id": "52df9022835246eeb317dbd739ccd059",
  "start_timestamp": -2,
  "timestamp": -1
}"#;
        let mut transaction = Annotated::<Event>::from_json(json).unwrap();
        assert_eq!(
            process_value(
                &mut transaction,
                &mut TimestampProcessor,
                ProcessingState::root()
            )
            .unwrap_err()
            .to_string(),
            "invalid transaction event: timestamp is too stale"
        );
    }

    #[test]
    fn test_reject_long_running_transactions() {
        let json = r#"{
  "event_id": "52df9022835246eeb317dbd739ccd059",
  "start_timestamp": -1,
  "timestamp": 1
}"#;
        let mut transaction = Annotated::<Event>::from_json(json).unwrap();
        assert_eq!(
            process_value(
                &mut transaction,
                &mut TimestampProcessor,
                ProcessingState::root()
            )
            .unwrap_err()
            .to_string(),
            "invalid transaction event: timestamp is too stale"
        );
    }

    #[test]
    fn test_accept_recent_span() {
        let json = r#"{
      "span_id": "52df9022835246eeb317dbd739ccd050",
      "start_timestamp": 1,
      "timestamp": 2
    }"#;
        let mut span = Annotated::<Span>::from_json(json).unwrap();
        assert!(process_value(&mut span, &mut TimestampProcessor, ProcessingState::root()).is_ok());
        assert_eq!(
            get_value!(span.start_timestamp!).into_inner().timestamp(),
            1
        );
        assert_eq!(get_value!(span.timestamp!).into_inner().timestamp(), 2);
    }

    #[test]
    fn test_reject_stale_span() {
        let json = r#"{
      "span_id": "52df9022835246eeb317dbd739ccd050",
      "start_timestamp": -2,
      "timestamp": -1
    }"#;
        let mut span = Annotated::<Span>::from_json(json).unwrap();
        assert!(process_value(&mut span, &mut TimestampProcessor, ProcessingState::root()).is_ok());
        assert_annotated_snapshot!(&span, @r###"
        {
          "_meta": {
            "": {
              "err": [
                [
                  "invalid_data",
                  {
                    "reason": "start_timestamp is too stale: 1969-12-31 23:59:58 UTC"
                  }
                ]
              ]
            }
          }
        }
        "###);
    }

    #[test]
    fn test_reject_long_running_span() {
        let json = r#"{
      "span_id": "52df9022835246eeb317dbd739ccd050",
      "start_timestamp": -1,
      "timestamp": 1
    }"#;
        let mut span = Annotated::<Span>::from_json(json).unwrap();
        assert!(process_value(&mut span, &mut TimestampProcessor, ProcessingState::root()).is_ok());
        assert_annotated_snapshot!(&span, @r###"
        {
          "_meta": {
            "": {
              "err": [
                [
                  "invalid_data",
                  {
                    "reason": "start_timestamp is too stale: 1969-12-31 23:59:59 UTC"
                  }
                ]
              ]
            }
          }
        }
        "###);
    }

    #[test]
    fn test_accept_recent_breadcrumb() {
        let json = r#"{
      "timestamp": 1
    }"#;
        let mut breadcrumb = Annotated::<Breadcrumb>::from_json(json).unwrap();
        assert!(process_value(
            &mut breadcrumb,
            &mut TimestampProcessor,
            ProcessingState::root()
        )
        .is_ok());
        assert_eq!(
            get_value!(breadcrumb.timestamp!).into_inner().timestamp(),
            1
        );
    }

    #[test]
    fn test_reject_stale_breadcrumb() {
        let json = r#"{
      "timestamp": -1
    }"#;
        let mut breadcrumb = Annotated::<Breadcrumb>::from_json(json).unwrap();
        assert!(process_value(
            &mut breadcrumb,
            &mut TimestampProcessor,
            ProcessingState::root()
        )
        .is_ok());
        assert_annotated_snapshot!(&breadcrumb, @r###"
        {
          "_meta": {
            "": {
              "err": [
                [
                  "invalid_data",
                  {
                    "reason": "timestamp is too stale: 1969-12-31 23:59:59 UTC"
                  }
                ]
              ]
            }
          }
        }
        "###);
    }
}