Skip to content

Commit 3bcdda3

Browse files
authored
feat(core): transaction.set_data sets data on TraceContext (#739)
* change types that provide access to Transaction data to also access data attributes instead of extra * change tests to reflect new API
1 parent 1d3212a commit 3bcdda3

File tree

3 files changed

+32
-21
lines changed

3 files changed

+32
-21
lines changed

Diff for: sentry-core/src/performance.rs

+22-14
Original file line numberDiff line numberDiff line change
@@ -526,32 +526,32 @@ pub struct Transaction {
526526
pub(crate) inner: TransactionArc,
527527
}
528528

529-
/// Iterable for a transaction's [`extra` field](protocol::Transaction::extra).
529+
/// Iterable for a transaction's [data attributes](protocol::TraceContext::data).
530530
pub struct TransactionData<'a>(MutexGuard<'a, TransactionInner>);
531531

532532
impl<'a> TransactionData<'a> {
533-
/// Iterate over the `extra` map
534-
/// of the [transaction][protocol::Transaction].
533+
/// Iterate over the [data attributes](protocol::TraceContext::data)
534+
/// associated with this [transaction][protocol::Transaction].
535535
///
536-
/// If the transaction not sampled for sending,
537-
/// the metadata will not be populated at all
536+
/// If the transaction is not sampled for sending,
537+
/// the metadata will not be populated at all,
538538
/// so the produced iterator is empty.
539539
pub fn iter(&self) -> Box<dyn Iterator<Item = (&String, &protocol::Value)> + '_> {
540-
if let Some(ref rx) = self.0.transaction {
541-
Box::new(rx.extra.iter())
540+
if self.0.transaction.is_some() {
541+
Box::new(self.0.context.data.iter())
542542
} else {
543543
Box::new(std::iter::empty())
544544
}
545545
}
546546

547-
/// Set some extra information to be sent with this Transaction.
547+
/// Set a data attribute to be sent with this Transaction.
548548
pub fn set_data(&mut self, key: Cow<'a, str>, value: protocol::Value) {
549-
if let Some(transaction) = self.0.transaction.as_mut() {
550-
transaction.extra.insert(key.into(), value);
549+
if self.0.transaction.is_some() {
550+
self.0.context.data.insert(key.into(), value);
551551
}
552552
}
553553

554-
/// Set some extra information to be sent with this Transaction.
554+
/// Set a tag to be sent with this Transaction.
555555
pub fn set_tag(&mut self, key: Cow<'_, str>, value: String) {
556556
if let Some(transaction) = self.0.transaction.as_mut() {
557557
transaction.tags.insert(key.into(), value);
@@ -610,8 +610,16 @@ impl Transaction {
610610
}
611611
}
612612

613-
/// Set some extra information to be sent with this Transaction.
613+
/// Set a data attribute to be sent with this Transaction.
614614
pub fn set_data(&self, key: &str, value: protocol::Value) {
615+
let mut inner = self.inner.lock().unwrap();
616+
if inner.transaction.is_some() {
617+
inner.context.data.insert(key.into(), value);
618+
}
619+
}
620+
621+
/// Set some extra information to be sent with this Transaction.
622+
pub fn set_extra(&self, key: &str, value: protocol::Value) {
615623
let mut inner = self.inner.lock().unwrap();
616624
if let Some(transaction) = inner.transaction.as_mut() {
617625
transaction.extra.insert(key.into(), value);
@@ -627,10 +635,10 @@ impl Transaction {
627635
}
628636

629637
/// Returns an iterating accessor to the transaction's
630-
/// [`extra` field](protocol::Transaction::extra).
638+
/// [data attributes](protocol::TraceContext::data).
631639
///
632640
/// # Concurrency
633-
/// In order to obtain any kind of reference to the `extra` field,
641+
/// In order to obtain any kind of reference to the `TraceContext::data` field,
634642
/// a `Mutex` needs to be locked. The returned `TransactionData` holds on to this lock
635643
/// for as long as it lives. Therefore you must take care not to keep the returned
636644
/// `TransactionData` around too long or it will never relinquish the lock and you may run into

Diff for: sentry-tracing/tests/smoke.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,21 @@ fn should_instrument_function_with_event() {
3333
unexpected => panic!("Expected transaction, but got {:#?}", unexpected),
3434
};
3535
assert_eq!(transaction.tags.len(), 1);
36-
assert_eq!(transaction.extra.len(), 2);
36+
assert_eq!(trace.data.len(), 2);
3737

3838
let tag = transaction
3939
.tags
4040
.get("tag")
4141
.expect("to have tag with name 'tag'");
4242
assert_eq!(tag, "key");
43-
let not_tag = transaction
44-
.extra
43+
let not_tag = trace
44+
.data
4545
.get("not_tag")
46-
.expect("to have extra with name 'not_tag'");
46+
.expect("to have data attribute with name 'not_tag'");
4747
assert_eq!(not_tag, "value");
48-
let value = transaction
49-
.extra
48+
let value = trace
49+
.data
5050
.get("value")
51-
.expect("to have extra with name 'value'");
51+
.expect("to have data attribute with name 'value'");
5252
assert_eq!(value, 1);
5353
}

Diff for: sentry-types/src/protocol/v7.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1435,6 +1435,9 @@ pub struct TraceContext {
14351435
/// Describes the status of the span (e.g. `ok`, `cancelled`, etc.)
14361436
#[serde(default, skip_serializing_if = "Option::is_none")]
14371437
pub status: Option<SpanStatus>,
1438+
/// Optional data attributes to be associated with the transaction.
1439+
#[serde(default, skip_serializing_if = "Map::is_empty")]
1440+
pub data: Map<String, Value>,
14381441
}
14391442

14401443
macro_rules! into_context {

0 commit comments

Comments
 (0)