relay_event_schema/protocol/contexts/
browser.rs

1use relay_protocol::{Annotated, Empty, FromValue, IntoValue, Object, Value};
2
3use crate::processor::ProcessValue;
4
5/// Web browser information.
6#[derive(Clone, Debug, Default, PartialEq, Empty, FromValue, IntoValue, ProcessValue)]
7pub struct BrowserContext {
8    /// Computed field from `name` and `version`. Needed by the metrics extraction.
9    pub browser: Annotated<String>,
10
11    /// Display name of the browser application.
12    pub name: Annotated<String>,
13
14    /// Version string of the browser.
15    pub version: Annotated<String>,
16
17    /// Additional arbitrary fields for forwards compatibility.
18    #[metastructure(additional_properties, retain = true, pii = "maybe")]
19    pub other: Object<Value>,
20}
21
22impl super::DefaultContext for BrowserContext {
23    fn default_key() -> &'static str {
24        "browser"
25    }
26
27    fn from_context(context: super::Context) -> Option<Self> {
28        match context {
29            super::Context::Browser(c) => Some(*c),
30            _ => None,
31        }
32    }
33
34    fn cast(context: &super::Context) -> Option<&Self> {
35        match context {
36            super::Context::Browser(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::Browser(c) => Some(c),
44            _ => None,
45        }
46    }
47
48    fn into_context(self) -> super::Context {
49        super::Context::Browser(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_browser_context_roundtrip() {
60        let json = r#"{
61  "browser": "Google Chrome 67.0.3396.99",
62  "name": "Google Chrome",
63  "version": "67.0.3396.99",
64  "other": "value",
65  "type": "browser"
66}"#;
67        let context = Annotated::new(Context::Browser(Box::new(BrowserContext {
68            browser: Annotated::new(String::from("Google Chrome 67.0.3396.99")),
69            name: Annotated::new("Google Chrome".to_string()),
70            version: Annotated::new("67.0.3396.99".to_string()),
71            other: {
72                let mut map = Object::new();
73                map.insert(
74                    "other".to_string(),
75                    Annotated::new(Value::String("value".to_string())),
76                );
77                map
78            },
79        })));
80
81        assert_eq!(context, Annotated::from_json(json).unwrap());
82        assert_eq!(json, context.to_json_pretty().unwrap());
83    }
84}