[go: up one dir, main page]

Documentation
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
extern crate time;

use std::thread;
use std::sync::mpsc::channel;
use std::sync::mpsc::Sender;
use std::sync::mpsc::Receiver;
use std::sync::{Arc, Mutex};
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
use std::fmt::Debug;
use std::time::Duration;
use std::io::Read;
use std::env;

#[macro_use]
extern crate hyper;
use hyper::Client;
use hyper::header::{Headers, ContentType};

extern crate chrono;
use chrono::offset::utc::UTC;

struct ThreadState<'a> {
    alive: &'a mut Arc<AtomicBool>,
}
impl<'a> ThreadState<'a> {
    fn set_alive(&self) {
        self.alive.store(true, Ordering::Relaxed);
    }
}
impl<'a> Drop for ThreadState<'a> {
    fn drop(&mut self) {
        self.alive.store(false, Ordering::Relaxed);
    }
}

pub trait WorkerClosure<T, P>: Fn(&P, T) -> () + Send + Sync {}
impl<T, F, P> WorkerClosure<T, P> for F where F: Fn(&P, T) -> () + Send + Sync {}

#[derive(Clone)]
pub struct SingleWorker<T: 'static + Send, P: Clone + Send> {
    parameters: P,
    f: Arc<Box<WorkerClosure<T, P, Output = ()>>>,
    receiver: Arc<Mutex<Receiver<T>>>,
    sender: Sender<T>,
    alive: Arc<AtomicBool>,
}

impl<T: 'static + Debug + Send, P: 'static + Clone + Send> SingleWorker<T, P> {
    pub fn new(parameters: P, f: Box<WorkerClosure<T, P, Output = ()>>) -> SingleWorker<T, P> {
        let (sender, receiver) = channel::<T>();

        let worker = SingleWorker {
            parameters: parameters,
            f: Arc::new(f),
            receiver: Arc::new(Mutex::new(receiver)),
            sender: sender,
            alive: Arc::new(AtomicBool::new(true)),
        };
        SingleWorker::spawn_thread(&worker);
        worker
    }

    fn is_alive(&self) -> bool {
        self.alive.clone().load(Ordering::Relaxed)
    }

    fn spawn_thread(worker: &SingleWorker<T, P>) {
        let mut alive = worker.alive.clone();
        let f = worker.f.clone();
        let receiver = worker.receiver.clone();
        let parameters = worker.parameters.clone();
        thread::spawn(move || {
            let state = ThreadState { alive: &mut alive };
            state.set_alive();

            let lock = match receiver.lock() {
                Ok(guard) => guard,
                Err(poisoned) => poisoned.into_inner(),
            };
            loop {
                match lock.recv() {
                    Ok(value) => f(&parameters, value),
                    Err(_) => {
                        thread::yield_now();
                    }
                };
            }

        });
        while !worker.is_alive() {
            thread::yield_now();
        }
    }

    pub fn work_with(&self, msg: T) {
        let alive = self.is_alive();
        if !alive {
            SingleWorker::spawn_thread(self);
        }
        let _ = self.sender.send(msg);
    }
}


// see https://docs.getsentry.com/hosted/clientdev/attributes/
#[derive(Debug,Clone)]
pub struct Event {
    // required
    event_id: String, // uuid4 exactly 32 characters (no dashes!)
    message: String, // Maximum length is 1000 characters.
    timestamp: String, // ISO 8601 format, without a timezone ex: "2011-05-02T17:41:36"
    level: String, // fatal, error, warning, info, debug
    logger: String, // ex "my.logger.name"
    platform: String, // Acceptable values ..., other
    sdk: SDK,
    device: Device,
    // optional
    culprit: Option<String>, // the primary perpetrator of this event ex: "my.module.function_name"
    server_name: Option<String>, // host client from which the event was recorded
    release: Option<String>, // generally be something along the lines of the git SHA for the given project
    tags: Vec<(String, String)>, // WARNING! should be serialized as json object k->v
    environment: Option<String>, // ex: "production"
    modules: Vec<(String, String)>, // WARNING! should be serialized as json object k->v
    extra: Vec<(String, String)>, // WARNING! should be serialized as json object k->v
    fingerprint: Vec<String>, // An array of strings used to dictate the deduplicating for this event.
}
impl Event {
    pub fn to_json_string(&self) -> String {
        let mut s = String::new();
        s.push_str("{");
        s.push_str(&format!("\"event_id\":\"{}\",", self.event_id));
        s.push_str(&format!("\"message\":\"{}\",", self.message));
        s.push_str(&format!("\"timestamp\":\"{}\",", self.timestamp));
        s.push_str(&format!("\"level\":\"{}\",", self.level));
        s.push_str(&format!("\"logger\":\"{}\",", self.logger));
        s.push_str(&format!("\"platform\":\"{}\",", self.platform));
        s.push_str(&format!("\"sdk\": {},", self.sdk.to_json_string()));
        s.push_str(&format!("\"device\": {}", self.device.to_json_string()));

        if let Some(ref culprit) = self.culprit {
            s.push_str(&format!(",\"culprit\":\"{}\"", culprit));
        }
        if let Some(ref server_name) = self.server_name {
            s.push_str(&format!(",\"server_name\":\"{}\"", server_name));
        }
        if let Some(ref release) = self.release {
            s.push_str(&format!(",\"release\":\"{}\"", release));
        }
        if self.tags.len() > 0 {
            s.push_str(",\"tags\":\"{");
            for tag in self.tags.iter() {
                s.push_str(&format!("\"{}\":\"{}\"", tag.0, tag.1));
            }
            s.push_str("}");
        }
        if let Some(ref environment) = self.environment {
            s.push_str(&format!(",\"environment\":\"{}\"", environment));
        }
        if self.modules.len() > 0 {
            s.push_str(",\"modules\":\"{");
            for module in self.modules.iter() {
                s.push_str(&format!("\"{}\":\"{}\"", module.0, module.1));
            }
            s.push_str("}");
        }
        if self.extra.len() > 0 {
            s.push_str(",\"extra\":\"{");
            for extra in self.extra.iter() {
                s.push_str(&format!("\"{}\":\"{}\"", extra.0, extra.1));
            }
            s.push_str("}");
        }
        if self.fingerprint.len() > 0 {
            s.push_str(",\"fingerprint\":\"[");
            for fingerprint in self.fingerprint.iter() {
                s.push_str(&format!("\"{}\"", fingerprint));
            }
            s.push_str("]");
        }

        s.push_str("}");
        s
    }

    pub fn new(logger: &str,
               level: &str,
               message: &str,
               culprit: Option<&str>,
               server_name: Option<&str>,
               release: Option<&str>,
               environment: Option<&str>)
               -> Event {


        Event {
            event_id: "".to_string(),
            message: message.to_owned(),
            timestamp: UTC::now().format("%Y-%m-%dT%H:%M:%S").to_string(), /* ISO 8601 format, without a timezone ex: "2011-05-02T17:41:36" */
            level: level.to_owned(),
            logger: logger.to_owned(),
            platform: "other".to_string(),
            sdk: SDK {
                name: "rust-sentry".to_string(),
                version: env!("CARGO_PKG_VERSION").to_string(),
            },
            device: Device {
                name: env::var_os("OSTYPE")
                    .and_then(|cs| cs.into_string().ok())
                    .unwrap_or("".to_string()),
                version: "".to_string(),
                build: "".to_string(),
            },
            culprit: culprit.map(|c| c.to_owned()),
            server_name: server_name.map(|c| c.to_owned()),
            release: release.map(|c| c.to_owned()),
            tags: vec![],
            environment: environment.map(|c| c.to_owned()),
            modules: vec![],
            extra: vec![],
            fingerprint: vec![],
        }
    }
}

#[derive(Debug,Clone)]
pub struct SDK {
    name: String,
    version: String,
}
impl SDK {
    pub fn to_json_string(&self) -> String {
        format!("{{\"name\":\"{}\",\"version\":\"{}\"}}",
                self.name,
                self.version)
    }
}
#[derive(Debug,Clone)]
pub struct Device {
    name: String,
    version: String,
    build: String,
}
impl Device {
    pub fn to_json_string(&self) -> String {
        format!("{{\"name\":\"{}\",\"version\":\"{}\",\"build\":\"{}\"}}",
                self.name,
                self.version,
                self.build)
    }
}


#[derive(Debug,Clone)]
pub struct SentryCrediential {
    pub key: String,
    pub secret: String,
    pub project_id: String,
}
pub struct Sentry {
    server_name: String,
    release: String,
    environment: String,
    worker: SingleWorker<Event, SentryCrediential>,
}

header! { (XSentryAuth, "X-Sentry-Auth") => [String] }

impl Sentry {
    pub fn new(server_name: String,
               release: String,
               environment: String,
               credential: SentryCrediential)
               -> Sentry {
        let worker = SingleWorker::new(credential,
                                       Box::new(move |credential, e| -> () {
                                           Sentry::post(credential, &e);
                                       }));
        Sentry {
            server_name: server_name,
            release: release,
            environment: environment,
            worker: worker,
        }
    }



    // POST /api/1/store/ HTTP/1.1
    // Content-Type: application/json
    //
    fn post(credential: &SentryCrediential, e: &Event) {
        let mut headers = Headers::new();

        // X-Sentry-Auth: Sentry sentry_version=7,
        // sentry_client=<client version, arbitrary>,
        // sentry_timestamp=<current timestamp>,
        // sentry_key=<public api key>,
        // sentry_secret=<secret api key>
        //
        let timestamp = time::get_time().sec.to_string();
        let xsentryauth = format!("Sentry sentry_version=7,sentry_client=rust-sentry/0.1.0,\
                                   sentry_timestamp={},sentry_key={},sentry_secret={}",
                                  timestamp,
                                  credential.key,
                                  credential.secret);
        headers.set(XSentryAuth(xsentryauth));


        headers.set(ContentType::json());

        let body = e.to_json_string();
        println!("Sentry body {}", body);

        let mut client = Client::new();
        client.set_read_timeout(Some(Duration::new(5, 0)));
        client.set_write_timeout(Some(Duration::new(5, 0)));

        // {PROTOCOL}://{PUBLIC_KEY}:{SECRET_KEY}@{HOST}/{PATH}{PROJECT_ID}/store/
        let url = format!("https://{}:{}@{}/api/{}/store/",
                          credential.key,
                          credential.secret,
                          "app.getsentry.com",
                          credential.project_id);

        let mut res = client.post(&url)
            .headers(headers)
            .body(&body)
            .send()
            .unwrap();

        // Read the Response.
        let mut body = String::new();
        res.read_to_string(&mut body).unwrap();
        println!("Sentry Response {}", body);
    }



    // fatal, error, warning, info, debug
    pub fn fatal(&self, logger: &str, message: &str, culprit: Option<&str>) {
        self.log(logger, "fatal", message, culprit);
    }
    pub fn error(&self, logger: &str, message: &str, culprit: Option<&str>) {
        self.log(logger, "error", message, culprit);
    }
    pub fn warning(&self, logger: &str, message: &str, culprit: Option<&str>) {
        self.log(logger, "warning", message, culprit);
    }
    pub fn info(&self, logger: &str, message: &str, culprit: Option<&str>) {
        self.log(logger, "info", message, culprit);
    }
    pub fn debug(&self, logger: &str, message: &str, culprit: Option<&str>) {
        self.log(logger, "debug", message, culprit);
    }

    fn log(&self, logger: &str, level: &str, message: &str, culprit: Option<&str>) {
        self.worker.work_with(Event::new(logger,
                                         level,
                                         message,
                                         culprit,
                                         Some(&self.server_name),
                                         Some(&self.release),
                                         Some(&self.environment)));
    }
}

#[cfg(test)]
mod tests {
    use super::SingleWorker;
    use super::Sentry;
    use super::SentryCrediential;

    use std::sync::Mutex;
    use std::sync::mpsc::channel;
    use std::sync::atomic::{AtomicUsize, Ordering};
    use std::thread;

    use std::time::Duration;

    #[test]
    fn it_should_pass_value_to_worker_thread() {

        let (sender, receiver) = channel();
        let s = Mutex::new(sender);
        let worker = SingleWorker::new("",
                                       Box::new(move |_, v| {
                                           let _ = s.lock().unwrap().send(v);
                                       }));
        let v = "Value";
        worker.work_with(v);

        let recv_v = receiver.recv().ok();
        assert!(recv_v == Some(v));
    }

    #[test]
    fn it_should_pass_value_event_after_thread_panic() {
        let (sender, receiver) = channel();
        let s = Mutex::new(sender);
        let i = AtomicUsize::new(0);
        let worker = SingleWorker::new("",
                                       Box::new(move |_, v| {
            let lock = match s.lock() {
                Ok(guard) => guard,
                Err(poisoned) => poisoned.into_inner(),
            };
            let _ = lock.send(v);

            i.fetch_add(1, Ordering::SeqCst);
            if i.load(Ordering::Relaxed) == 2 {
                panic!("PanicTesting");
            }

        }));
        let v0 = "Value0";
        let v1 = "Value1";
        let v2 = "Value2";
        let v3 = "Value3";
        worker.work_with(v0);
        worker.work_with(v1);
        let recv_v0 = receiver.recv().ok();
        let recv_v1 = receiver.recv().ok();

        while worker.is_alive() {
            thread::yield_now();
        }

        worker.work_with(v2);
        worker.work_with(v3);
        let recv_v2 = receiver.recv().ok();
        let recv_v3 = receiver.recv().ok();

        assert!(recv_v0 == Some(v0));
        assert!(recv_v1 == Some(v1));
        assert!(recv_v2 == Some(v2));
        assert!(recv_v3 == Some(v3));

    }

    // #[test]
    // fn it_post_sentry_event() {
    //     let sentry = Sentry::new("Server Name".to_string(),
    //                              "release".to_string(),
    //                              "test_env".to_string(),
    //                              SentryCrediential {
    //                                  key: "xx".to_string(),
    //                                  secret: "xx".to_string(),
    //                                  project_id: "xx".to_string(),
    //                              });
    //
    //     sentry.info("test.logger", "Test Message", None);
    //
    //     thread::sleep(Duration::new(5, 0));
    //
    // }
}