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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
use std::borrow::Cow;
use std::{cell::RefCell, rc::Rc};

mod client;
mod consts;
pub(crate) mod converter;
mod crypto;
pub mod error;
pub mod layer;
mod net;
mod protocol;
mod request_context;
mod utils;

pub use self::client::Sentry;
pub use self::layer::Layer;
pub use self::protocol::{Breadcrumb, Level, Map, Request, SpanStatus as Status, User, Value};
pub use self::request_context::RequestContext;

thread_local!(pub(crate) static SENTRY: RefCell<Option<Sentry>> = RefCell::new(None));

pub struct Options {
    pub project: String,
    pub token: String,
}

pub fn init(ctx: worker::Context, options: impl Into<Option<Options>>) -> SentryToken {
    if let Some(options) = options.into() {
        let sentry = Sentry::new(ctx, options);
        SENTRY.with(move |cell| cell.borrow_mut().replace(sentry));
        SentryToken(true)
    } else {
        SentryToken(false)
    }
}

pub struct SentryToken(bool);

impl SentryToken {
    pub fn initialized(&self) -> bool {
        self.0
    }
}

impl Drop for SentryToken {
    fn drop(&mut self) {
        if self.initialized() {
            SENTRY.with(|cell| {
                let sentry = cell.borrow_mut().take();
                if let Some(mut sentry) = sentry {
                    sentry.finish_transaction();
                }
            })
        }
    }
}

pub struct TransactionContext {
    pub name: String,
    pub op: String,
}

pub fn start_transaction(ctx: TransactionContext) {
    with_sentry_mut(|sentry| sentry.start_transaction(ctx));
}

pub fn update_transaction(status: Status) {
    with_sentry_mut(|sentry| sentry.update_transaction(status));
}

pub fn add_breadcrumb(breadcrumb: Breadcrumb) {
    with_sentry_mut(move |sentry| {
        sentry.add_breadcrumb(breadcrumb);
    });
}

pub fn add_attachment_plain(data: Rc<[u8]>, filename: impl Into<Cow<'static, str>>) {
    with_sentry_mut(move |sentry| {
        sentry.add_attachment(protocol::Attachment {
            buffer: data,
            filename: filename.into(),
            content_type: Some("text/plain".into()),
            ty: Some(protocol::AttachmentType::Attachment),
        })
    });
}

pub fn set_user(user: User) {
    with_sentry_mut(|sentry| sentry.set_user(user));
}

pub fn update_username(name: impl Into<String>) {
    with_sentry_mut(|sentry| {
        if let Some(user) = sentry.user_mut() {
            user.username = Some(name.into());
        }
    });
}

pub fn set_request(request: Request) {
    with_sentry_mut(|sentry| sentry.set_request(request));
}

pub fn with_sentry<F, T>(f: F) -> Option<T>
where
    F: FnOnce(&Sentry) -> T,
{
    SENTRY.with(|sentry| sentry.borrow().as_ref().map(f))
}

pub(crate) fn with_sentry_mut<F, T>(f: F) -> Option<T>
where
    F: FnOnce(&mut Sentry) -> T,
{
    SENTRY.with(|sentry| sentry.borrow_mut().as_mut().map(f))
}