Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ jobs:
codecov:
name: Code Coverage
runs-on: ubuntu-latest
env:
# Keep in sync with test.yml: anyhow backtrace tests require this preset.
RUST_BACKTRACE: "1"

steps:
- uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ on:

env:
RUSTFLAGS: -Dwarnings
# anyhow backtrace capture (and some SDK tests) require this to already be set;
# mutating the process environment from tests via std::env::set_var is unsound.
RUST_BACKTRACE: "1"

jobs:
test:
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
resolver = "2"
resolver = "3"
members = [
"sentry",
"sentry-actix",
Expand All @@ -21,7 +21,7 @@ members = [
authors = ["Sentry <hello@sentry.io>"]
repository = "https://github.com/getsentry/sentry-rust"
homepage = "https://sentry.io/welcome/"
edition = "2021"
edition = "2024"
rust-version = "1.88"

[workspace.lints.clippy]
Expand Down
1 change: 0 additions & 1 deletion sentry-actix/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ fn main() -> io::Result<()> {
let _guard = sentry::init(
sentry::ClientOptions::new().maybe_release(sentry::release_name!()),
);
std::env::set_var("RUST_BACKTRACE", "1");

let runtime = tokio::runtime::Builder::new_multi_thread()
.enable_all()
Expand Down
4 changes: 1 addition & 3 deletions sentry-actix/examples/basic.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::env;
use std::io;

use actix_web::{get, App, Error, HttpRequest, HttpServer};
use actix_web::{App, Error, HttpRequest, HttpServer, get};
use sentry::Level;

#[get("/")]
Expand Down Expand Up @@ -30,7 +29,6 @@ fn main() -> io::Result<()> {
.session_mode(sentry::SessionMode::Request)
.debug(true),
);
env::set_var("RUST_BACKTRACE", "1");

let runtime = tokio::runtime::Builder::new_multi_thread()
.enable_all()
Expand Down
38 changes: 18 additions & 20 deletions sentry-actix/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
//! let _guard = sentry::init(
//! sentry::ClientOptions::new().maybe_release(sentry::release_name!()),
//! );
//! std::env::set_var("RUST_BACKTRACE", "1");
//!
//! let runtime = tokio::runtime::Builder::new_multi_thread()
//! .enable_all()
Expand Down Expand Up @@ -79,16 +78,16 @@ use std::rc::Rc;
use std::sync::Arc;

use actix_http::header::{self, HeaderMap};
use actix_web::Error;
use actix_web::dev::{Service, ServiceRequest, ServiceResponse, Transform};
use actix_web::http::StatusCode;
use actix_web::Error;
use bytes::{Bytes, BytesMut};
use futures_util::future::{ok, Future, Ready};
use futures_util::future::{Future, Ready, ok};
use futures_util::{FutureExt as _, TryStreamExt as _};

use sentry_core::MaxRequestBodySize;
use sentry_core::protocol::{self, ClientSdkPackage, Event, Request};
use sentry_core::utils::{is_sensitive_header, scrub_pii_from_url};
use sentry_core::MaxRequestBodySize;
use sentry_core::{Hub, SentryFutureExt};

/// A helper construct that can be used to reconfigure and build the middleware.
Expand Down Expand Up @@ -369,16 +368,17 @@ where
};

// Response errors
if inner.capture_server_errors && res.response().status().is_server_error() {
if let Some(e) = res.response().error() {
let event_id = hub.capture_error(e);

if inner.emit_header {
res.response_mut().headers_mut().insert(
"x-sentry-event".parse().unwrap(),
event_id.simple().to_string().parse().unwrap(),
);
}
if inner.capture_server_errors
&& res.response().status().is_server_error()
&& let Some(e) = res.response().error()
{
let event_id = hub.capture_error(e);

if inner.emit_header {
res.response_mut().headers_mut().insert(
"x-sentry-event".parse().unwrap(),
event_id.simple().to_string().parse().unwrap(),
);
}
}

Expand Down Expand Up @@ -443,10 +443,8 @@ fn sentry_request_from_http(request: &ServiceRequest, with_pii: bool) -> Request
};

// If PII is enabled, include the remote address
if with_pii {
if let Some(remote) = request.connection_info().remote_addr() {
sentry_req.env.insert("REMOTE_ADDR".into(), remote.into());
}
if with_pii && let Some(remote) = request.connection_info().remote_addr() {
sentry_req.env.insert("REMOTE_ADDR".into(), remote.into());
};

sentry_req
Expand Down Expand Up @@ -476,8 +474,8 @@ mod tests {
use std::io;

use actix_web::body::BoxBody;
use actix_web::test::{call_service, init_service, TestRequest};
use actix_web::{get, web, App, HttpRequest, HttpResponse};
use actix_web::test::{TestRequest, call_service, init_service};
use actix_web::{App, HttpRequest, HttpResponse, get, web};
use futures::executor::block_on;

use futures::future::join_all;
Expand Down
21 changes: 14 additions & 7 deletions sentry-anyhow/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@
#![warn(missing_docs)]
#![deny(unsafe_code)]

use sentry_core::Hub;
use sentry_core::protocol::Event;
use sentry_core::types::Uuid;
use sentry_core::Hub;

/// Captures an [`anyhow::Error`].
///
Expand Down Expand Up @@ -101,11 +101,20 @@ impl AnyhowHubExt for Hub {
mod tests {
use super::*;

fn anyhow_with_captured_backtrace() -> anyhow::Error {
let err = anyhow::anyhow!("Oh jeez");
assert_eq!(
err.backtrace().status(),
std::backtrace::BacktraceStatus::Captured,
"run with RUST_BACKTRACE=1 (anyhow captures only when that is preset; \
tests must not call std::env::set_var)"
);
err
}

#[test]
fn test_event_from_error_with_backtrace() {
std::env::set_var("RUST_BACKTRACE", "1");

let event = event_from_error(&anyhow::anyhow!("Oh jeez"));
let event = event_from_error(&anyhow_with_captured_backtrace());

let stacktrace = event.exception[0].stacktrace.as_ref().unwrap();
let found_test_fn = stacktrace
Expand All @@ -121,9 +130,7 @@ mod tests {

#[test]
fn test_capture_anyhow_uses_event_from_error_helper() {
std::env::set_var("RUST_BACKTRACE", "1");

let err = &anyhow::anyhow!("Oh jeez");
let err = &anyhow_with_captured_backtrace();

let event = event_from_error(err);
let events = sentry::test::with_captured_events(|| {
Expand Down
2 changes: 1 addition & 1 deletion sentry-backtrace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ mod trim;
mod utils;

pub use crate::integration::{
current_thread, AttachStacktraceIntegration, ProcessStacktraceIntegration,
AttachStacktraceIntegration, ProcessStacktraceIntegration, current_thread,
};
pub use crate::parse::parse_stacktrace;
pub use crate::process::{backtrace_to_stacktrace, process_event_stacktrace};
Expand Down
7 changes: 6 additions & 1 deletion sentry-backtrace/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,12 @@ mod tests {
&strip_symbol("<fn() as core[bb3d6b31f0e973c8]::ops::function::FnOnce<()>>::call_once"),
"<fn() as core::ops::function::FnOnce<()>>::call_once"
);
assert_eq!(&strip_symbol("<std[550525b9dd91a68e]::thread::local::LocalKey<(arc_swap[1d34a79be67db79e]::ArcSwapAny<alloc[bc7f897b574022f6]::sync::Arc<sentry_core[1d5336878cce1456]::hub::Hub>>, core[bb3d6b31f0e973c8]::cell::Cell<bool>)>>::with::<<sentry_core[1d5336878cce1456]::hub::Hub>::with<<sentry_core[1d5336878cce1456]::hub::Hub>::with_active<sentry_core[1d5336878cce1456]::api::with_integration<sentry_panic[c87c9124ff32f50e]::PanicIntegration, sentry_panic[c87c9124ff32f50e]::panic_handler::{closure#0}, ()>::{closure#0}, ()>::{closure#0}, ()>::{closure#0}, ()>"), "<std::thread::local::LocalKey<(arc_swap::ArcSwapAny<alloc::sync::Arc<sentry_core::hub::Hub>>, core::cell::Cell<bool>)>>::with::<<sentry_core::hub::Hub>::with<<sentry_core::hub::Hub>::with_active<sentry_core::api::with_integration<sentry_panic::PanicIntegration, sentry_panic::panic_handler::{closure#0}, ()>::{closure#0}, ()>::{closure#0}, ()>::{closure#0}, ()>");
assert_eq!(
&strip_symbol(
"<std[550525b9dd91a68e]::thread::local::LocalKey<(arc_swap[1d34a79be67db79e]::ArcSwapAny<alloc[bc7f897b574022f6]::sync::Arc<sentry_core[1d5336878cce1456]::hub::Hub>>, core[bb3d6b31f0e973c8]::cell::Cell<bool>)>>::with::<<sentry_core[1d5336878cce1456]::hub::Hub>::with<<sentry_core[1d5336878cce1456]::hub::Hub>::with_active<sentry_core[1d5336878cce1456]::api::with_integration<sentry_panic[c87c9124ff32f50e]::PanicIntegration, sentry_panic[c87c9124ff32f50e]::panic_handler::{closure#0}, ()>::{closure#0}, ()>::{closure#0}, ()>::{closure#0}, ()>"
),
"<std::thread::local::LocalKey<(arc_swap::ArcSwapAny<alloc::sync::Arc<sentry_core::hub::Hub>>, core::cell::Cell<bool>)>>::with::<<sentry_core::hub::Hub>::with<<sentry_core::hub::Hub>::with_active<sentry_core::api::with_integration<sentry_panic::PanicIntegration, sentry_panic::panic_handler::{closure#0}, ()>::{closure#0}, ()>::{closure#0}, ()>::{closure#0}, ()>"
);
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion sentry-contexts/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fs::File;
use std::io::Write;
use std::path::Path;

use rustc_version::{version, version_meta, Channel};
use rustc_version::{Channel, version, version_meta};

fn main() -> Result<(), Box<dyn std::error::Error>> {
let out_dir = env::var("OUT_DIR")?;
Expand Down
13 changes: 6 additions & 7 deletions sentry-contexts/src/integration.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::borrow::Cow;

use sentry_core::protocol::map::Entry;
use sentry_core::protocol::Event;
use sentry_core::protocol::map::Entry;
use sentry_core::{ClientOptions, Integration};

use crate::utils::{device_context, os_context, rust_context, server_name};
Expand Down Expand Up @@ -82,12 +82,11 @@ impl Integration for ContextIntegration {
mut event: Event<'static>,
_cfg: &ClientOptions,
) -> Option<Event<'static>> {
if self.add_os {
if let Entry::Vacant(entry) = event.contexts.entry("os".to_string()) {
if let Some(os) = os_context() {
entry.insert(os);
}
}
if self.add_os
&& let Entry::Vacant(entry) = event.contexts.entry("os".to_string())
&& let Some(os) = os_context()
{
entry.insert(os);
}
if self.add_rust {
event
Expand Down
7 changes: 4 additions & 3 deletions sentry-contexts/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,10 @@ mod model_support {
let dot_count = v.split('.').count() - 1;
assert_eq!(dot_count, 2);
let b = get_macos_build().unwrap();
assert!(b
.chars()
.all(|c| c.is_ascii_alphabetic() || c.is_ascii_digit()));
assert!(
b.chars()
.all(|c| c.is_ascii_alphabetic() || c.is_ascii_digit())
);
}
}

Expand Down
2 changes: 1 addition & 1 deletion sentry-core/benches/scope_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use std::ops::Range;
#[cfg(feature = "client")]
use std::sync::Arc;

use criterion::{criterion_group, criterion_main, Criterion};
use criterion::{Criterion, criterion_group, criterion_main};
use sentry::protocol::Breadcrumb;
#[cfg(not(feature = "client"))]
use sentry_core as sentry;
Expand Down
2 changes: 1 addition & 1 deletion sentry-core/src/client/batcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use std::thread::JoinHandle;
use std::time::{Duration, Instant};

use super::EnvelopeSender;
use crate::protocol::EnvelopeItem;
use crate::Envelope;
use crate::protocol::EnvelopeItem;
use sentry_types::protocol::v7::Log;
#[cfg(feature = "metrics")]
use sentry_types::protocol::v7::Metric;
Expand Down
2 changes: 1 addition & 1 deletion sentry-core/src/client/client_reports/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};

use sentry_types::protocol::v7::client_report::{Category, Item, Reason, Report};
use sentry_types::IndexedEnum;
use sentry_types::protocol::v7::client_report::{Category, Item, Reason, Report};

const ARRAY_SIZE: usize = Reason::VARIANTS.len() * Category::VARIANTS.len();

Expand Down
2 changes: 1 addition & 1 deletion sentry-core/src/client/client_reports/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
#[cfg(all(target_has_atomic = "64", target_has_atomic = "8"))]
use std::sync::Arc;

use sentry_types::protocol::v7::ClientReport;
#[cfg(all(target_has_atomic = "64", target_has_atomic = "8"))]
use sentry_types::protocol::v7::client_report::ItemLoss;
use sentry_types::protocol::v7::client_report::{Category, LossSource, Reason};
use sentry_types::protocol::v7::ClientReport;

#[cfg(all(target_has_atomic = "64", target_has_atomic = "8"))]
use self::inner::ClientReportAggregatorInner;
Expand Down
2 changes: 1 addition & 1 deletion sentry-core/src/client/envelope_sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
use std::sync::Arc;
use std::time::Duration;

use sentry_types::protocol::v7::EnvelopeItem;
use sentry_types::protocol::v7::client_report::{
Category as ClientReportCategory, LossSource, Reason as ClientReportReason,
};
use sentry_types::protocol::v7::EnvelopeItem;

use self::slot::TransportSlot;
use super::client_reports::{ClientReportAggregator, Recorder};
Expand Down
21 changes: 10 additions & 11 deletions sentry-core/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ use sentry_types::random_uuid;

#[cfg(any(feature = "logs", feature = "metrics"))]
use self::batcher::Batcher;
#[cfg(feature = "release-health")]
use crate::SessionMode;
use crate::constants::SDK_INFO;
use crate::protocol::{ClientSdkInfo, Event};
#[cfg(feature = "release-health")]
use crate::session::SessionFlusher;
use crate::types::{Dsn, Uuid};
#[cfg(feature = "release-health")]
use crate::SessionMode;
use crate::{ClientOptions, Envelope, EventSamplingStrategy, Hub, Integration, Scope};

#[cfg(feature = "logs")]
Expand Down Expand Up @@ -559,10 +559,10 @@ impl Client {
sentry_debug!("[Client] called capture_log, but options.enable_logs is set to false");
return;
}
if let Some(log) = self.prepare_log(log, scope) {
if let Some(ref batcher) = *self.logs_batcher.read().unwrap() {
batcher.enqueue(log);
}
if let Some(log) = self.prepare_log(log, scope)
&& let Some(ref batcher) = *self.logs_batcher.read().unwrap()
{
batcher.enqueue(log);
}
}

Expand Down Expand Up @@ -600,15 +600,14 @@ impl Client {
return;
}

if let Some(metric) = self.prepare_metric(metric, scope) {
if let Some(batcher) = self
if let Some(metric) = self.prepare_metric(metric, scope)
&& let Some(batcher) = self
.metrics_batcher
.read()
.expect("metrics batcher lock could not be acquired")
.as_ref()
{
batcher.enqueue(metric);
}
{
batcher.enqueue(metric);
}
}

Expand Down
2 changes: 1 addition & 1 deletion sentry-core/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::error::Error;

use crate::Hub;
use crate::protocol::{Event, Exception, Level};
use crate::types::Uuid;
use crate::Hub;

impl Hub {
/// Capture any `std::error::Error`.
Expand Down
2 changes: 1 addition & 1 deletion sentry-core/src/futures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl<F> SentryFutureExt for F where F: Future {}
#[cfg(all(test, feature = "test"))]
mod tests {
use crate::test::with_captured_events;
use crate::{capture_message, configure_scope, Hub, Level, SentryFutureExt};
use crate::{Hub, Level, SentryFutureExt, capture_message, configure_scope};
use tokio::runtime::Runtime;

#[test]
Expand Down
5 changes: 2 additions & 3 deletions sentry-core/src/hub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,10 @@ impl Hub {
{
use_without_client!(f);
with_client_impl! {{
if let Some(client) = self.client() {
if let Some(integration) = client.get_integration::<I>() {
if let Some(client) = self.client()
&& let Some(integration) = client.get_integration::<I>() {
return f(integration);
}
}
Default::default()
}}
}
Expand Down
Loading
Loading