sentry_debug_images/
integration.rs

1use std::borrow::Cow;
2
3use once_cell::sync::Lazy;
4use sentry_core::protocol::{DebugMeta, Event};
5use sentry_core::{ClientOptions, Integration};
6
7/// The Sentry Debug Images Integration.
8pub struct DebugImagesIntegration {
9    filter: Box<dyn Fn(&Event<'_>) -> bool + Send + Sync>,
10}
11
12impl DebugImagesIntegration {
13    /// Creates a new Debug Images Integration.
14    pub fn new() -> Self {
15        Self::default()
16    }
17
18    /// Sets a custom filter function.
19    ///
20    /// The filter specified which [`Event`]s should get debug images.
21    #[must_use]
22    pub fn filter<F>(mut self, filter: F) -> Self
23    where
24        F: Fn(&Event<'_>) -> bool + Send + Sync + 'static,
25    {
26        self.filter = Box::new(filter);
27        self
28    }
29}
30
31impl Default for DebugImagesIntegration {
32    fn default() -> Self {
33        Self {
34            filter: Box::new(|_| true),
35        }
36    }
37}
38
39impl std::fmt::Debug for DebugImagesIntegration {
40    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41        #[derive(Debug)]
42        struct Filter;
43        f.debug_struct("DebugImagesIntegration")
44            .field("filter", &Filter)
45            .finish()
46    }
47}
48
49impl Integration for DebugImagesIntegration {
50    fn name(&self) -> &'static str {
51        "debug-images"
52    }
53
54    fn process_event(
55        &self,
56        mut event: Event<'static>,
57        _opts: &ClientOptions,
58    ) -> Option<Event<'static>> {
59        static DEBUG_META: Lazy<DebugMeta> = Lazy::new(|| DebugMeta {
60            images: crate::debug_images(),
61            ..Default::default()
62        });
63
64        if event.debug_meta.is_empty() && (self.filter)(&event) {
65            event.debug_meta = Cow::Borrowed(&DEBUG_META);
66        }
67
68        Some(event)
69    }
70}