Skip to content

Commit 669a78d

Browse files
fixup! simplify
1 parent 0ff33cf commit 669a78d

6 files changed

Lines changed: 51 additions & 374 deletions

File tree

sentry-core/src/client.rs

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ pub struct Client {
6868
#[cfg(feature = "logs")]
6969
default_log_attributes: Option<BTreeMap<String, LogAttribute>>,
7070
#[cfg(feature = "metrics")]
71-
default_metric_attributes: Option<BTreeMap<Cow<'static, str>, LogAttribute>>,
71+
default_metric_attributes: BTreeMap<Cow<'static, str>, LogAttribute>,
7272
integrations: Vec<(TypeId, Arc<dyn Integration>)>,
7373
pub(crate) sdk_info: ClientSdkInfo,
7474
}
@@ -215,7 +215,7 @@ impl Client {
215215
#[cfg(feature = "logs")]
216216
default_log_attributes: None,
217217
#[cfg(feature = "metrics")]
218-
default_metric_attributes: None,
218+
default_metric_attributes: Default::default(),
219219
integrations,
220220
sdk_info,
221221
};
@@ -282,31 +282,24 @@ impl Client {
282282

283283
#[cfg(feature = "metrics")]
284284
fn cache_default_metric_attributes(&mut self) {
285-
let mut attributes = BTreeMap::new();
286-
287-
if let Some(environment) = self.options.environment.as_ref() {
288-
attributes.insert("sentry.environment".into(), environment.clone().into());
289-
}
290-
291-
if let Some(release) = self.options.release.as_ref() {
292-
attributes.insert("sentry.release".into(), release.clone().into());
293-
}
294-
295-
attributes.insert(
296-
"sentry.sdk.name".into(),
297-
self.sdk_info.name.to_owned().into(),
298-
);
299-
300-
attributes.insert(
301-
"sentry.sdk.version".into(),
302-
self.sdk_info.version.to_owned().into(),
303-
);
304-
305-
if let Some(server) = &self.options.server_name {
306-
attributes.insert("server.address".into(), server.clone().into());
307-
}
308-
309-
self.default_metric_attributes = Some(attributes);
285+
let always_present_attributes = [
286+
("sentry.sdk.name", &self.sdk_info.name),
287+
("sentry.sdk.version", &self.sdk_info.version),
288+
]
289+
.into_iter()
290+
.map(|(name, value)| (name.into(), value.as_str().into()));
291+
292+
let maybe_present_attributes = [
293+
("sentry.environment", &self.options.environment),
294+
("sentry.release", &self.options.release),
295+
("server.address", &self.options.server_name),
296+
]
297+
.into_iter()
298+
.filter_map(|(name, value)| value.clone().map(|value| (name.into(), value.into())));
299+
300+
self.default_metric_attributes = maybe_present_attributes
301+
.chain(always_present_attributes)
302+
.collect();
310303
}
311304

312305
pub(crate) fn get_integration<I>(&self) -> Option<&I>
@@ -565,11 +558,13 @@ impl Client {
565558
/// Captures a metric and sends it to Sentry.
566559
#[cfg(feature = "metrics")]
567560
pub fn capture_metric(&self, metric: Metric, scope: &Scope) {
568-
if !self.options.enable_metrics {
569-
return;
570-
}
571561
if let Some(metric) = self.prepare_metric(metric, scope) {
572-
if let Some(ref batcher) = *self.metrics_batcher.read().unwrap() {
562+
if let Some(batcher) = self
563+
.metrics_batcher
564+
.read()
565+
.expect("metrics batcher lock could not be acquired")
566+
.as_ref()
567+
{
573568
batcher.enqueue(metric);
574569
}
575570
}
@@ -581,10 +576,8 @@ impl Client {
581576
fn prepare_metric(&self, mut metric: Metric, scope: &Scope) -> Option<Metric> {
582577
scope.apply_to_metric(&mut metric, self.options.send_default_pii);
583578

584-
if let Some(default_attributes) = self.default_metric_attributes.as_ref() {
585-
for (key, val) in default_attributes.iter() {
586-
metric.attributes.entry(key.clone()).or_insert(val.clone());
587-
}
579+
for (key, val) in &self.default_metric_attributes {
580+
metric.attributes.entry(key.clone()).or_insert(val.clone());
588581
}
589582

590583
if let Some(ref func) = self.options.before_send_metric {

sentry-core/src/clientoptions.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,12 @@ impl fmt::Debug for ClientOptions {
240240
struct BeforeSendLog;
241241
self.before_send_log.as_ref().map(|_| BeforeSendLog)
242242
};
243+
#[cfg(feature = "metrics")]
244+
let before_send_metric = {
245+
#[derive(Debug)]
246+
struct BeforeSendMetric;
247+
self.before_send_metric.as_ref().map(|_| BeforeSendMetric)
248+
};
243249
#[derive(Debug)]
244250
struct TransportFactory;
245251

@@ -287,16 +293,9 @@ impl fmt::Debug for ClientOptions {
287293
.field("before_send_log", &before_send_log);
288294

289295
#[cfg(feature = "metrics")]
290-
{
291-
let before_send_metric = {
292-
#[derive(Debug)]
293-
struct BeforeSendMetric;
294-
self.before_send_metric.as_ref().map(|_| BeforeSendMetric)
295-
};
296-
debug_struct
297-
.field("enable_metrics", &self.enable_metrics)
298-
.field("before_send_metric", &before_send_metric);
299-
}
296+
debug_struct
297+
.field("enable_metrics", &self.enable_metrics)
298+
.field("before_send_metric", &before_send_metric);
300299

301300
debug_struct.field("user_agent", &self.user_agent).finish()
302301
}

sentry-core/src/lib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,3 @@ pub use sentry_types::protocol::v7::{Breadcrumb, Envelope, Level, User};
163163

164164
// utilities reused across integrations
165165
pub mod utils;
166-
167-
// telemtry types (limited to metrics for now)
168-
pub mod telemetry;

sentry-core/src/scope/real.rs

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -408,26 +408,19 @@ 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-
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-
};
415-
416-
metric.span_id = metric
417-
.span_id
418-
.or_else(|| self.get_span().map(|ts| ts.span_id()));
419-
420-
if !send_default_pii {
421-
return;
411+
metric.trace_id = (*self.span)
412+
.as_ref()
413+
.map(|span| span.get_trace_context().trace_id)
414+
.unwrap_or(self.propagation_context.trace_id);
415+
416+
metric.span_id = self.get_span().map(|ts| ts.span_id());
417+
418+
// Apply user data if send_default_pii is true
419+
if let Some(user) = send_default_pii.then_some(self.user.as_ref()).flatten() {
420+
metric.insert_attribute("user.id", user.id.as_deref());
421+
metric.insert_attribute("user.name", user.username.as_deref());
422+
metric.insert_attribute("user.email", user.email.as_deref());
422423
}
423-
424-
let Some(user) = self.user.as_ref() else {
425-
return;
426-
};
427-
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());
431424
}
432425

433426
/// Set the given [`TransactionOrSpan`] as the active span for this scope.

0 commit comments

Comments
 (0)