Skip to content

Commit 92be25b

Browse files
authored
ref: Forward set_transaction to a running Transaction (#433)
A call to `Scope::set_transaction` will now also forward the given name to the currently running performance monitoring transaction to override the `name` it was started with.
1 parent 4a162ef commit 92be25b

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

sentry-core/src/performance.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,12 +257,12 @@ impl TransactionOrSpan {
257257
}
258258

259259
#[derive(Debug)]
260-
struct TransactionInner {
260+
pub(crate) struct TransactionInner {
261261
#[cfg(feature = "client")]
262262
client: Option<Arc<Client>>,
263263
sampled: bool,
264264
context: protocol::TraceContext,
265-
transaction: Option<protocol::Transaction<'static>>,
265+
pub(crate) transaction: Option<protocol::Transaction<'static>>,
266266
}
267267

268268
type TransactionArc = Arc<Mutex<TransactionInner>>;
@@ -274,7 +274,7 @@ type TransactionArc = Arc<Mutex<TransactionInner>>;
274274
/// to Sentry.
275275
#[derive(Clone, Debug)]
276276
pub struct Transaction {
277-
inner: TransactionArc,
277+
pub(crate) inner: TransactionArc,
278278
}
279279

280280
impl Transaction {
@@ -428,7 +428,7 @@ impl Transaction {
428428
/// will not be sent to Sentry.
429429
#[derive(Clone, Debug)]
430430
pub struct Span {
431-
transaction: TransactionArc,
431+
pub(crate) transaction: TransactionArc,
432432
sampled: bool,
433433
span: SpanArc,
434434
}

sentry-core/src/scope/real.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,17 @@ impl Scope {
160160
/// Sets the transaction.
161161
pub fn set_transaction(&mut self, transaction: Option<&str>) {
162162
self.transaction = transaction.map(Arc::from);
163+
if let Some(name) = transaction {
164+
let trx = match self.span.as_ref() {
165+
Some(TransactionOrSpan::Span(span)) => &span.transaction,
166+
Some(TransactionOrSpan::Transaction(trx)) => &trx.inner,
167+
_ => return,
168+
};
169+
170+
if let Some(trx) = trx.lock().unwrap().transaction.as_mut() {
171+
trx.name = Some(name.into());
172+
}
173+
}
163174
}
164175

165176
/// Sets the user for the current scope.

sentry/tests/test_tracing.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,34 @@ fn test_span_record() {
134134
"some data"
135135
);
136136
}
137+
138+
#[test]
139+
fn test_set_transaction() {
140+
let options = sentry::ClientOptions {
141+
traces_sample_rate: 1.0,
142+
..Default::default()
143+
};
144+
145+
let envelopes = sentry::test::with_captured_envelopes_options(
146+
|| {
147+
let ctx = sentry::TransactionContext::new("old name", "ye, whatever");
148+
let trx = sentry::start_transaction(ctx);
149+
sentry::configure_scope(|scope| scope.set_span(Some(trx.clone().into())));
150+
151+
sentry::configure_scope(|scope| scope.set_transaction(Some("new name")));
152+
153+
trx.finish();
154+
},
155+
options,
156+
);
157+
158+
assert_eq!(envelopes.len(), 1);
159+
160+
let envelope_item = envelopes[0].items().next().unwrap();
161+
let transaction = match envelope_item {
162+
sentry::protocol::EnvelopeItem::Transaction(t) => t,
163+
_ => panic!("expected only a transaction item"),
164+
};
165+
166+
assert_eq!(transaction.name.as_deref().unwrap(), "new name");
167+
}

0 commit comments

Comments
 (0)