Skip to content

Commit 6a0ffeb

Browse files
pepperoni505lcian
andauthored
feat(core): introduce release-health feature (#749)
* feat: introduce `release-health `feature * enable by default in subcrates --------- Co-authored-by: lcian <[email protected]>
1 parent 1f0be60 commit 6a0ffeb

File tree

12 files changed

+660
-575
lines changed

12 files changed

+660
-575
lines changed

sentry-actix/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ Sentry client extension for actix-web 3.
1212
edition = "2021"
1313
rust-version = "1.81"
1414

15+
[features]
16+
default = ["release-health"]
17+
release-health = ["sentry-core/release-health"]
18+
1519
[dependencies]
1620
actix-web = { version = "4", default-features = false }
1721
bytes = "1.2"

sentry-core/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ client = ["rand"]
2525
# I would love to just have a `log` feature, but this is used inside a macro,
2626
# and macros actually expand features (and extern crate) where they are used!
2727
debug-logs = ["dep:log"]
28-
test = ["client"]
28+
test = ["client", "release-health"]
29+
release-health = []
2930

3031
[dependencies]
3132
cadence = { version = "1.4.0", optional = true }

sentry-core/src/api.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#[cfg(feature = "release-health")]
12
use sentry_types::protocol::v7::SessionStatus;
23

34
use crate::protocol::{Event, Level};
@@ -279,11 +280,13 @@ pub fn last_event_id() -> Option<Uuid> {
279280
///
280281
/// sentry::end_session();
281282
/// ```
283+
#[cfg(feature = "release-health")]
282284
pub fn start_session() {
283285
Hub::with_active(|hub| hub.start_session())
284286
}
285287

286288
/// End the current Release Health Session.
289+
#[cfg(feature = "release-health")]
287290
pub fn end_session() {
288291
end_session_with_status(SessionStatus::Exited)
289292
}
@@ -295,6 +298,7 @@ pub fn end_session() {
295298
///
296299
/// When an `Abnormal` session should be captured, it has to be done explicitly
297300
/// using this function.
301+
#[cfg(feature = "release-health")]
298302
pub fn end_session_with_status(status: SessionStatus) {
299303
Hub::with_active(|hub| hub.end_session_with_status(status))
300304
}

sentry-core/src/client.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@ use std::sync::{Arc, RwLock};
66
use std::time::Duration;
77

88
use rand::random;
9+
#[cfg(feature = "release-health")]
910
use sentry_types::protocol::v7::SessionUpdate;
1011
use sentry_types::random_uuid;
1112

1213
use crate::constants::SDK_INFO;
1314
use crate::protocol::{ClientSdkInfo, Event};
15+
#[cfg(feature = "release-health")]
1416
use crate::session::SessionFlusher;
1517
use crate::types::{Dsn, Uuid};
16-
use crate::{ClientOptions, Envelope, Hub, Integration, Scope, SessionMode, Transport};
18+
#[cfg(feature = "release-health")]
19+
use crate::SessionMode;
20+
use crate::{ClientOptions, Envelope, Hub, Integration, Scope, Transport};
1721

1822
impl<T: Into<ClientOptions>> From<T> for Client {
1923
fn from(o: T) -> Client {
@@ -43,6 +47,7 @@ pub(crate) type TransportArc = Arc<RwLock<Option<Arc<dyn Transport>>>>;
4347
pub struct Client {
4448
options: ClientOptions,
4549
transport: TransportArc,
50+
#[cfg(feature = "release-health")]
4651
session_flusher: RwLock<Option<SessionFlusher>>,
4752
integrations: Vec<(TypeId, Arc<dyn Integration>)>,
4853
pub(crate) sdk_info: ClientSdkInfo,
@@ -60,13 +65,17 @@ impl fmt::Debug for Client {
6065
impl Clone for Client {
6166
fn clone(&self) -> Client {
6267
let transport = Arc::new(RwLock::new(self.transport.read().unwrap().clone()));
68+
69+
#[cfg(feature = "release-health")]
6370
let session_flusher = RwLock::new(Some(SessionFlusher::new(
6471
transport.clone(),
6572
self.options.session_mode,
6673
)));
74+
6775
Client {
6876
options: self.options.clone(),
6977
transport,
78+
#[cfg(feature = "release-health")]
7079
session_flusher,
7180
integrations: self.integrations.clone(),
7281
sdk_info: self.sdk_info.clone(),
@@ -131,6 +140,7 @@ impl Client {
131140
sdk_info.integrations.push(integration.name().to_string());
132141
}
133142

143+
#[cfg(feature = "release-health")]
134144
let session_flusher = RwLock::new(Some(SessionFlusher::new(
135145
transport.clone(),
136146
options.session_mode,
@@ -139,6 +149,7 @@ impl Client {
139149
Client {
140150
options,
141151
transport,
152+
#[cfg(feature = "release-health")]
142153
session_flusher,
143154
integrations,
144155
sdk_info,
@@ -266,6 +277,7 @@ impl Client {
266277
let mut envelope: Envelope = event.into();
267278
// For request-mode sessions, we aggregate them all instead of
268279
// flushing them out early.
280+
#[cfg(feature = "release-health")]
269281
if self.options.session_mode == SessionMode::Application {
270282
let session_item = scope.and_then(|scope| {
271283
scope
@@ -300,6 +312,7 @@ impl Client {
300312
}
301313
}
302314

315+
#[cfg(feature = "release-health")]
303316
pub(crate) fn enqueue_session(&self, session_update: SessionUpdate<'static>) {
304317
if let Some(ref flusher) = *self.session_flusher.read().unwrap() {
305318
flusher.enqueue(session_update);
@@ -308,6 +321,7 @@ impl Client {
308321

309322
/// Drains all pending events without shutting down.
310323
pub fn flush(&self, timeout: Option<Duration>) -> bool {
324+
#[cfg(feature = "release-health")]
311325
if let Some(ref flusher) = *self.session_flusher.read().unwrap() {
312326
flusher.flush();
313327
}
@@ -326,6 +340,7 @@ impl Client {
326340
/// If no timeout is provided the client will wait for as long a
327341
/// `shutdown_timeout` in the client options.
328342
pub fn close(&self, timeout: Option<Duration>) -> bool {
343+
#[cfg(feature = "release-health")]
329344
drop(self.session_flusher.write().unwrap().take());
330345
let transport_opt = self.transport.write().unwrap().take();
331346
if let Some(transport) = transport_opt {

sentry-core/src/clientoptions.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ pub type BeforeCallback<T> = Arc<dyn Fn(T) -> Option<T> + Send + Sync>;
3232
///
3333
/// See the [Documentation on Session Modes](https://develop.sentry.dev/sdk/sessions/#sdk-considerations)
3434
/// for more information.
35+
///
36+
/// **NOTE**: The `release-health` feature (enabled by default) needs to be enabled for this option to have
37+
/// any effect.
3538
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
3639
pub enum SessionMode {
3740
/// Long running application session.
@@ -171,8 +174,14 @@ pub struct ClientOptions {
171174
/// When automatic session tracking is enabled, a new "user-mode" session
172175
/// is started at the time of `sentry::init`, and will persist for the
173176
/// application lifetime.
177+
///
178+
/// **NOTE**: The `release-health` feature (enabled by default) needs to be enabled for this option to have
179+
/// any effect.
174180
pub auto_session_tracking: bool,
175181
/// Determine how Sessions are being tracked.
182+
///
183+
/// **NOTE**: The `release-health` feature (enabled by default) needs to be enabled for this option to have
184+
/// any effect.
176185
pub session_mode: SessionMode,
177186
/// Border frames which indicate a border from a backtrace to
178187
/// useless internals. Some are automatically included.

sentry-core/src/hub.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ impl Hub {
126126
///
127127
/// See the global [`start_session`](fn.start_session.html)
128128
/// for more documentation.
129+
#[cfg(feature = "release-health")]
129130
pub fn start_session(&self) {
130131
with_client_impl! {{
131132
self.inner.with_mut(|stack| {
@@ -143,6 +144,7 @@ impl Hub {
143144
/// End the current Release Health Session.
144145
///
145146
/// See the global [`sentry::end_session`](crate::end_session) for more documentation.
147+
#[cfg(feature = "release-health")]
146148
pub fn end_session(&self) {
147149
self.end_session_with_status(SessionStatus::Exited)
148150
}
@@ -151,6 +153,7 @@ impl Hub {
151153
///
152154
/// See the global [`end_session_with_status`](crate::end_session_with_status)
153155
/// for more documentation.
156+
#[cfg(feature = "release-health")]
154157
pub fn end_session_with_status(&self, status: SessionStatus) {
155158
with_client_impl! {{
156159
self.inner.with_mut(|stack| {

sentry-core/src/scope/real.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
use std::borrow::Cow;
22
use std::collections::{HashMap, VecDeque};
33
use std::fmt;
4-
use std::sync::{Arc, Mutex, PoisonError, RwLock};
4+
#[cfg(feature = "release-health")]
5+
use std::sync::Mutex;
6+
use std::sync::{Arc, PoisonError, RwLock};
57

68
use crate::performance::TransactionOrSpan;
79
use crate::protocol::{Attachment, Breadcrumb, Context, Event, Level, Transaction, User, Value};
10+
#[cfg(feature = "release-health")]
811
use crate::session::Session;
912
use crate::Client;
1013

@@ -45,14 +48,16 @@ pub struct Scope {
4548
pub(crate) tags: Arc<HashMap<String, String>>,
4649
pub(crate) contexts: Arc<HashMap<String, Context>>,
4750
pub(crate) event_processors: Arc<Vec<EventProcessor>>,
51+
#[cfg(feature = "release-health")]
4852
pub(crate) session: Arc<Mutex<Option<Session>>>,
4953
pub(crate) span: Arc<Option<TransactionOrSpan>>,
5054
pub(crate) attachments: Arc<Vec<Attachment>>,
5155
}
5256

5357
impl fmt::Debug for Scope {
5458
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55-
f.debug_struct("Scope")
59+
let mut debug_struct = f.debug_struct("Scope");
60+
debug_struct
5661
.field("level", &self.level)
5762
.field("fingerprint", &self.fingerprint)
5863
.field("transaction", &self.transaction)
@@ -61,8 +66,12 @@ impl fmt::Debug for Scope {
6166
.field("extra", &self.extra)
6267
.field("tags", &self.tags)
6368
.field("contexts", &self.contexts)
64-
.field("event_processors", &self.event_processors.len())
65-
.field("session", &self.session)
69+
.field("event_processors", &self.event_processors.len());
70+
71+
#[cfg(feature = "release-health")]
72+
debug_struct.field("session", &self.session);
73+
74+
debug_struct
6675
.field("span", &self.span)
6776
.field("attachments", &self.attachments.len())
6877
.finish()
@@ -341,7 +350,9 @@ impl Scope {
341350
self.span.as_ref().clone()
342351
}
343352

353+
#[allow(unused_variables)]
344354
pub(crate) fn update_session_from_event(&self, event: &Event<'static>) {
355+
#[cfg(feature = "release-health")]
345356
if let Some(session) = self.session.lock().unwrap().as_mut() {
346357
session.update_from_event(event);
347358
}

0 commit comments

Comments
 (0)