Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref: Forward set_transaction to a running Transaction #433

Merged
merged 1 commit into from
Feb 14, 2022
Merged
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
8 changes: 4 additions & 4 deletions sentry-core/src/performance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,12 @@ impl TransactionOrSpan {
}

#[derive(Debug)]
struct TransactionInner {
pub(crate) struct TransactionInner {
#[cfg(feature = "client")]
client: Option<Arc<Client>>,
sampled: bool,
context: protocol::TraceContext,
transaction: Option<protocol::Transaction<'static>>,
pub(crate) transaction: Option<protocol::Transaction<'static>>,
}

type TransactionArc = Arc<Mutex<TransactionInner>>;
Expand All @@ -274,7 +274,7 @@ type TransactionArc = Arc<Mutex<TransactionInner>>;
/// to Sentry.
#[derive(Clone, Debug)]
pub struct Transaction {
inner: TransactionArc,
pub(crate) inner: TransactionArc,
}

impl Transaction {
Expand Down Expand Up @@ -428,7 +428,7 @@ impl Transaction {
/// will not be sent to Sentry.
#[derive(Clone, Debug)]
pub struct Span {
transaction: TransactionArc,
pub(crate) transaction: TransactionArc,
sampled: bool,
span: SpanArc,
}
Expand Down
11 changes: 11 additions & 0 deletions sentry-core/src/scope/real.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,17 @@ impl Scope {
/// Sets the transaction.
pub fn set_transaction(&mut self, transaction: Option<&str>) {
self.transaction = transaction.map(Arc::from);
if let Some(name) = transaction {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: if you're switching to if let i think it makes more sense to move line 162 into this block as well to keep the logic the same which make reading easier.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The line above should explicitly set None as well though

let trx = match self.span.as_ref() {
Some(TransactionOrSpan::Span(span)) => &span.transaction,
Some(TransactionOrSpan::Transaction(trx)) => &trx.inner,
_ => return,
};

if let Some(trx) = trx.lock().unwrap().transaction.as_mut() {
trx.name = Some(name.into());
}
}
}

/// Sets the user for the current scope.
Expand Down
31 changes: 31 additions & 0 deletions sentry/tests/test_tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,34 @@ fn test_span_record() {
"some data"
);
}

#[test]
fn test_set_transaction() {
let options = sentry::ClientOptions {
traces_sample_rate: 1.0,
..Default::default()
};

let envelopes = sentry::test::with_captured_envelopes_options(
|| {
let ctx = sentry::TransactionContext::new("old name", "ye, whatever");
let trx = sentry::start_transaction(ctx);
sentry::configure_scope(|scope| scope.set_span(Some(trx.clone().into())));

sentry::configure_scope(|scope| scope.set_transaction(Some("new name")));

trx.finish();
},
options,
);

assert_eq!(envelopes.len(), 1);

let envelope_item = envelopes[0].items().next().unwrap();
let transaction = match envelope_item {
sentry::protocol::EnvelopeItem::Transaction(t) => t,
_ => panic!("expected only a transaction item"),
};

assert_eq!(transaction.name.as_deref().unwrap(), "new name");
}