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
use std::borrow::Cow;

use sentry_core::protocol::map::Entry;
use sentry_core::protocol::Event;
use sentry_core::{ClientOptions, Integration};

use crate::utils::{device_context, os_context, rust_context, server_name};

/// Adds Contexts to Sentry Events.
///
/// See the [Contexts Interface] documentation for more info.
///
/// [Contexts Interface]: https://develop.sentry.dev/sdk/event-payloads/contexts/
#[derive(Debug)]
pub struct ContextIntegration {
    add_os: bool,
    add_rust: bool,
    add_device: bool,
}

impl Default for ContextIntegration {
    fn default() -> Self {
        Self {
            add_os: true,
            add_rust: true,
            add_device: true,
        }
    }
}

impl ContextIntegration {
    /// Create a new Context Integration.
    pub fn new() -> Self {
        Self::default()
    }

    /// Add `os` context, enabled by default.
    pub fn add_os(mut self, add_os: bool) -> Self {
        self.add_os = add_os;
        self
    }
    /// Add `rust` context, enabled by default.
    pub fn add_rust(mut self, add_rust: bool) -> Self {
        self.add_rust = add_rust;
        self
    }

    /// Add `device` context, enabled by default.
    pub fn add_device(mut self, add_device: bool) -> Self {
        self.add_device = add_device;
        self
    }
}

impl Integration for ContextIntegration {
    fn name(&self) -> &'static str {
        "contexts"
    }

    fn setup(&self, options: &mut ClientOptions) {
        if options.server_name.is_none() {
            options.server_name = server_name().map(Cow::Owned);
        }
    }

    fn process_event(
        &self,
        mut event: Event<'static>,
        _cfg: &ClientOptions,
    ) -> Option<Event<'static>> {
        if self.add_os {
            if let Entry::Vacant(entry) = event.contexts.entry("os".to_string()) {
                if let Some(os) = os_context() {
                    entry.insert(os);
                }
            }
        }
        if self.add_rust {
            event
                .contexts
                .entry("rust".to_string())
                .or_insert_with(rust_context);
        }
        if self.add_device {
            event
                .contexts
                .entry("device".to_string())
                .or_insert_with(device_context);
        }

        Some(event)
    }
}