Skip to content

Commit

Permalink
send root span's attributes too (if traces enabled)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsionyx committed Dec 2, 2023
1 parent 6a774ee commit 3a1d9ad
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
32 changes: 32 additions & 0 deletions sentry-core/src/performance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,26 @@ pub struct Transaction {
pub(crate) inner: TransactionArc,
}

/// Iterable for a transaction's [`extra` field](protocol::Transaction::extra).
pub struct TransactionData<'a>(MutexGuard<'a, TransactionInner>);

impl<'a> TransactionData<'a> {
/// Iterate over the `extra` map
/// of the [transaction][protocol::Transaction].
///
/// If the transaction not sampled for sending,
/// the metadata will not be populated at all
/// so the produced iterator is empty.
pub fn iter(&self) -> Box<dyn Iterator<Item=(&String, &protocol::Value)> + '_> {
if let Some(ref rx) = self.0.transaction {
Box::new(rx.extra.iter())
}
else {
Box::new(std::iter::empty())
}
}
}

impl Transaction {
#[cfg(feature = "client")]
fn new(mut client: Option<Arc<Client>>, ctx: TransactionContext) -> Self {
Expand Down Expand Up @@ -464,6 +484,18 @@ impl Transaction {
}
}

/// Returns an iterating accessor to the transaction's [`extra` field](protocol::Transaction::extra).
///
/// # Concurrency
/// In order to obtain any kind of reference to the `extra` field,
/// a `Mutex` needs to be locked. The returned `TransactionData` holds on to this lock
/// for as long as it lives. Therefore you must take care not to keep the returned
/// `TransactionData` around too long or it will never relinquish the lock and you may run into
/// a deadlock.
pub fn data(&self) -> TransactionData {
TransactionData(self.inner.lock().unwrap())
}

/// Get the TransactionContext of the Transaction.
///
/// Note that this clones the underlying value.
Expand Down
7 changes: 6 additions & 1 deletion sentry-tracing/src/converters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@ where
}
}
TransactionOrSpan::Transaction(transaction) => {
// TODO: extract data from transaction
for (key, value) in transaction.data().iter() {
if key != "message" {
let key = format!("{}:{}", name, key);
visitor.json_values.insert(key, value.clone());
}
}
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions sentry-tracing/src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ impl<S> SentryLayer<S> {


/// Enable every parent span's attributes to be sent along with own event's attributes.
///
/// Note that the root span is considered a [transaction][sentry_core::protocol::Transaction]
/// so its context will only be grabbed only if you set the transaction to be sampled.
/// The most straightforward way to do this is to set
/// the [traces_sample_rate][sentry_core::ClientOptions::traces_sample_rate] to `1.0`
/// while configuring your sentry client.
#[must_use]
pub fn enable_span_attributes(mut self) -> Self {
self.with_span_attributes = true;
Expand Down

0 comments on commit 3a1d9ad

Please sign in to comment.