From b0d6431bf3f46f97fc70ea4c42fbb9273dc76ead Mon Sep 17 00:00:00 2001 From: Tom Milligan Date: Sun, 6 Nov 2022 12:57:43 +0000 Subject: [PATCH 1/2] sentry-core: add public getters for TransactionContext --- sentry-core/src/performance.rs | 28 +++++++++++++++++++++++++++- sentry/tests/test_client.rs | 3 +++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/sentry-core/src/performance.rs b/sentry-core/src/performance.rs index 6672d6de..9bc687b7 100644 --- a/sentry-core/src/performance.rs +++ b/sentry-core/src/performance.rs @@ -154,6 +154,21 @@ impl TransactionContext { pub fn set_sampled(&mut self, sampled: impl Into>) { self.sampled = sampled.into(); } + + /// Get the sampling decision for this Transaction. + pub fn sampled(&self) -> Option { + self.sampled + } + + /// Get the name of this Transaction. + pub fn name(&self) -> &str { + &self.name + } + + /// Get the operation of this Transaction. + pub fn operation(&self) -> &str { + &self.op + } } /// A function to be run for each new transaction, to determine the rate at which @@ -716,6 +731,17 @@ mod tests { assert_eq!(parsed.2, Some(true)); } + #[test] + fn transaction_context_public_getters() { + let mut ctx = TransactionContext::new("test-name", "test-operation"); + assert_eq!(ctx.name(), "test-name"); + assert_eq!(ctx.operation(), "test-operation"); + assert_eq!(ctx.sampled(), None); + + ctx.set_sampled(true); + assert_eq!(ctx.sampled(), Some(true)); + } + #[cfg(feature = "client")] #[test] fn compute_transaction_sample_rate() { @@ -737,7 +763,7 @@ mod tests { ctx.set_sampled(false); assert_eq!(transaction_sample_rate(Some(&|_| { 0.7 }), &ctx, 0.3), 0.7); // But the sampler may choose to inspect parent sampling - let sampler = |ctx: &TransactionContext| match ctx.sampled { + let sampler = |ctx: &TransactionContext| match ctx.sampled() { Some(true) => 0.8, Some(false) => 0.4, None => 0.6, diff --git a/sentry/tests/test_client.rs b/sentry/tests/test_client.rs index a3ca737a..19b3361e 100644 --- a/sentry/tests/test_client.rs +++ b/sentry/tests/test_client.rs @@ -18,6 +18,9 @@ fn test_into_client() { "https://public@example.com/42%21", sentry::ClientOptions { release: Some("foo@1.0".into()), + traces_sampler: Some(Arc::new( + |ctx| if ctx.name().is_empty() { 0.0 } else { 1.0 }, + )), ..Default::default() }, )); From c31fe4a42fb512555c4f594e3f651e346e98d60f Mon Sep 17 00:00:00 2001 From: Tom Milligan Date: Sun, 6 Nov 2022 13:01:56 +0000 Subject: [PATCH 2/2] [fixup] update changelog --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ec4a08dc..4d4fc2a5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## Unreleased + +**Features** + +- Allow `traces_sampler` to inspect well known properties of `TransactionContext` ([#514](https://github.com/getsentry/sentry-rust/pull/514)) + ## 0.28.0 **Breaking Changes**: