Skip to content

Commit 499a435

Browse files
authored
feat(tracing): Add method for accessing span data (#548)
1 parent 37be013 commit 499a435

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
- `debug_images` is now a default feature. ([#545](https://github.com/getsentry/sentry-rust/pull/545)
88
- Added a `from_path_raw` function to `Envelope` that reads an envelope from a file without parsing anything. ([#549](https://github.com/getsentry/sentry-rust/pull/549))
9+
- Added a `data` method to `performance::Span` that gives access to the span's attached data. ([#548](https://github.com/getsentry/sentry-rust/pull/548))
910

1011
**Fixes**:
1112

sentry-core/src/performance.rs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
use std::sync::Arc;
2-
use std::sync::Mutex;
1+
use std::collections::BTreeMap;
2+
use std::ops::{Deref, DerefMut};
3+
use std::sync::{Arc, Mutex, MutexGuard};
34

45
#[cfg(all(feature = "profiling", target_family = "unix"))]
56
use crate::profiling;
@@ -601,6 +602,23 @@ impl Transaction {
601602
}
602603
}
603604

605+
/// A smart pointer to a span's [`data` field](protocol::Span::data).
606+
pub struct Data<'a>(MutexGuard<'a, protocol::Span>);
607+
608+
impl<'a> Deref for Data<'a> {
609+
type Target = BTreeMap<String, protocol::Value>;
610+
611+
fn deref(&self) -> &Self::Target {
612+
&self.0.data
613+
}
614+
}
615+
616+
impl<'a> DerefMut for Data<'a> {
617+
fn deref_mut(&mut self) -> &mut Self::Target {
618+
&mut self.0.data
619+
}
620+
}
621+
604622
/// A running Performance Monitoring Span.
605623
///
606624
/// The span needs to be explicitly finished via [`Span::finish`], otherwise it
@@ -621,6 +639,21 @@ impl Span {
621639
span.data.insert(key.into(), value);
622640
}
623641

642+
/// Returns a smart pointer to the span's [`data` field](protocol::Span::data).
643+
///
644+
/// Since [`Data`] implements `Deref` and `DerefMut`, this can be used to read and mutate
645+
/// the span data.
646+
///
647+
/// # Concurrency
648+
/// In order to obtain any kind of reference to the `data` field,
649+
/// a `Mutex` needs to be locked. The returned `Data` holds on to this lock
650+
/// for as long as it lives. Therefore you must take care not to keep the returned
651+
/// `Data` around too long or it will never relinquish the lock and you may run into
652+
/// a deadlock.
653+
pub fn data(&self) -> Data {
654+
Data(self.span.lock().unwrap())
655+
}
656+
624657
/// Get the TransactionContext of the Span.
625658
///
626659
/// Note that this clones the underlying value.

0 commit comments

Comments
 (0)