From 3ee3dbdb445095490f45fb0a4510eab72e61fb79 Mon Sep 17 00:00:00 2001 From: Sebastian Zivota Date: Fri, 9 Jun 2023 18:10:11 +0200 Subject: [PATCH] feat: Apply tag, extras, contexts from scope to transaction --- sentry-core/src/performance.rs | 90 ++++++++++++++++++++++++++++++++++ sentry-core/src/scope/real.rs | 33 +++++++++++-- 2 files changed, 119 insertions(+), 4 deletions(-) diff --git a/sentry-core/src/performance.rs b/sentry-core/src/performance.rs index e28f46089..126400661 100644 --- a/sentry-core/src/performance.rs +++ b/sentry-core/src/performance.rs @@ -2,6 +2,8 @@ use std::collections::BTreeMap; use std::ops::{Deref, DerefMut}; use std::sync::{Arc, Mutex, MutexGuard}; +use sentry_types::protocol::latest::Context; + #[cfg(all(feature = "profiling", target_family = "unix"))] use crate::profiling; @@ -265,6 +267,32 @@ impl TransactionOrSpan { } } + /// Removes extra information for a key. + pub fn remove_data(&self, key: &str) { + match self { + TransactionOrSpan::Transaction(transaction) => transaction.remove_data(key), + TransactionOrSpan::Span(span) => span.remove_data(key), + } + } + + /// Sets a tag to a specific value. + pub fn set_tag(&self, key: &str, value: V) { + match self { + TransactionOrSpan::Transaction(transaction) => transaction.set_tag(key, value), + TransactionOrSpan::Span(span) => span.set_tag(key, value), + } + } + + /// Removes a tag. + /// + /// If the tag is not set, does nothing. + pub fn remove_tag(&self, key: &str) { + match self { + TransactionOrSpan::Transaction(transaction) => transaction.remove_tag(key), + TransactionOrSpan::Span(span) => span.remove_tag(key), + } + } + /// Get the TransactionContext of the Transaction/Span. /// /// Note that this clones the underlying value. @@ -516,6 +544,48 @@ impl Transaction { } } + /// Removes extra information for a key. + pub fn remove_data(&self, key: &str) { + let mut inner = self.inner.lock().unwrap(); + if let Some(transaction) = inner.transaction.as_mut() { + transaction.extra.remove(key); + } + } + + /// Sets a tag to a specific value. + pub fn set_tag(&self, key: &str, value: V) { + let mut inner = self.inner.lock().unwrap(); + if let Some(transaction) = inner.transaction.as_mut() { + transaction.tags.insert(key.to_string(), value.to_string()); + } + } + + /// Removes a tag. + /// + /// If the tag is not set, does nothing. + pub fn remove_tag(&self, key: &str) { + let mut inner = self.inner.lock().unwrap(); + if let Some(transaction) = inner.transaction.as_mut() { + transaction.tags.remove(key); + } + } + + /// Sets a context for a key. + pub fn set_context>(&self, key: &str, value: C) { + let mut inner = self.inner.lock().unwrap(); + if let Some(transaction) = inner.transaction.as_mut() { + transaction.contexts.insert(key.to_string(), value.into()); + } + } + + /// Removes a context for a key. + pub fn remove_context(&self, key: &str) { + let mut inner = self.inner.lock().unwrap(); + if let Some(transaction) = inner.transaction.as_mut() { + transaction.contexts.remove(key); + } + } + /// Get the TransactionContext of the Transaction. /// /// Note that this clones the underlying value. @@ -666,6 +736,26 @@ impl Span { span.data.insert(key.into(), value); } + /// Removes data for the specified key. + pub fn remove_data(&self, key: &str) { + let mut span = self.span.lock().unwrap(); + span.data.remove(key); + } + + /// Sets a tag to a specific value. + pub fn set_tag(&self, key: &str, value: V) { + let mut span = self.span.lock().unwrap(); + span.tags.insert(key.to_string(), value.to_string()); + } + + /// Removes a tag. + /// + /// If the tag is not set, does nothing. + pub fn remove_tag(&self, key: &str) { + let mut span = self.span.lock().unwrap(); + span.tags.remove(key); + } + /// 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 diff --git a/sentry-core/src/scope/real.rs b/sentry-core/src/scope/real.rs index 0b22940a7..acef83314 100644 --- a/sentry-core/src/scope/real.rs +++ b/sentry-core/src/scope/real.rs @@ -197,6 +197,10 @@ impl Scope { /// Sets a tag to a specific value. pub fn set_tag(&mut self, key: &str, value: V) { Arc::make_mut(&mut self.tags).insert(key.to_string(), value.to_string()); + + if let Some(ref transaction_or_span) = *self.span { + transaction_or_span.set_tag(key, value); + } } /// Removes a tag. @@ -204,26 +208,47 @@ impl Scope { /// If the tag is not set, does nothing. pub fn remove_tag(&mut self, key: &str) { Arc::make_mut(&mut self.tags).remove(key); + + if let Some(ref transaction_or_span) = *self.span { + transaction_or_span.remove_tag(key); + } } /// Sets a context for a key. pub fn set_context>(&mut self, key: &str, value: C) { - Arc::make_mut(&mut self.contexts).insert(key.to_string(), value.into()); + let context = value.into(); + Arc::make_mut(&mut self.contexts).insert(key.to_string(), context.clone()); + + if let Some(TransactionOrSpan::Transaction(ref transaction)) = *self.span { + transaction.set_context(key, context); + } } /// Removes a context for a key. pub fn remove_context(&mut self, key: &str) { Arc::make_mut(&mut self.contexts).remove(key); + + if let Some(TransactionOrSpan::Transaction(ref transaction)) = *self.span { + transaction.remove_context(key); + } } - /// Sets a extra to a specific value. + /// Sets an extra to a specific value. pub fn set_extra(&mut self, key: &str, value: Value) { - Arc::make_mut(&mut self.extra).insert(key.to_string(), value); + Arc::make_mut(&mut self.extra).insert(key.to_string(), value.clone()); + + if let Some(ref transaction_or_span) = *self.span { + transaction_or_span.set_data(key, value); + } } - /// Removes a extra. + /// Removes an extra. pub fn remove_extra(&mut self, key: &str) { Arc::make_mut(&mut self.extra).remove(key); + + if let Some(ref transaction_or_span) = *self.span { + transaction_or_span.remove_data(key); + } } /// Add an event processor to the scope.