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

use once_cell::sync::Lazy;
use sentry_core::protocol::{DebugMeta, Event};
use sentry_core::{ClientOptions, Integration};

/// The Sentry Debug Images Integration.
pub struct DebugImagesIntegration {
    filter: Box<dyn Fn(&Event<'_>) -> bool + Send + Sync>,
}

impl DebugImagesIntegration {
    /// Creates a new Debug Images Integration.
    pub fn new() -> Self {
        Self::default()
    }

    /// Sets a custom filter function.
    ///
    /// The filter specified which [`Event`]s should get debug images.
    #[must_use]
    pub fn filter<F>(mut self, filter: F) -> Self
    where
        F: Fn(&Event<'_>) -> bool + Send + Sync + 'static,
    {
        self.filter = Box::new(filter);
        self
    }
}

impl Default for DebugImagesIntegration {
    fn default() -> Self {
        Self {
            filter: Box::new(|_| true),
        }
    }
}

impl std::fmt::Debug for DebugImagesIntegration {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        #[derive(Debug)]
        struct Filter;
        f.debug_struct("DebugImagesIntegration")
            .field("filter", &Filter)
            .finish()
    }
}

impl Integration for DebugImagesIntegration {
    fn name(&self) -> &'static str {
        "debug-images"
    }

    fn process_event(
        &self,
        mut event: Event<'static>,
        _opts: &ClientOptions,
    ) -> Option<Event<'static>> {
        static DEBUG_META: Lazy<DebugMeta> = Lazy::new(|| DebugMeta {
            images: crate::debug_images(),
            ..Default::default()
        });

        if event.debug_meta.is_empty() && (self.filter)(&event) {
            event.debug_meta = Cow::Borrowed(&DEBUG_META);
        }

        Some(event)
    }
}