From 8cc4848f115152af6f2ad66d7aeb17bd3bbe38fd Mon Sep 17 00:00:00 2001 From: Thomas B <9094255+Ten0@users.noreply.github.com> Date: Mon, 15 Jul 2024 10:42:34 +0200 Subject: [PATCH] Give access to is_sampled to allow users to dodge work in unsampled case (#669) --- sentry-core/src/performance.rs | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/sentry-core/src/performance.rs b/sentry-core/src/performance.rs index 552cc563..062899ae 100644 --- a/sentry-core/src/performance.rs +++ b/sentry-core/src/performance.rs @@ -84,7 +84,7 @@ impl TransactionContext { /// can be used for distributed tracing. #[must_use = "this must be used with `start_transaction`"] pub fn new(name: &str, op: &str) -> Self { - Self::continue_from_headers(name, op, vec![]) + Self::continue_from_headers(name, op, std::iter::empty()) } /// Creates a new Transaction Context based on the distributed tracing `headers`. @@ -200,7 +200,8 @@ impl TransactionContext { /// /// If the context did not have this key present, None is returned. /// - /// If the context did have this key present, the value is updated, and the old value is returned. + /// If the context did have this key present, the value is updated, and the old value is + /// returned. pub fn custom_insert( &mut self, key: String, @@ -301,6 +302,14 @@ impl TransactionOrSpan { } } + /// Get the sampling decision for this Transaction/Span. + pub fn is_sampled(&self) -> bool { + match self { + TransactionOrSpan::Transaction(transaction) => transaction.is_sampled(), + TransactionOrSpan::Span(span) => span.is_sampled(), + } + } + /// Starts a new child Span with the given `op` and `description`. /// /// The span must be explicitly finished via [`Span::finish`], as it will @@ -483,7 +492,8 @@ impl Transaction { } } - /// Returns an iterating accessor to the transaction's [`extra` field](protocol::Transaction::extra). + /// Returns an iterating accessor to the transaction's + /// [`extra` field](protocol::Transaction::extra). /// /// # Concurrency /// In order to obtain any kind of reference to the `extra` field, @@ -536,6 +546,11 @@ impl Transaction { } } + /// Get the sampling decision for this Transaction. + pub fn is_sampled(&self) -> bool { + self.inner.lock().unwrap().sampled + } + /// Finishes the Transaction. /// /// This records the end timestamp and sends the transaction together with @@ -708,6 +723,11 @@ impl Span { } } + /// Get the sampling decision for this Span. + pub fn is_sampled(&self) -> bool { + self.sampled + } + /// Finishes the Span. /// /// This will record the end timestamp and add the span to the transaction