Skip to content

Commit 9d0f853

Browse files
Delay sampling of span to finish
1 parent eb902e0 commit 9d0f853

File tree

4 files changed

+13
-14
lines changed

4 files changed

+13
-14
lines changed

sentry-core/src/performance.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -561,8 +561,8 @@ impl<'a> TransactionData<'a> {
561561

562562
impl Transaction {
563563
#[cfg(feature = "client")]
564-
fn new(mut client: Option<Arc<Client>>, ctx: TransactionContext) -> Self {
565-
let (sampled, mut transaction) = match client.as_ref() {
564+
fn new(client: Option<Arc<Client>>, ctx: TransactionContext) -> Self {
565+
let (sampled, transaction) = match client.as_ref() {
566566
Some(client) => (
567567
client.is_transaction_sampled(&ctx),
568568
Some(protocol::Transaction {
@@ -581,13 +581,6 @@ impl Transaction {
581581
..Default::default()
582582
};
583583

584-
// throw away the transaction here, which means there is nothing to send
585-
// on `finish`.
586-
if !sampled {
587-
transaction = None;
588-
client = None;
589-
}
590-
591584
Self {
592585
inner: Arc::new(Mutex::new(TransactionInner {
593586
client,
@@ -699,6 +692,12 @@ impl Transaction {
699692
pub fn finish(self) {
700693
with_client_impl! {{
701694
let mut inner = self.inner.lock().unwrap();
695+
696+
// Discard `Transaction` unless sampled.
697+
if !inner.sampled {
698+
return;
699+
}
700+
702701
if let Some(mut transaction) = inner.transaction.take() {
703702
if let Some(client) = inner.client.take() {
704703
transaction.finish();

sentry-tracing/tests/breadcrumbs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ mod shared;
22

33
#[test]
44
fn breadcrumbs_should_capture_span_fields() {
5-
let transport = shared::init_sentry();
5+
let transport = shared::init_sentry(0.0); // This test should work even if we are not sampling transactions.
66

77
foo();
88

99
let data = transport.fetch_and_clear_envelopes();
10-
assert_eq!(data.len(), 2);
10+
assert_eq!(data.len(), 1);
1111

1212
let event = data.first().expect("should have 1 event");
1313
let event = match event.items().next().unwrap() {

sentry-tracing/tests/shared.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ use sentry_core::test::TestTransport;
33

44
use std::sync::Arc;
55

6-
pub fn init_sentry() -> Arc<TestTransport> {
6+
pub fn init_sentry(traces_sample_rate: f32) -> Arc<TestTransport> {
77
use tracing_subscriber::prelude::*;
88

99
let transport = TestTransport::new();
1010
let options = ClientOptions {
1111
dsn: Some("https://test@sentry-tracing.com/test".parse().unwrap()),
1212
transport: Some(Arc::new(transport.clone())),
1313
sample_rate: 1.0,
14-
traces_sample_rate: 1.0,
14+
traces_sample_rate,
1515
..ClientOptions::default()
1616
};
1717
Hub::current().bind_client(Some(Arc::new(options.into())));

sentry-tracing/tests/smoke.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ fn function_with_tags(value: i32) {
77

88
#[test]
99
fn should_instrument_function_with_event() {
10-
let transport = shared::init_sentry();
10+
let transport = shared::init_sentry(1.0); // Sample all spans.
1111

1212
function_with_tags(1);
1313

0 commit comments

Comments
 (0)