Skip to content

Commit 41698e9

Browse files
committed
Add data option in tracing integration to force sampling of transaction
1 parent 196501d commit 41698e9

File tree

2 files changed

+41
-18
lines changed

2 files changed

+41
-18
lines changed

sentry-tracing/src/layer.rs

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@ use tracing_subscriber::layer::{Context, Layer};
1212
use tracing_subscriber::registry::LookupSpan;
1313

1414
use crate::converters::*;
15-
use crate::SENTRY_NAME_FIELD;
16-
use crate::SENTRY_OP_FIELD;
17-
use crate::SENTRY_TRACE_FIELD;
18-
use crate::TAGS_PREFIX;
15+
use crate::{
16+
SENTRY_NAME_FIELD, SENTRY_OP_FIELD, SENTRY_SAMPLE_FIELD, SENTRY_TRACE_FIELD, TAGS_PREFIX,
17+
};
1918

2019
bitflags! {
2120
/// The action that Sentry should perform for a given [`Event`]
@@ -301,7 +300,13 @@ where
301300
return;
302301
}
303302

304-
let (data, sentry_name, sentry_op, sentry_trace) = extract_span_data(attrs);
303+
let SpanData {
304+
json_values: data,
305+
name: sentry_name,
306+
op: sentry_op,
307+
trace: sentry_trace,
308+
sample: force_sample_value,
309+
} = extract_span_data(attrs);
305310
let sentry_name = sentry_name.as_deref().unwrap_or_else(|| span.name());
306311
let sentry_op =
307312
sentry_op.unwrap_or_else(|| format!("{}::{}", span.metadata().target(), span.name()));
@@ -312,7 +317,7 @@ where
312317
let mut sentry_span: sentry_core::TransactionOrSpan = match &parent_sentry_span {
313318
Some(parent) => parent.start_child(&sentry_op, sentry_name).into(),
314319
None => {
315-
let ctx = if let Some(trace_header) = sentry_trace {
320+
let mut ctx = if let Some(trace_header) = sentry_trace {
316321
sentry_core::TransactionContext::continue_from_headers(
317322
sentry_name,
318323
&sentry_op,
@@ -322,6 +327,10 @@ where
322327
sentry_core::TransactionContext::new(sentry_name, &sentry_op)
323328
};
324329

330+
if let Some(force_sample) = force_sample_value {
331+
ctx.set_sampled(force_sample);
332+
}
333+
325334
let tx = sentry_core::start_transaction(ctx);
326335
tx.set_origin("auto.tracing");
327336
tx.into()
@@ -462,16 +471,16 @@ where
462471
Default::default()
463472
}
464473

474+
struct SpanData {
475+
json_values: BTreeMap<&'static str, Value>,
476+
name: Option<String>,
477+
op: Option<String>,
478+
trace: Option<String>,
479+
sample: Option<bool>,
480+
}
465481
/// Extracts the attributes from a span,
466482
/// returning the values of SENTRY_NAME_FIELD, SENTRY_OP_FIELD, SENTRY_TRACE_FIELD separately
467-
fn extract_span_data(
468-
attrs: &span::Attributes,
469-
) -> (
470-
BTreeMap<&'static str, Value>,
471-
Option<String>,
472-
Option<String>,
473-
Option<String>,
474-
) {
483+
fn extract_span_data(attrs: &span::Attributes) -> SpanData {
475484
let mut json_values = VISITOR_BUFFER.with_borrow_mut(|debug_buffer| {
476485
let mut visitor = SpanFieldVisitor {
477486
debug_buffer,
@@ -491,14 +500,27 @@ fn extract_span_data(
491500
_ => None,
492501
});
493502

494-
let sentry_trace = json_values
503+
let trace = json_values
495504
.remove(SENTRY_TRACE_FIELD)
496505
.and_then(|v| match v {
497506
Value::String(s) => Some(s),
498507
_ => None,
499508
});
500509

501-
(json_values, name, op, sentry_trace)
510+
let sample = json_values
511+
.remove(SENTRY_SAMPLE_FIELD)
512+
.and_then(|v: Value| match v {
513+
Value::Bool(b) => Some(b),
514+
_ => None,
515+
});
516+
517+
SpanData {
518+
json_values,
519+
name,
520+
op,
521+
trace,
522+
sample,
523+
}
502524
}
503525

504526
thread_local! {

sentry-tracing/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@
107107
//!
108108
//! ```
109109
//! tracing::error!(
110-
//! field = "value", // will become a context field
111-
//! tags.custom = "value", // will become a tag in Sentry
110+
//! field = "value", // will become a context field
111+
//! tags.custom = "value", // will become a tag in Sentry
112112
//! "this is an error with a custom tag",
113113
//! );
114114
//! ```
@@ -249,3 +249,4 @@ const TAGS_PREFIX: &str = "tags.";
249249
const SENTRY_OP_FIELD: &str = "sentry.op";
250250
const SENTRY_NAME_FIELD: &str = "sentry.name";
251251
const SENTRY_TRACE_FIELD: &str = "sentry.trace";
252+
const SENTRY_SAMPLE_FIELD: &str = "sentry.sample";

0 commit comments

Comments
 (0)