Skip to content

Commit 3c8a190

Browse files
committed
Refactoring to correctly fetch tags from context
1 parent f0ae4d8 commit 3c8a190

File tree

4 files changed

+102
-21
lines changed

4 files changed

+102
-21
lines changed

sentry-core/src/performance.rs

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

529+
/// Iterable for a transaction's [tags attributes](protocol::Transaction::tags).
530+
pub struct TransactionTags<'a>(MutexGuard<'a, TransactionInner>);
531+
impl<'a> TransactionTags<'a> {
532+
#[inline]
533+
/// Returns iterator over available tags, if transaction is sampled
534+
pub fn iter(&'a self) -> Option<impl Iterator<Item = (&'a String, &'a String)> + 'a> {
535+
self.0.transaction.as_ref().map(|tx| tx.tags.iter())
536+
}
537+
538+
#[inline]
539+
/// Converts self into data pointer
540+
pub fn into_data(self) -> TransactionData<'a> {
541+
TransactionData(self.0)
542+
}
543+
}
544+
529545
/// Iterable for a transaction's [data attributes](protocol::TraceContext::data).
530546
pub struct TransactionData<'a>(MutexGuard<'a, TransactionInner>);
531547

@@ -647,6 +663,12 @@ impl Transaction {
647663
TransactionData(self.inner.lock().unwrap())
648664
}
649665

666+
/// Returns an iterating accessor to the transaction's
667+
/// [tags attributes](protocol::TraceContext::tags).
668+
pub fn tags(&self) -> TransactionTags {
669+
TransactionTags(self.inner.lock().unwrap())
670+
}
671+
650672
/// Get the TransactionContext of the Transaction.
651673
///
652674
/// Note that this clones the underlying value.
@@ -756,6 +778,38 @@ impl Transaction {
756778
}
757779
}
758780

781+
/// A smart pointer to a span's [`tags` field](protocol::Span::tags).
782+
pub struct Tags<'a>(MutexGuard<'a, protocol::Span>);
783+
784+
impl<'a> Tags<'a> {
785+
/// Set some tag to be sent with this Span.
786+
pub fn set_tag(&mut self, key: String, value: String) {
787+
self.0.tags.insert(key, value);
788+
}
789+
790+
#[inline]
791+
/// Moves pointer to [`data` field](protocol::Span::data)
792+
pub fn into_data(self) -> Data<'a> {
793+
Data(self.0)
794+
}
795+
}
796+
797+
impl Deref for Tags<'_> {
798+
type Target = BTreeMap<String, String>;
799+
800+
#[inline]
801+
fn deref(&self) -> &Self::Target {
802+
&self.0.tags
803+
}
804+
}
805+
806+
impl DerefMut for Tags<'_> {
807+
#[inline]
808+
fn deref_mut(&mut self) -> &mut Self::Target {
809+
&mut self.0.tags
810+
}
811+
}
812+
759813
/// A smart pointer to a span's [`data` field](protocol::Span::data).
760814
pub struct Data<'a>(MutexGuard<'a, protocol::Span>);
761815

@@ -826,6 +880,11 @@ impl Span {
826880
Data(self.span.lock().unwrap())
827881
}
828882

883+
/// Returns a smart pointer to the span's [`tags` field](protocol::Span::tags).
884+
pub fn tags(&self) -> Tags {
885+
Tags(self.span.lock().unwrap())
886+
}
887+
829888
/// Get the TransactionContext of the Span.
830889
///
831890
/// Note that this clones the underlying value.

sentry-tracing/src/converters.rs

+34-20
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,34 @@ where
7575
if let Some(span_data) = ext.get::<SentrySpanData>() {
7676
match &span_data.sentry_span {
7777
TransactionOrSpan::Span(span) => {
78-
for (key, value) in span.data().iter() {
79-
visitor.propagate_span_attr(key, value, propagation, name);
78+
let tags = span.tags();
79+
80+
if propagation.is_tags_enabled() {
81+
for (key, value) in tags.iter() {
82+
visitor.propagate_span_tag(key, value);
83+
}
84+
}
85+
86+
if propagation.is_attrs_enabled() {
87+
for (key, value) in tags.into_data().iter() {
88+
visitor.propagate_span_attr(key, value, name);
89+
}
8090
}
8191
}
8292
TransactionOrSpan::Transaction(transaction) => {
83-
for (key, value) in transaction.data().iter() {
84-
visitor.propagate_span_attr(key, value, propagation, name);
93+
let tags = transaction.tags();
94+
if propagation.is_tags_enabled() {
95+
if let Some(tags) = tags.iter() {
96+
for (key, value) in tags {
97+
visitor.propagate_span_tag(key, value);
98+
}
99+
}
100+
}
101+
102+
if propagation.is_attrs_enabled() {
103+
for (key, value) in tags.into_data().iter() {
104+
visitor.propagate_span_attr(key, value, name);
105+
}
85106
}
86107
}
87108
}
@@ -100,23 +121,16 @@ pub(crate) struct FieldVisitor {
100121
}
101122

102123
impl FieldVisitor {
103-
fn propagate_span_attr(
104-
&mut self,
105-
key: &str,
106-
value: &Value,
107-
span_propagation: SpanPropagation,
108-
span_name: &str,
109-
) {
124+
fn propagate_span_tag(&mut self, key: &str, value: &str) {
125+
//Propagate tags as it is, it will be extracted later on
126+
let tag = format!("{TAGS_PREFIX}{key}");
127+
self.json_values.entry(tag).or_insert_with(|| value.into());
128+
}
129+
130+
fn propagate_span_attr(&mut self, key: &str, value: &Value, span_name: &str) {
110131
if key != "message" {
111-
if span_propagation.is_tags_enabled() && key.starts_with(TAGS_PREFIX) {
112-
//Propagate tags as it is, it will be extracted later on
113-
if !self.json_values.contains_key(key) {
114-
self.json_values.insert(key.to_owned(), value.clone());
115-
}
116-
} else if span_propagation.is_attrs_enabled() {
117-
let key = format!("{}:{}", span_name, key);
118-
self.json_values.insert(key, value.clone());
119-
}
132+
let key = format!("{}:{}", span_name, key);
133+
self.json_values.insert(key, value.clone());
120134
}
121135
}
122136

sentry-tracing/tests/shared.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use sentry::{ClientOptions, Hub};
22
use sentry_core::test::TestTransport;
3+
use sentry_tracing::SpanPropagation;
34

45
use std::sync::Arc;
56

@@ -17,7 +18,7 @@ pub fn init_sentry(traces_sample_rate: f32) -> Arc<TestTransport> {
1718
Hub::current().bind_client(Some(Arc::new(options.into())));
1819

1920
let _ = tracing_subscriber::registry()
20-
.with(sentry_tracing::layer().enable_span_attributes())
21+
.with(sentry_tracing::layer().enable_span_propagation(SpanPropagation::All))
2122
.try_init();
2223

2324
transport

sentry-tracing/tests/smoke.rs

+7
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ fn should_instrument_function_with_event() {
1919
unexpected => panic!("Expected event, but got {:#?}", unexpected),
2020
};
2121

22+
//Event must be get tag attached
23+
let event_tag = event
24+
.tags
25+
.get("tag")
26+
.expect("event should be associated with span's tag");
27+
assert_eq!(event_tag, "key");
28+
2229
//Validate transaction is created
2330
let trace = match event.contexts.get("trace").expect("to get 'trace' context") {
2431
sentry::protocol::Context::Trace(trace) => trace,

0 commit comments

Comments
 (0)