Skip to content

Commit 8cc4848

Browse files
authored
Give access to is_sampled to allow users to dodge work in unsampled case (#669)
1 parent 36f1384 commit 8cc4848

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

sentry-core/src/performance.rs

+23-3
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl TransactionContext {
8484
/// can be used for distributed tracing.
8585
#[must_use = "this must be used with `start_transaction`"]
8686
pub fn new(name: &str, op: &str) -> Self {
87-
Self::continue_from_headers(name, op, vec![])
87+
Self::continue_from_headers(name, op, std::iter::empty())
8888
}
8989

9090
/// Creates a new Transaction Context based on the distributed tracing `headers`.
@@ -200,7 +200,8 @@ impl TransactionContext {
200200
///
201201
/// If the context did not have this key present, None is returned.
202202
///
203-
/// If the context did have this key present, the value is updated, and the old value is returned.
203+
/// If the context did have this key present, the value is updated, and the old value is
204+
/// returned.
204205
pub fn custom_insert(
205206
&mut self,
206207
key: String,
@@ -301,6 +302,14 @@ impl TransactionOrSpan {
301302
}
302303
}
303304

305+
/// Get the sampling decision for this Transaction/Span.
306+
pub fn is_sampled(&self) -> bool {
307+
match self {
308+
TransactionOrSpan::Transaction(transaction) => transaction.is_sampled(),
309+
TransactionOrSpan::Span(span) => span.is_sampled(),
310+
}
311+
}
312+
304313
/// Starts a new child Span with the given `op` and `description`.
305314
///
306315
/// The span must be explicitly finished via [`Span::finish`], as it will
@@ -483,7 +492,8 @@ impl Transaction {
483492
}
484493
}
485494

486-
/// Returns an iterating accessor to the transaction's [`extra` field](protocol::Transaction::extra).
495+
/// Returns an iterating accessor to the transaction's
496+
/// [`extra` field](protocol::Transaction::extra).
487497
///
488498
/// # Concurrency
489499
/// In order to obtain any kind of reference to the `extra` field,
@@ -536,6 +546,11 @@ impl Transaction {
536546
}
537547
}
538548

549+
/// Get the sampling decision for this Transaction.
550+
pub fn is_sampled(&self) -> bool {
551+
self.inner.lock().unwrap().sampled
552+
}
553+
539554
/// Finishes the Transaction.
540555
///
541556
/// This records the end timestamp and sends the transaction together with
@@ -708,6 +723,11 @@ impl Span {
708723
}
709724
}
710725

726+
/// Get the sampling decision for this Span.
727+
pub fn is_sampled(&self) -> bool {
728+
self.sampled
729+
}
730+
711731
/// Finishes the Span.
712732
///
713733
/// This will record the end timestamp and add the span to the transaction

0 commit comments

Comments
 (0)