|
| 1 | +//! Contains the [`ClientReportRecorder`] type, which allows recording data losses. |
| 2 | +//! |
| 3 | +//! This type is `pub` to allow transports, which are defined outside the `sentry-core` crate, to |
| 4 | +//! record lost events, without giving full access to the underlying [`ClientReportAggregator`]. |
| 5 | +
|
| 6 | +use std::sync::{Arc, Weak}; |
| 7 | + |
| 8 | +use sentry_types::protocol::v7::{DataCategory, DiscardReason}; |
| 9 | + |
| 10 | +use super::{ClientReportAggregator, ClientReportAggregatorInner}; |
| 11 | + |
| 12 | +/// A handle for recording lost Sentry data. |
| 13 | +/// |
| 14 | +/// Lost items recorded here will be aggregated into a [client report] and eventually sent to |
| 15 | +/// Sentry. We attempt to send client reports with a future envelope, so recording lost events |
| 16 | +/// should not lead to increased requests to Sentry. |
| 17 | +/// |
| 18 | +/// Cloning has [`Arc`]-like semantics in the sense that clones record into the same client report |
| 19 | +/// aggregator. |
| 20 | +/// |
| 21 | +/// As client reports require atomics for aggregation, this struct's methods are no-ops on |
| 22 | +/// platforms which lack support for 8-bit and/or 64-bit atomic operations. |
| 23 | +/// |
| 24 | +/// [client report]: https://develop.sentry.dev/sdk/telemetry/client-reports/ |
| 25 | +#[derive(Debug, Clone)] |
| 26 | +pub struct ClientReportRecorder { |
| 27 | + /// The inner aggregator. |
| 28 | + /// |
| 29 | + /// As the recorder only records losses, but cannot retrieve them, it does not make sense for |
| 30 | + /// the recorder to keep the underlying aggregator alive. |
| 31 | + /// |
| 32 | + /// We therefore store `inner` as a [`Weak`] so that we do not keep the aggregator alive. |
| 33 | + /// |
| 34 | + /// In practice, we would expect the recorder not to outlive the underlying aggregator, but in |
| 35 | + /// case it happens, it makes sense to make the `Weak` relationship explicit. |
| 36 | + #[cfg(all(target_has_atomic = "8", target_has_atomic = "64"))] |
| 37 | + inner: Weak<ClientReportAggregatorInner>, |
| 38 | +} |
| 39 | + |
| 40 | +impl ClientReportRecorder { |
| 41 | + /// Record `quantity` lost items, of the given `category`, discarded for the given `reason`. |
| 42 | + pub fn record_loss(&self, category: DataCategory, reason: DiscardReason, quantity: u64) { |
| 43 | + #[cfg(all(target_has_atomic = "8", target_has_atomic = "64"))] |
| 44 | + if let Some(aggregator) = self.aggregator() { |
| 45 | + aggregator.record_loss(category, reason, quantity); |
| 46 | + } |
| 47 | + #[cfg(not(all(target_has_atomic = "8", target_has_atomic = "64")))] |
| 48 | + let _ = (category, reason, quantity); |
| 49 | + } |
| 50 | + |
| 51 | + /// Creates a new no-op [`ClientReportRecorder`]. |
| 52 | + /// |
| 53 | + /// This is used in backwards-compatibility code to handle the case where we might not have an |
| 54 | + /// aggregator. |
| 55 | + /// |
| 56 | + /// To get a useful [`ClientReportRecorder`], use [`ClientReportAggregator::recorder`]. |
| 57 | + pub(crate) fn new_no_op() -> Self { |
| 58 | + Self { inner: Weak::new() } |
| 59 | + } |
| 60 | + |
| 61 | + /// Create a new [`ClientReportRecorder`] which records into the given |
| 62 | + /// [`ClientReportAggregator`]. |
| 63 | + #[cfg(all(target_has_atomic = "8", target_has_atomic = "64"))] |
| 64 | + pub(super) fn new(aggregator: &ClientReportAggregator) -> Self { |
| 65 | + #[cfg(all(target_has_atomic = "8", target_has_atomic = "64"))] |
| 66 | + { |
| 67 | + let ClientReportAggregator { |
| 68 | + inner: aggregator_inner, |
| 69 | + } = aggregator; |
| 70 | + |
| 71 | + let inner = Arc::downgrade(aggregator_inner); |
| 72 | + Self { inner } |
| 73 | + } |
| 74 | + #[cfg(not(all(target_has_atomic = "8", target_has_atomic = "64")))] |
| 75 | + { |
| 76 | + let _ = aggregator; |
| 77 | + Self {} |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + /// Helper to obtain the [`ClientReportAggregator`] we record into, if still alive. |
| 82 | + /// |
| 83 | + /// This works by upgrading the [`Weak`] pointer to the [`ClientReportAggregatorInner`] stored |
| 84 | + /// in `self.inner`, then wrapping it in a [`ClientReportAggregator`]. |
| 85 | + #[cfg(all(target_has_atomic = "8", target_has_atomic = "64"))] |
| 86 | + fn aggregator(&self) -> Option<ClientReportAggregator> { |
| 87 | + self.inner |
| 88 | + .upgrade() |
| 89 | + .map(|inner| ClientReportAggregator { inner }) |
| 90 | + } |
| 91 | +} |
0 commit comments