Skip to content

Commit

Permalink
Merge 3dfbc48 into 37be013
Browse files Browse the repository at this point in the history
  • Loading branch information
loewenheim authored Feb 8, 2023
2 parents 37be013 + 3dfbc48 commit a12490b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- `debug_images` is now a default feature. ([#545](https://github.com/getsentry/sentry-rust/pull/545)
- 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))
- 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))

**Fixes**:

Expand Down
37 changes: 35 additions & 2 deletions sentry-core/src/performance.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::sync::Arc;
use std::sync::Mutex;
use std::collections::BTreeMap;
use std::ops::{Deref, DerefMut};
use std::sync::{Arc, Mutex, MutexGuard};

#[cfg(all(feature = "profiling", target_family = "unix"))]
use crate::profiling;
Expand Down Expand Up @@ -601,6 +602,23 @@ impl Transaction {
}
}

/// A smart pointer to a span's [`data` field](protocol::Span::data).
pub struct Data<'a>(MutexGuard<'a, protocol::Span>);

impl<'a> Deref for Data<'a> {
type Target = BTreeMap<String, protocol::Value>;

fn deref(&self) -> &Self::Target {
&self.0.data
}
}

impl<'a> DerefMut for Data<'a> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0.data
}
}

/// A running Performance Monitoring Span.
///
/// The span needs to be explicitly finished via [`Span::finish`], otherwise it
Expand All @@ -621,6 +639,21 @@ impl Span {
span.data.insert(key.into(), value);
}

/// Returns a smart pointer to the span's [`data` field](protocol::Span::data).
///
/// Since [`Data`] implements `Deref` and `DerefMut`, this can be used to read and mutate
/// the span data.
///
/// # Concurrency
/// In order to obtain any kind of reference to the `data` field,
/// a `Mutex` needs to be locked. The returned `Data` holds on to this lock
/// for as long as it lives. Therefore you must take care not to keep the returned
/// `Data` around too long or it will never relinquish the lock and you may run into
/// a deadlock.
pub fn data(&self) -> Data {
Data(self.span.lock().unwrap())
}

/// Get the TransactionContext of the Span.
///
/// Note that this clones the underlying value.
Expand Down

0 comments on commit a12490b

Please sign in to comment.