Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tracing): Add method for accessing span data #548

Merged
merged 5 commits into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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