ckb_sentry_contexts/
integration.rs1use std::borrow::Cow;
2
3use sentry_core::protocol::map::Entry;
4use sentry_core::protocol::Event;
5use sentry_core::{ClientOptions, Integration};
6
7use crate::utils::{device_context, os_context, rust_context, server_name};
8
9#[derive(Debug)]
15pub struct ContextIntegration {
16 add_os: bool,
17 add_rust: bool,
18 add_device: bool,
19}
20
21impl Default for ContextIntegration {
22 fn default() -> Self {
23 Self {
24 add_os: true,
25 add_rust: true,
26 add_device: true,
27 }
28 }
29}
30
31impl ContextIntegration {
32 pub fn new() -> Self {
34 Self::default()
35 }
36
37 pub fn add_os(mut self, add_os: bool) -> Self {
39 self.add_os = add_os;
40 self
41 }
42 pub fn add_rust(mut self, add_rust: bool) -> Self {
44 self.add_rust = add_rust;
45 self
46 }
47
48 pub fn add_device(mut self, add_device: bool) -> Self {
50 self.add_device = add_device;
51 self
52 }
53}
54
55impl Integration for ContextIntegration {
56 fn name(&self) -> &'static str {
57 "contexts"
58 }
59
60 fn setup(&self, options: &mut ClientOptions) {
61 if options.server_name.is_none() {
62 options.server_name = server_name().map(Cow::Owned);
63 }
64 }
65
66 fn process_event(
67 &self,
68 mut event: Event<'static>,
69 _cfg: &ClientOptions,
70 ) -> Option<Event<'static>> {
71 if self.add_os {
72 if let Entry::Vacant(entry) = event.contexts.entry("os".to_string()) {
73 if let Some(os) = os_context() {
74 entry.insert(os);
75 }
76 }
77 }
78 if self.add_rust {
79 event
80 .contexts
81 .entry("rust".to_string())
82 .or_insert_with(rust_context);
83 }
84 if self.add_device {
85 event
86 .contexts
87 .entry("device".to_string())
88 .or_insert_with(device_context);
89 }
90
91 Some(event)
92 }
93}