Skip to content

Commit

Permalink
feat: Add sampling and pii flag (seanmonstar#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko authored Aug 1, 2018
1 parent 6295554 commit b9e048a
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 8 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ im = { version = "10.2.0", optional = true }
libc = { version = "0.2.42", optional = true }
hostname = { version = "0.1.5", optional = true }
findshlibs = { version = "0.4.0", optional = true }
rand = "0.5.4"

[target."cfg(not(windows))".dependencies]
uname = { version = "0.1.1", optional = true }
Expand Down
12 changes: 10 additions & 2 deletions integrations/sentry-actix/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ impl<S: 'static> Middleware<S> for SentryMiddleware {
let hub = self.new_hub();
let outer_req = req;
let req = outer_req.clone();
let client = hub.client();
hub.add_event_processor(move || {
let resource = req.resource();
let transaction = if let Some(rdef) = resource.rdef() {
Expand All @@ -174,7 +175,7 @@ impl<S: 'static> Middleware<S> for SentryMiddleware {
None
}
};
let req = sentry::protocol::Request {
let mut sentry_req = sentry::protocol::Request {
url: format!(
"{}://{}{}",
req.connection_info().scheme(),
Expand All @@ -189,11 +190,18 @@ impl<S: 'static> Middleware<S> for SentryMiddleware {
.collect(),
..Default::default()
};
if client.map_or(false, |x| x.options().send_default_pii) {
if let Some(remote) = req.connection_info().remote() {
sentry_req.env.insert("REMOTE_ADDR".into(), remote.into());
}
};
Box::new(move |event| {
if event.transaction.is_none() {
event.transaction = transaction.clone();
}
event.request = Some(req.clone());
if event.request.is_none() {
event.request = Some(sentry_req.clone());
}
})
});
outer_req.extensions_mut().insert(hub);
Expand Down
40 changes: 34 additions & 6 deletions src/client.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use std::borrow::Cow;
use std::collections::HashMap;
use std::env;
use std::ffi::{OsStr, OsString};
use std::fmt;
use std::sync::Arc;
use std::time::Duration;

use rand::random;
use regex::Regex;
use uuid::Uuid;

use api::protocol::{DebugMeta, Event};
use api::protocol::{DebugMeta, Event, RepoReference};
use api::Dsn;
use backtrace_support::{function_starts_with, is_sys_function, trim_stacktrace};
use constants::{SDK_INFO, USER_AGENT};
Expand Down Expand Up @@ -49,10 +51,14 @@ pub struct ClientOptions {
pub trim_backtraces: bool,
/// The release to be sent with events.
pub release: Option<Cow<'static, str>>,
/// The repos to send along with the events.
pub repos: HashMap<String, RepoReference>,
/// The environment to be sent with events.
pub environment: Option<Cow<'static, str>>,
/// The server name to be reported.
pub server_name: Option<Cow<'static, str>>,
/// The sample rate for event submission (0.0 - 1.0, defaults to 1.0)
pub sample_rate: f32,
/// The user agent that should be reported.
pub user_agent: Cow<'static, str>,
/// An optional HTTP proxy to use.
Expand All @@ -68,6 +74,8 @@ pub struct ClientOptions {
pub shutdown_timeout: Option<Duration>,
/// Attaches stacktraces to messages.
pub attach_stacktrace: bool,
/// If turned on some default PII informat is attached.
pub send_default_pii: bool,
}

impl Default for ClientOptions {
Expand All @@ -79,12 +87,14 @@ impl Default for ClientOptions {
max_breadcrumbs: 100,
trim_backtraces: true,
release: None,
repos: Default::default(),
environment: Some(if cfg!(debug_assertions) {
"debug".into()
} else {
"release".into()
}),
server_name: server_name().map(Cow::Owned),
sample_rate: 1.0,
user_agent: Cow::Borrowed(&USER_AGENT),
http_proxy: env::var("http_proxy").ok().map(Cow::Owned),
https_proxy: env::var("https_proxy")
Expand All @@ -94,6 +104,7 @@ impl Default for ClientOptions {
.or_else(|| env::var("http_proxy").ok().map(Cow::Owned)),
shutdown_timeout: Some(Duration::from_secs(2)),
attach_stacktrace: false,
send_default_pii: false,
}
}
}
Expand Down Expand Up @@ -322,6 +333,13 @@ impl Client {
if event.release.is_none() {
event.release = self.options.release.clone();
}
if event.repos.is_empty() && !self.options.repos.is_empty() {
event.repos = self.options
.repos
.iter()
.map(|(k, v)| (k.to_string(), v.clone()))
.collect();
}
if event.environment.is_none() {
event.environment = self.options.environment.clone();
}
Expand Down Expand Up @@ -433,12 +451,13 @@ impl Client {
/// Captures an event and sends it to sentry.
pub fn capture_event(&self, mut event: Event<'static>, scope: Option<&Scope>) -> Uuid {
if let Some(ref transport) = self.transport {
let event_id = self.prepare_event(&mut event, scope);
transport.send_event(event);
event_id
} else {
Default::default()
if self.sample_should_send() {
let event_id = self.prepare_event(&mut event, scope);
transport.send_event(event);
return event_id;
}
}
Default::default()
}

/// Drains all pending events up to the current time.
Expand All @@ -460,6 +479,15 @@ impl Client {
.as_ref()
.expect("Client has no associated transport")
}

fn sample_should_send(&self) -> bool {
let rate = self.options.sample_rate;
if rate >= 1.0 {
true
} else {
random::<f32>() <= rate
}
}
}

/// Helper struct that is returned from `init`.
Expand Down
1 change: 1 addition & 0 deletions src/hub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use std::sync::atomic::{AtomicBool, Ordering};
use client::Client;
use protocol::{Breadcrumb, Event, Level};
use scope::{Scope, ScopeGuard};

#[cfg(feature = "with_client_implementation")]
use utils::current_thread;

Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ extern crate env_logger;
#[cfg(feature = "with_debug_meta")]
extern crate findshlibs;

extern crate rand;

#[macro_use]
mod macros;

Expand Down

0 comments on commit b9e048a

Please sign in to comment.