diff --git a/sentry-core/src/performance.rs b/sentry-core/src/performance.rs index 154b7b11..91b6f99f 100644 --- a/sentry-core/src/performance.rs +++ b/sentry-core/src/performance.rs @@ -257,12 +257,12 @@ impl TransactionOrSpan { } #[derive(Debug)] -struct TransactionInner { +pub(crate) struct TransactionInner { #[cfg(feature = "client")] client: Option>, sampled: bool, context: protocol::TraceContext, - transaction: Option>, + pub(crate) transaction: Option>, } type TransactionArc = Arc>; @@ -274,7 +274,7 @@ type TransactionArc = Arc>; /// to Sentry. #[derive(Clone, Debug)] pub struct Transaction { - inner: TransactionArc, + pub(crate) inner: TransactionArc, } impl Transaction { @@ -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, } diff --git a/sentry-core/src/scope/real.rs b/sentry-core/src/scope/real.rs index c424f1a9..fbdc6df8 100644 --- a/sentry-core/src/scope/real.rs +++ b/sentry-core/src/scope/real.rs @@ -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 { + 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. diff --git a/sentry/tests/test_tracing.rs b/sentry/tests/test_tracing.rs index 34c2ca6e..4c8038a0 100644 --- a/sentry/tests/test_tracing.rs +++ b/sentry/tests/test_tracing.rs @@ -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"); +}