Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sentry-core: add public getters for TransactionContext #514

Merged
merged 3 commits into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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**:
Expand Down
28 changes: 27 additions & 1 deletion sentry-core/src/performance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,21 @@ impl TransactionContext {
pub fn set_sampled(&mut self, sampled: impl Into<Option<bool>>) {
self.sampled = sampled.into();
}

/// Get the sampling decision for this Transaction.
pub fn sampled(&self) -> Option<bool> {
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
Expand Down Expand Up @@ -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() {
Expand All @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions sentry/tests/test_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
},
));
Expand Down