Skip to content

Commit 15fc5c1

Browse files
authored
sentry-core: add public getters for TransactionContext (#514)
sentry-core: add public getters for TransactionContext
1 parent 6253282 commit 15fc5c1

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
**Features**
6+
7+
- Allow `traces_sampler` to inspect well known properties of `TransactionContext` ([#514](https://github.com/getsentry/sentry-rust/pull/514))
8+
39
## 0.28.0
410

511
**Breaking Changes**:

sentry-core/src/performance.rs

+27-1
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,21 @@ impl TransactionContext {
154154
pub fn set_sampled(&mut self, sampled: impl Into<Option<bool>>) {
155155
self.sampled = sampled.into();
156156
}
157+
158+
/// Get the sampling decision for this Transaction.
159+
pub fn sampled(&self) -> Option<bool> {
160+
self.sampled
161+
}
162+
163+
/// Get the name of this Transaction.
164+
pub fn name(&self) -> &str {
165+
&self.name
166+
}
167+
168+
/// Get the operation of this Transaction.
169+
pub fn operation(&self) -> &str {
170+
&self.op
171+
}
157172
}
158173

159174
/// A function to be run for each new transaction, to determine the rate at which
@@ -716,6 +731,17 @@ mod tests {
716731
assert_eq!(parsed.2, Some(true));
717732
}
718733

734+
#[test]
735+
fn transaction_context_public_getters() {
736+
let mut ctx = TransactionContext::new("test-name", "test-operation");
737+
assert_eq!(ctx.name(), "test-name");
738+
assert_eq!(ctx.operation(), "test-operation");
739+
assert_eq!(ctx.sampled(), None);
740+
741+
ctx.set_sampled(true);
742+
assert_eq!(ctx.sampled(), Some(true));
743+
}
744+
719745
#[cfg(feature = "client")]
720746
#[test]
721747
fn compute_transaction_sample_rate() {
@@ -737,7 +763,7 @@ mod tests {
737763
ctx.set_sampled(false);
738764
assert_eq!(transaction_sample_rate(Some(&|_| { 0.7 }), &ctx, 0.3), 0.7);
739765
// But the sampler may choose to inspect parent sampling
740-
let sampler = |ctx: &TransactionContext| match ctx.sampled {
766+
let sampler = |ctx: &TransactionContext| match ctx.sampled() {
741767
Some(true) => 0.8,
742768
Some(false) => 0.4,
743769
None => 0.6,

sentry/tests/test_client.rs

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ fn test_into_client() {
1818
"https://[email protected]/42%21",
1919
sentry::ClientOptions {
2020
release: Some("[email protected]".into()),
21+
traces_sampler: Some(Arc::new(
22+
|ctx| if ctx.name().is_empty() { 0.0 } else { 1.0 },
23+
)),
2124
..Default::default()
2225
},
2326
));

0 commit comments

Comments
 (0)