From 9317df1b2dcd6677405aeec40da9dffeffb3a34c Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Wed, 12 Sep 2018 10:55:12 +0200 Subject: [PATCH] feat: Update to new sentry types (#82) --- Cargo.toml | 28 ++++++++++++++-------------- integrations/sentry-actix/src/lib.rs | 14 ++++++++++++-- src/backtrace_support.rs | 17 ++++++----------- src/client.rs | 25 +++++++++++++------------ src/constants.rs | 16 ++++++++++++++-- src/hub.rs | 2 +- src/integrations/error_chain.rs | 2 +- src/integrations/failure.rs | 24 +++++++++--------------- src/integrations/log.rs | 4 ++-- src/integrations/panic.rs | 4 ++-- src/scope/real.rs | 4 ++-- src/utils.rs | 14 +++++++++----- tests/test_basic.rs | 2 +- 13 files changed, 86 insertions(+), 70 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3ece1073..ef3bd35e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,33 +30,33 @@ with_debug_meta = ["findshlibs", "with_client_implementation"] with_test_support = [] [dependencies] -backtrace = { version = "0.3.8", optional = true } -url = { version = "1.7.0", optional = true } +backtrace = { version = "0.3.9", optional = true } +url = { version = "1.7.1", optional = true } failure = { version = "0.1.2", optional = true } -log = { version = "0.4.3", optional = true, features = ["std"] } -sentry-types = "0.5.3" -env_logger = { version = "0.5.10", optional = true } -reqwest = { version = "0.8.6", optional = true } +log = { version = "0.4.5", optional = true, features = ["std"] } +sentry-types = "0.7.1" +env_logger = { version = "0.5.13", optional = true } +reqwest = { version = "0.8.8", optional = true } uuid = { version = "0.6.5", features = ["v4"] } -lazy_static = "1.0.1" -regex = { version = "1.0.0", optional = true } +lazy_static = "1.1.0" +regex = { version = "1.0.5", optional = true } error-chain = { version = "0.12.0", optional = true } im = { version = "12.0.0", optional = true } -libc = { version = "0.2.42", optional = true } +libc = { version = "0.2.43", optional = true } hostname = { version = "0.1.5", optional = true } findshlibs = { version = "0.4.0", optional = true } -rand = "0.5.4" +rand = "0.5.5" [target."cfg(not(windows))".dependencies] uname = { version = "0.1.1", optional = true } [build-dependencies] -rustc_version = { version = "0.2.2", optional = true } +rustc_version = { version = "0.2.3", optional = true } [dev-dependencies] -failure_derive = "0.1.1" -pretty_env_logger = "0.2.3" -actix-web = { version = "0.6.12", default-features = false } +failure_derive = "0.1.2" +pretty_env_logger = "0.2.4" +actix-web = { version = "0.7.7", default-features = false } [[example]] name = "error-chain-demo" diff --git a/integrations/sentry-actix/src/lib.rs b/integrations/sentry-actix/src/lib.rs index c1a0707a..e8d0235e 100644 --- a/integrations/sentry-actix/src/lib.rs +++ b/integrations/sentry-actix/src/lib.rs @@ -84,8 +84,9 @@ use actix_web::middleware::{Middleware, Response, Started}; use actix_web::{Error, HttpMessage, HttpRequest, HttpResponse}; use failure::Fail; use sentry::integrations::failure::exception_from_single_fail; -use sentry::protocol::{Event, Level}; +use sentry::protocol::{ClientSdkPackageInfo, Event, Level}; use sentry::Hub; +use std::borrow::Cow; use std::sync::{Arc, Mutex}; use uuid::Uuid; @@ -228,6 +229,15 @@ impl Middleware for SentryMiddleware { } } + if let Some(sdk) = event.sdk.take() { + let mut sdk = sdk.into_owned(); + sdk.packages.push(ClientSdkPackageInfo { + package_name: "sentry-actix".into(), + version: env!("CARGO_PKG_VERSION").into(), + }); + event.sdk = Some(Cow::Owned(sdk)); + } + Some(event) })); }); @@ -303,7 +313,7 @@ impl ActixWebHubExt for Hub { } exceptions.reverse(); self.capture_event(Event { - exceptions: exceptions, + exception: exceptions.into(), level: Level::Error, ..Default::default() }) diff --git a/src/backtrace_support.rs b/src/backtrace_support.rs index 0c3b8a48..3a41bffd 100644 --- a/src/backtrace_support.rs +++ b/src/backtrace_support.rs @@ -3,7 +3,7 @@ use std::fmt; use backtrace::Backtrace; use regex::{Captures, Regex}; -use api::protocol::{FileLocation, Frame, InstructionInfo, Stacktrace}; +use api::protocol::{Frame, Stacktrace}; lazy_static!{ static ref HASH_FUNC_RE: Regex = Regex::new(r#"(?x) @@ -131,16 +131,11 @@ pub fn backtrace_to_stacktrace(bt: &Backtrace) -> Option { None }, function: Some(function), - instruction_info: InstructionInfo { - instruction_addr: Some(frame.ip().into()), - ..Default::default() - }, - location: FileLocation { - abs_path, - filename, - line: sym.lineno().map(|l| l.into()), - column: None, - }, + instruction_addr: Some(frame.ip().into()), + abs_path, + filename, + lineno: sym.lineno().map(|l| l.into()), + colno: None, ..Default::default() } }) diff --git a/src/client.rs b/src/client.rs index 5d83bca7..e399e9b5 100644 --- a/src/client.rs +++ b/src/client.rs @@ -395,8 +395,16 @@ impl Client { }; } - if event.id.is_none() { - event.id = Some(Uuid::new_v4()); + // id, debug meta and sdk are set before the processors run so that the + // processors can poke around in that data. + if event.event_id.is_nil() { + event.event_id = Uuid::new_v4(); + } + if event.debug_meta.is_empty() { + event.debug_meta = Cow::Borrowed(&DEBUG_META); + } + if event.sdk.is_none() { + event.sdk = Some(Cow::Borrowed(&SDK_INFO)); } if let Some(scope) = scope { @@ -423,19 +431,12 @@ impl Client { if event.server_name.is_none() { event.server_name = self.options.server_name.clone(); } - if event.sdk_info.is_none() { - event.sdk_info = Some(Cow::Borrowed(&SDK_INFO)); - } if &event.platform == "other" { event.platform = "native".into(); } - if event.debug_meta.is_empty() { - event.debug_meta = Cow::Borrowed(&DEBUG_META); - } - - for exc in &mut event.exceptions { + for exc in &mut event.exception { if let Some(ref mut stacktrace) = exc.stacktrace { // automatically trim backtraces if self.options.trim_backtraces { @@ -512,7 +513,7 @@ impl Client { if let Some(ref func) = self.options.before_send { sentry_debug!("invoking before_send callback"); - let id = event.id; + let id = event.event_id; func(event).or_else(move || { sentry_debug!("before_send dropped event {:?}", id); None @@ -542,7 +543,7 @@ impl Client { if let Some(ref transport) = *self.transport.read().unwrap() { if self.sample_should_send() { if let Some(event) = self.prepare_event(event, scope) { - let event_id = event.id.unwrap(); + let event_id = event.event_id; transport.send_event(event); return event_id; } diff --git a/src/constants.rs b/src/constants.rs index aed2e578..c243c893 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -1,4 +1,4 @@ -use api::protocol::ClientSdkInfo; +use api::protocol::{ClientSdkInfo, ClientSdkPackageInfo}; /// The version of the library pub const VERSION: &str = env!("CARGO_PKG_VERSION"); @@ -6,10 +6,14 @@ pub const VERSION: &str = env!("CARGO_PKG_VERSION"); include!(concat!(env!("OUT_DIR"), "/constants.gen.rs")); lazy_static! { - pub static ref USER_AGENT: String = format!("sentry-rust/{}", VERSION); + pub static ref USER_AGENT: String = format!("sentry.rust/{}", VERSION); pub static ref SDK_INFO: ClientSdkInfo = ClientSdkInfo { name: "sentry-rust".into(), version: VERSION.into(), + packages: vec![ClientSdkPackageInfo { + package_name: "cargo:sentry".into(), + version: VERSION.into(), + }], integrations: { #[allow(unused_mut)] let mut rv = vec![]; @@ -17,6 +21,14 @@ lazy_static! { { rv.push("failure".to_string()); } + #[cfg(feature = "with_panic")] + { + rv.push("panic".to_string()); + } + #[cfg(feature = "with_error_chain")] + { + rv.push("error_chain".to_string()); + } #[cfg(feature = "with_log")] { rv.push("log".to_string()); diff --git a/src/hub.rs b/src/hub.rs index 0047eb17..50ac2866 100644 --- a/src/hub.rs +++ b/src/hub.rs @@ -300,7 +300,7 @@ impl Hub { if client.options().attach_stacktrace { let thread = current_thread(true); if thread.stacktrace.is_some() { - event.threads.push(thread); + event.threads.values.push(thread); } } self.capture_event(event) diff --git a/src/integrations/error_chain.rs b/src/integrations/error_chain.rs index 09d5563d..0e44d82b 100644 --- a/src/integrations/error_chain.rs +++ b/src/integrations/error_chain.rs @@ -65,7 +65,7 @@ where T::ErrorKind: Debug + Display, { Event { - exceptions: exceptions_from_error_chain(e), + exception: exceptions_from_error_chain(e).into(), level: Level::Error, ..Default::default() } diff --git a/src/integrations/failure.rs b/src/integrations/failure.rs index 27656aa2..fcbc8355 100644 --- a/src/integrations/failure.rs +++ b/src/integrations/failure.rs @@ -32,7 +32,7 @@ use failure::{Error, Fail}; use regex::Regex; use uuid::Uuid; -use api::protocol::{Event, Exception, FileLocation, Frame, InstructionInfo, Level, Stacktrace}; +use api::protocol::{Event, Exception, Frame, Level, Stacktrace}; use backtrace_support::{demangle_symbol, error_typename, filename, strip_symbol}; use hub::Hub; @@ -71,18 +71,12 @@ fn parse_stacktrace(bt: &str) -> Option { None }, function: Some(function), - instruction_info: InstructionInfo { - instruction_addr: Some(captures["addr"].parse().unwrap()), - ..Default::default() - }, - location: FileLocation { - abs_path, - filename, - line: captures - .name("lineno") - .map(|x| x.as_str().parse::().unwrap()), - column: None, - }, + instruction_addr: Some(captures["addr"].parse().unwrap()), + abs_path, + filename, + lineno: captures + .name("lineno") + .map(|x| x.as_str().parse::().unwrap()), ..Default::default() } }) @@ -127,7 +121,7 @@ pub fn event_from_error(err: &failure::Error) -> Event<'static> { exceptions.reverse(); Event { - exceptions, + exception: exceptions.into(), level: Level::Error, ..Default::default() } @@ -145,7 +139,7 @@ pub fn event_from_fail(fail: &F) -> Event<'static> { exceptions.reverse(); Event { - exceptions, + exception: exceptions.into(), level: Level::Error, ..Default::default() } diff --git a/src/integrations/log.rs b/src/integrations/log.rs index 59df09c9..4a98da4f 100644 --- a/src/integrations/log.rs +++ b/src/integrations/log.rs @@ -153,7 +153,7 @@ pub fn event_from_record(record: &log::Record, with_stacktrace: bool) -> Event<' Event { logger: Some(record.target().into()), level: convert_log_level(record.level()), - exceptions: vec![Exception { + exception: vec![Exception { ty: record.target().into(), value: Some(format!("{}", record.args())), stacktrace: if with_stacktrace { @@ -162,7 +162,7 @@ pub fn event_from_record(record: &log::Record, with_stacktrace: bool) -> Event<' None }, ..Default::default() - }], + }].into(), ..Default::default() } } diff --git a/src/integrations/panic.rs b/src/integrations/panic.rs index a437716f..df80ad39 100644 --- a/src/integrations/panic.rs +++ b/src/integrations/panic.rs @@ -36,12 +36,12 @@ pub fn message_from_panic_info<'a>(info: &'a panic::PanicInfo) -> &'a str { pub fn event_from_panic_info(info: &panic::PanicInfo) -> Event<'static> { let msg = message_from_panic_info(info); Event { - exceptions: vec![Exception { + exception: vec![Exception { ty: "panic".into(), value: Some(msg.to_string()), stacktrace: current_stacktrace(), ..Default::default() - }], + }].into(), level: Level::Fatal, ..Default::default() } diff --git a/src/scope/real.rs b/src/scope/real.rs index 6a98852b..7f6d8dd5 100644 --- a/src/scope/real.rs +++ b/src/scope/real.rs @@ -299,11 +299,11 @@ impl Scope { } for processor in &self.event_processors { - let id = event.id; + let id = event.event_id; event = match processor(event) { Some(event) => event, None => { - sentry_debug!("event processor dropped event {:?}", id); + sentry_debug!("event processor dropped event {}", id); return None; } } diff --git a/src/utils.rs b/src/utils.rs index ea21cbbb..aaa08d0c 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -3,7 +3,7 @@ use std::mem; use std::thread; use api::protocol::{ - Context, DebugImage, DeviceContext, OsContext, RuntimeContext, Stacktrace, Thread, + Context, DebugImage, DeviceContext, Map, OsContext, RuntimeContext, Stacktrace, Thread, }; #[cfg(all(feature = "with_device_info", target_os = "macos"))] @@ -199,13 +199,17 @@ pub fn rust_context() -> Option { #[cfg(feature = "with_device_info")] { use constants::{RUSTC_CHANNEL, RUSTC_VERSION}; - let mut ctx: Context = RuntimeContext { + let ctx = RuntimeContext { name: Some("rustc".into()), version: RUSTC_VERSION.map(|x| x.into()), + other: { + let mut map = Map::default(); + if let Some(channel) = RUSTC_CHANNEL { + map.insert("channel".to_string(), channel.into()); + } + map + }, }.into(); - if let Some(channel) = RUSTC_CHANNEL { - ctx.extra.insert("channel".into(), channel.into()); - } Some(ctx) } #[cfg(not(feature = "with_device_info"))] diff --git a/tests/test_basic.rs b/tests/test_basic.rs index 6d376800..35d387b4 100644 --- a/tests/test_basic.rs +++ b/tests/test_basic.rs @@ -25,7 +25,7 @@ fn test_basic_capture_message() { vec![("worker".to_string(), "worker1".to_string())] ); - assert_eq!(event.id, last_event_id); + assert_eq!(Some(event.event_id), last_event_id); } #[test]