Skip to content

Commit ab32750

Browse files
fix(metrics): Enable metrics by default (#1106)
Align runtime metrics capture with other SDKs while preserving explicit opt-out via ClientOptions::enable_metrics. Closes #1105 Closes [RUST-207](https://linear.app/getsentry/issue/RUST-207/default-enable-metrics-to-true)
1 parent ae8fe6c commit ab32750

3 files changed

Lines changed: 39 additions & 47 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
### Fixes
6+
7+
- Changed [`ClientOptions::enable_metrics`](https://docs.rs/sentry-core/latest/sentry_core/struct.ClientOptions.html#structfield.enable_metrics) to default to `true`, aligning metrics behavior with other Sentry SDKs ([#1106](https://github.com/getsentry/sentry-rust/issues/1106)). Metric capture APIs still require the `metrics` feature flag at compile time.
8+
39
## 0.48.0
410

511
### Breaking Changes

sentry-core/src/clientoptions.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,15 +174,16 @@ pub struct ClientOptions {
174174
/// This setting has no effect unless the `logs` feature is enabled at compile-time, as the
175175
/// feature is a pre-requisite to sending logs.
176176
pub enable_logs: bool,
177-
/// Determines whether captured metrics should be sent to Sentry (defaults to false).
177+
/// Determines whether metric capture APIs should capture metrics (defaults to true).
178178
///
179-
/// This setting has no effect unless the `metrics` feature is enabled at compile-time,
180-
/// as the feature is a prerequisite for sending metrics.
179+
/// Metric capture APIs are only available when the `metrics` feature is enabled at compile
180+
/// time. If the `metrics` feature is enabled, set this to `false` to prevent metrics from
181+
/// being captured and sent at runtime.
181182
pub enable_metrics: bool,
182183
/// Callback that is executed for each Metric before sending.
183184
///
184185
/// This setting has no effect unless the `metrics` feature is enabled at compile-time,
185-
/// as the feature is a prerequisite for sending metrics.
186+
/// as the feature is a prerequisite for capturing metrics.
186187
pub before_send_metric: Option<BeforeCallback<Metric>>,
187188
// Other options not documented in Unified API
188189
/// Disable SSL verification.
@@ -329,7 +330,7 @@ impl Default for ClientOptions {
329330
max_request_body_size: MaxRequestBodySize::Medium,
330331
enable_logs: true,
331332
before_send_log: None,
332-
enable_metrics: false,
333+
enable_metrics: true,
333334
before_send_metric: None,
334335
}
335336
}

sentry-core/tests/metrics.rs

Lines changed: 27 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,28 @@ fn sent_when_enabled() {
4848
} if name == "test"));
4949
}
5050

51+
/// Test that metrics are sent by default.
52+
#[test]
53+
fn metrics_enabled_by_default() {
54+
let options = ClientOptions::default();
55+
56+
let envelopes =
57+
test::with_captured_envelopes_options(|| metrics::counter("test", 1).capture(), options);
58+
assert_eq!(
59+
envelopes.len(),
60+
1,
61+
"expected exactly one envelope when metrics are enabled by default"
62+
)
63+
}
64+
5165
/// Test that metrics are disabled (not sent) when disabled in the
5266
/// [`ClientOptions`].
5367
#[test]
54-
fn metrics_disabled_by_default() {
55-
// Metrics are disabled by default.
56-
let options: ClientOptions = Default::default();
68+
fn metrics_disabled_when_configured() {
69+
let options = ClientOptions {
70+
enable_metrics: false,
71+
..Default::default()
72+
};
5773

5874
let envelopes =
5975
test::with_captured_envelopes_options(|| metrics::counter("test", 1).capture(), options);
@@ -67,10 +83,7 @@ fn metrics_disabled_by_default() {
6783
/// metrics enabled
6884
#[test]
6985
fn noop_sends_nothing() {
70-
let options = ClientOptions {
71-
enable_metrics: true,
72-
..Default::default()
73-
};
86+
let options = ClientOptions::default();
7487

7588
let envelopes = test::with_captured_envelopes_options(|| (), options);
7689

@@ -80,10 +93,7 @@ fn noop_sends_nothing() {
8093
/// Test that 100 metrics are sent in a single envelope.
8194
#[test]
8295
fn test_metrics_batching_at_limit() {
83-
let options = ClientOptions {
84-
enable_metrics: true,
85-
..Default::default()
86-
};
96+
let options = ClientOptions::default();
8797

8898
let envelopes = test::with_captured_envelopes_options(
8999
|| {
@@ -133,10 +143,7 @@ fn test_metrics_batching_at_limit() {
133143
/// Test that 101 envelopes are sent in two separate envelopes
134144
#[test]
135145
fn test_metrics_batching_over_limit() {
136-
let options = ClientOptions {
137-
enable_metrics: true,
138-
..Default::default()
139-
};
146+
let options = ClientOptions::default();
140147

141148
let mut envelopes = test::with_captured_envelopes_options(
142149
|| {
@@ -207,10 +214,7 @@ fn test_metrics_batching_over_limit() {
207214

208215
#[test]
209216
fn metric_attributes_are_captured() {
210-
let options = ClientOptions {
211-
enable_metrics: true,
212-
..Default::default()
213-
};
217+
let options = ClientOptions::default();
214218

215219
let envelopes = test::with_captured_envelopes_options(
216220
|| {
@@ -265,10 +269,7 @@ fn metric_attributes_are_captured() {
265269

266270
#[test]
267271
fn metric_unit_is_captured() {
268-
let options = ClientOptions {
269-
enable_metrics: true,
270-
..Default::default()
271-
};
272+
let options = ClientOptions::default();
272273

273274
let envelopes = test::with_captured_envelopes_options(
274275
|| metrics::gauge("test", 42).unit(Unit::Millisecond).capture(),
@@ -311,10 +312,7 @@ fn metric_unit_is_captured() {
311312
/// This tests that trace ID is set from the propagation context when there is no active span.
312313
#[test]
313314
fn metrics_share_trace_id_without_active_span() {
314-
let options = ClientOptions {
315-
enable_metrics: true,
316-
..Default::default()
317-
};
315+
let options = ClientOptions::default();
318316

319317
let envelopes = test::with_captured_envelopes_options(
320318
|| {
@@ -348,10 +346,7 @@ fn metrics_share_trace_id_without_active_span() {
348346
/// Test that span_id is set from the active span when one is present.
349347
#[test]
350348
fn metrics_span_id_from_active_span() {
351-
let options = ClientOptions {
352-
enable_metrics: true,
353-
..Default::default()
354-
};
349+
let options = ClientOptions::default();
355350

356351
let mut expected_span_id = None;
357352
let envelopes = test::with_captured_envelopes_options(
@@ -389,7 +384,6 @@ fn metrics_span_id_from_active_span() {
389384
#[test]
390385
fn default_attributes_attached() {
391386
let options = ClientOptions {
392-
enable_metrics: true,
393387
environment: Some("test-env".into()),
394388
release: Some("1.0.0".into()),
395389
server_name: Some("test-server".into()),
@@ -417,10 +411,7 @@ fn default_attributes_attached() {
417411
/// Test that optional default attributes are omitted when not configured.
418412
#[test]
419413
fn optional_default_attributes_omitted_when_not_configured() {
420-
let options = ClientOptions {
421-
enable_metrics: true,
422-
..Default::default()
423-
};
414+
let options = ClientOptions::default();
424415

425416
let envelopes =
426417
test::with_captured_envelopes_options(|| metrics::counter("test", 1).capture(), options);
@@ -442,7 +433,6 @@ fn optional_default_attributes_omitted_when_not_configured() {
442433
#[test]
443434
fn default_attributes_do_not_overwrite_explicit() {
444435
let options = ClientOptions {
445-
enable_metrics: true,
446436
environment: Some("default-env".into()),
447437
..Default::default()
448438
};
@@ -475,7 +465,6 @@ fn default_attributes_do_not_overwrite_explicit() {
475465
#[test]
476466
fn user_attributes_absent_without_send_default_pii() {
477467
let options = ClientOptions {
478-
enable_metrics: true,
479468
send_default_pii: false,
480469
..Default::default()
481470
};
@@ -513,7 +502,6 @@ fn user_attributes_absent_without_send_default_pii() {
513502
#[test]
514503
fn metric_user_attributes_from_scope_are_applied_with_send_default_pii() {
515504
let options = ClientOptions {
516-
enable_metrics: true,
517505
send_default_pii: true,
518506
..Default::default()
519507
};
@@ -553,7 +541,6 @@ fn metric_user_attributes_from_scope_are_applied_with_send_default_pii() {
553541
#[test]
554542
fn metric_user_attributes_do_not_overwrite_explicit() {
555543
let options = ClientOptions {
556-
enable_metrics: true,
557544
send_default_pii: true,
558545
..Default::default()
559546
};
@@ -594,7 +581,6 @@ fn metric_user_attributes_do_not_overwrite_explicit() {
594581
#[test]
595582
fn before_send_metric_can_drop() {
596583
let options = ClientOptions {
597-
enable_metrics: true,
598584
before_send_metric: Some(Arc::new(|_| None)),
599585
..Default::default()
600586
};
@@ -611,7 +597,6 @@ fn before_send_metric_can_drop() {
611597
#[test]
612598
fn before_send_metric_can_modify() {
613599
let options = ClientOptions {
614-
enable_metrics: true,
615600
before_send_metric: Some(Arc::new(|mut metric| {
616601
metric
617602
.attributes

0 commit comments

Comments
 (0)