relay_event_schema/protocol/
client_report.rs

1use relay_base_schema::data_category::DataCategory;
2use relay_common::time::UnixTimestamp;
3use serde::{Deserialize, Serialize};
4
5#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
6pub struct DiscardedEvent {
7    pub reason: String,
8    pub category: DataCategory,
9    pub quantity: u32,
10}
11
12#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
13pub struct ClientReport {
14    /// The timestamp of when the report was created.
15    pub timestamp: Option<UnixTimestamp>,
16    /// Counters for events discarded by the client.
17    pub discarded_events: Vec<DiscardedEvent>,
18    /// Counters for events rate limited by a relay configured to emit outcomes as client reports
19    #[serde(default, skip_serializing_if = "Vec::is_empty")]
20    pub rate_limited_events: Vec<DiscardedEvent>,
21    /// Counters for events filtered by a relay configured to emit outcomes as client reports
22    #[serde(default, skip_serializing_if = "Vec::is_empty")]
23    pub filtered_events: Vec<DiscardedEvent>,
24    /// Counters for events filtered by a sampling rule,
25    /// by a relay configured to emit outcomes as client reports
26    #[serde(default, skip_serializing_if = "Vec::is_empty")]
27    pub filtered_sampling_events: Vec<DiscardedEvent>,
28}
29
30impl ClientReport {
31    /// Parses a client report update from JSON.
32    pub fn parse(payload: &[u8]) -> Result<Self, serde_json::Error> {
33        serde_json::from_slice(payload)
34    }
35
36    /// Serializes a client report update back into JSON.
37    pub fn serialize(&self) -> Result<Vec<u8>, serde_json::Error> {
38        serde_json::to_vec(self)
39    }
40}
41
42#[cfg(test)]
43mod tests {
44    use similar_asserts::assert_eq;
45
46    use super::*;
47
48    #[test]
49    fn test_client_report_roundtrip() {
50        let json = r#"{
51  "timestamp": "2020-02-07T15:17:00Z",
52  "discarded_events": [
53    {"reason": "foo_reason", "category": "error", "quantity": 42},
54    {"reason": "foo_reason", "category": "transaction", "quantity": 23}
55  ],
56  "rate_limited_events" : [
57      {"reason": "bar_reason", "category": "session", "quantity": 456}
58  ]
59}"#;
60
61        let output = r#"{
62  "timestamp": 1581088620,
63  "discarded_events": [
64    {
65      "reason": "foo_reason",
66      "category": "error",
67      "quantity": 42
68    },
69    {
70      "reason": "foo_reason",
71      "category": "transaction",
72      "quantity": 23
73    }
74  ],
75  "rate_limited_events": [
76    {
77      "reason": "bar_reason",
78      "category": "session",
79      "quantity": 456
80    }
81  ]
82}"#;
83
84        let update = ClientReport {
85            timestamp: Some("2020-02-07T15:17:00Z".parse().unwrap()),
86            discarded_events: vec![
87                DiscardedEvent {
88                    reason: "foo_reason".into(),
89                    category: DataCategory::Error,
90                    quantity: 42,
91                },
92                DiscardedEvent {
93                    reason: "foo_reason".into(),
94                    category: DataCategory::Transaction,
95                    quantity: 23,
96                },
97            ],
98            rate_limited_events: vec![DiscardedEvent {
99                reason: "bar_reason".into(),
100                category: DataCategory::Session,
101                quantity: 456,
102            }],
103
104            ..Default::default()
105        };
106
107        let parsed = ClientReport::parse(json.as_bytes()).unwrap();
108        assert_eq!(update, parsed);
109        assert_eq!(output, serde_json::to_string_pretty(&update).unwrap());
110    }
111}