Skip to content

Commit 66bb8f1

Browse files
fixup! some improvements
1 parent f802c18 commit 66bb8f1

2 files changed

Lines changed: 45 additions & 45 deletions

File tree

sentry-core/src/performance.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,13 @@ impl TransactionOrSpan {
586586
TransactionOrSpan::Span(span) => span.finish(),
587587
}
588588
}
589+
590+
pub(crate) fn span_id(&self) -> SpanId {
591+
match self {
592+
TransactionOrSpan::Transaction(transaction) => transaction.get_trace_context().span_id,
593+
TransactionOrSpan::Span(span) => span.get_span_id(),
594+
}
595+
}
589596
}
590597

591598
#[derive(Debug)]

sentry-core/src/scope/real.rs

Lines changed: 38 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ use std::sync::Mutex;
66
use std::sync::{Arc, PoisonError, RwLock};
77

88
use crate::performance::TransactionOrSpan;
9-
use crate::protocol::{
10-
Attachment, Breadcrumb, Context, Event, Level, TraceContext, Transaction, User, Value,
11-
};
129
#[cfg(feature = "logs")]
1310
use crate::protocol::Log;
1411
#[cfg(any(feature = "logs", feature = "metrics"))]
1512
use crate::protocol::LogAttribute;
1613
#[cfg(feature = "metrics")]
1714
use crate::protocol::Metric;
15+
use crate::protocol::{
16+
Attachment, Breadcrumb, Context, Event, Level, TraceContext, Transaction, User, Value,
17+
};
1818
#[cfg(feature = "release-health")]
1919
use crate::session::Session;
2020
use crate::{Client, SentryTrace, TraceHeader, TraceHeadersIter};
@@ -408,52 +408,26 @@ impl Scope {
408408
/// `send_default_pii` is `true`.
409409
#[cfg(feature = "metrics")]
410410
pub fn apply_to_metric(&self, metric: &mut Metric, send_default_pii: bool) {
411-
if let Some(span) = self.span.as_ref() {
412-
metric.trace_id = span.get_trace_context().trace_id;
413-
} else {
414-
metric.trace_id = self.propagation_context.trace_id;
415-
}
411+
metric.trace_id = match self.span.as_ref().as_ref() {
412+
Some(span) => span.get_trace_context().trace_id,
413+
None => self.propagation_context.trace_id,
414+
};
416415

417-
if metric.span_id.is_none() {
418-
if let Some(span) = self.get_span() {
419-
let span_id = match span {
420-
crate::TransactionOrSpan::Transaction(transaction) => {
421-
transaction.get_trace_context().span_id
422-
}
423-
crate::TransactionOrSpan::Span(span) => span.get_span_id(),
424-
};
425-
metric.span_id = Some(span_id);
426-
}
427-
}
416+
metric.span_id = metric
417+
.span_id
418+
.or_else(|| self.get_span().map(|ts| ts.span_id()));
428419

429-
if send_default_pii {
430-
if let Some(user) = self.user.as_ref() {
431-
if !metric.attributes.contains_key("user.id") {
432-
if let Some(id) = user.id.as_ref() {
433-
metric
434-
.attributes
435-
.insert("user.id".to_owned(), LogAttribute(id.to_owned().into()));
436-
}
437-
}
420+
if !send_default_pii {
421+
return;
422+
}
438423

439-
if !metric.attributes.contains_key("user.name") {
440-
if let Some(name) = user.username.as_ref() {
441-
metric
442-
.attributes
443-
.insert("user.name".to_owned(), LogAttribute(name.to_owned().into()));
444-
}
445-
}
424+
let Some(user) = self.user.as_ref() else {
425+
return;
426+
};
446427

447-
if !metric.attributes.contains_key("user.email") {
448-
if let Some(email) = user.email.as_ref() {
449-
metric.attributes.insert(
450-
"user.email".to_owned(),
451-
LogAttribute(email.to_owned().into()),
452-
);
453-
}
454-
}
455-
}
456-
}
428+
metric.insert_attribute("user.id", user.id.as_deref());
429+
metric.insert_attribute("user.name", user.username.as_deref());
430+
metric.insert_attribute("user.email", user.email.as_deref());
457431
}
458432

459433
/// Set the given [`TransactionOrSpan`] as the active span for this scope.
@@ -501,3 +475,22 @@ impl Scope {
501475
}
502476
}
503477
}
478+
479+
#[cfg(feature = "metrics")]
480+
trait MetricExt {
481+
fn insert_attribute<K, V>(&mut self, key: K, value: V)
482+
where
483+
K: Into<String>,
484+
V: Into<Value>;
485+
}
486+
487+
#[cfg(feature = "metrics")]
488+
impl MetricExt for Metric {
489+
fn insert_attribute<K, V>(&mut self, key: K, value: V)
490+
where
491+
K: Into<String>,
492+
V: Into<Value>,
493+
{
494+
self.attributes.insert(key, LogAttribute(value.into()));
495+
}
496+
}

0 commit comments

Comments
 (0)