Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions sentry_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
get_before_send_log,
get_before_send_metric,
has_logs_enabled,
has_metrics_enabled,
)
from sentry_sdk.serializer import serialize
from sentry_sdk.tracing import trace
Expand Down Expand Up @@ -374,7 +375,9 @@ def _capture_envelope(envelope):

self.log_batcher = LogBatcher(capture_func=_capture_envelope)

self.metrics_batcher = MetricsBatcher(capture_func=_capture_envelope)
self.metrics_batcher = None
if has_metrics_enabled(self.options):
self.metrics_batcher = MetricsBatcher(capture_func=_capture_envelope)

max_request_body_size = ("always", "never", "small", "medium")
if self.options["max_request_body_size"] not in max_request_body_size:
Expand Down Expand Up @@ -975,7 +978,7 @@ def _capture_log(self, log):

def _capture_metric(self, metric):
# type: (Optional[Metric]) -> None
if metric is None:
if not has_metrics_enabled(self.options) or metric is None:
return

current_scope = sentry_sdk.get_current_scope()
Expand Down
1 change: 1 addition & 0 deletions sentry_sdk/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,7 @@ def __init__(
enable_logs=False, # type: bool
before_send_log=None, # type: Optional[Callable[[Log, Hint], Optional[Log]]]
trace_ignore_status_codes=frozenset(), # type: AbstractSet[int]
enable_metrics=True, # type: bool
):
# type: (...) -> None
"""Initialize the Sentry SDK with the given parameters. All parameters described here can be used in a call to `sentry_sdk.init()`.
Expand Down
8 changes: 8 additions & 0 deletions sentry_sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2047,6 +2047,14 @@ def get_before_send_log(options):
)


def has_metrics_enabled(options):
# type: (Optional[dict[str, Any]]) -> bool
if options is None:
return False

return bool(options.get("enable_metrics", True))

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Metrics toggle ignores experimental flag, breaking compatibility

The has_metrics_enabled function only checks the top-level enable_metrics option, ignoring the experimental _experiments.enable_metrics option. This breaks backward compatibility for users disabling metrics through the experimental flag.

Fix in Cursor Fix in Web

Copy link
Contributor Author

@alexander-alderman-webb alexander-alderman-webb Nov 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but I couldn't think of a good way to combine them because the experimental option defaulted to False and the stable option defaults to True.


def get_before_send_metric(options):
# type: (Optional[dict[str, Any]]) -> Optional[Callable[[Metric, Hint], Optional[Metric]]]
if options is None:
Expand Down
12 changes: 12 additions & 0 deletions tests/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ def envelopes_to_metrics(envelopes):
return res


def test_metrics_disabled(sentry_init, capture_envelopes):
sentry_init(enable_metrics=False)

envelopes = capture_envelopes()

sentry_sdk.metrics.count("test.counter", 1)
sentry_sdk.metrics.gauge("test.gauge", 42)
sentry_sdk.metrics.distribution("test.distribution", 200)

assert len(envelopes) == 0


def test_metrics_basics(sentry_init, capture_envelopes):
sentry_init()
envelopes = capture_envelopes()
Expand Down