Skip to content

Commit eee728c

Browse files
authored
feat(ddm): Enable metrics related settings by default (#2685)
1 parent e373e35 commit eee728c

File tree

5 files changed

+79
-19
lines changed

5 files changed

+79
-19
lines changed

sentry_sdk/client.py

+14-9
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
get_default_release,
1616
handle_in_app,
1717
logger,
18+
is_gevent,
1819
)
1920
from sentry_sdk.serializer import serialize
2021
from sentry_sdk.tracing import trace, has_tracing_enabled
@@ -249,15 +250,19 @@ def _capture_envelope(envelope):
249250

250251
self.metrics_aggregator = None # type: Optional[MetricsAggregator]
251252
experiments = self.options.get("_experiments", {})
252-
if experiments.get("enable_metrics"):
253-
from sentry_sdk.metrics import MetricsAggregator
254-
255-
self.metrics_aggregator = MetricsAggregator(
256-
capture_func=_capture_envelope,
257-
enable_code_locations=bool(
258-
experiments.get("metric_code_locations")
259-
),
260-
)
253+
if experiments.get("enable_metrics", True):
254+
if is_gevent():
255+
logger.warning("Metrics currently not supported with gevent.")
256+
257+
else:
258+
from sentry_sdk.metrics import MetricsAggregator
259+
260+
self.metrics_aggregator = MetricsAggregator(
261+
capture_func=_capture_envelope,
262+
enable_code_locations=bool(
263+
experiments.get("metric_code_locations", True)
264+
),
265+
)
261266

262267
max_request_body_size = ("always", "never", "small", "medium")
263268
if self.options["max_request_body_size"] not in max_request_body_size:

sentry_sdk/metrics.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,11 @@ def _get_aggregator_and_update_tags(key, tags):
719719
if transaction_name:
720720
updated_tags.setdefault("transaction", transaction_name)
721721
if scope._span is not None:
722-
sample_rate = experiments.get("metrics_summary_sample_rate") or 0.0
722+
sample_rate = experiments.get("metrics_summary_sample_rate")
723+
# We default the sample rate of metrics summaries to 1.0 only when the sample rate is `None` since we
724+
# want to honor the user's decision if they pass a valid float.
725+
if sample_rate is None:
726+
sample_rate = 1.0
723727
should_summarize_metric_callback = experiments.get(
724728
"should_summarize_metric"
725729
)

sentry_sdk/utils.py

+15
Original file line numberDiff line numberDiff line change
@@ -1741,3 +1741,18 @@ def now():
17411741
def now():
17421742
# type: () -> float
17431743
return time.perf_counter()
1744+
1745+
1746+
try:
1747+
from gevent.monkey import is_module_patched
1748+
except ImportError:
1749+
1750+
def is_module_patched(*args, **kwargs):
1751+
# type: (*Any, **Any) -> bool
1752+
# unable to import from gevent means no modules have been patched
1753+
return False
1754+
1755+
1756+
def is_gevent():
1757+
# type: () -> bool
1758+
return is_module_patched("threading") or is_module_patched("_thread")

tests/test_metrics.py

+44-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# coding: utf-8
2-
2+
import pytest
33
import sys
44
import time
55
import linecache
@@ -13,6 +13,13 @@
1313
except ImportError:
1414
import mock # python < 3.3
1515

16+
try:
17+
import gevent
18+
except ImportError:
19+
gevent = None
20+
21+
requires_gevent = pytest.mark.skipif(gevent is None, reason="gevent not enabled")
22+
1623

1724
def parse_metrics(bytes):
1825
rv = []
@@ -418,7 +425,7 @@ def test_gauge(sentry_init, capture_envelopes):
418425
sentry_init(
419426
release="[email protected]",
420427
environment="not-fun-env",
421-
_experiments={"enable_metrics": True},
428+
_experiments={"enable_metrics": True, "metric_code_locations": False},
422429
)
423430
ts = time.time()
424431
envelopes = capture_envelopes()
@@ -450,7 +457,7 @@ def test_multiple(sentry_init, capture_envelopes):
450457
sentry_init(
451458
release="[email protected]",
452459
environment="not-fun-env",
453-
_experiments={"enable_metrics": True},
460+
_experiments={"enable_metrics": True, "metric_code_locations": False},
454461
)
455462
ts = time.time()
456463
envelopes = capture_envelopes()
@@ -503,7 +510,7 @@ def test_transaction_name(sentry_init, capture_envelopes):
503510
sentry_init(
504511
release="[email protected]",
505512
environment="not-fun-env",
506-
_experiments={"enable_metrics": True},
513+
_experiments={"enable_metrics": True, "metric_code_locations": False},
507514
)
508515
ts = time.time()
509516
envelopes = capture_envelopes()
@@ -536,12 +543,16 @@ def test_transaction_name(sentry_init, capture_envelopes):
536543
}
537544

538545

539-
def test_metric_summaries(sentry_init, capture_envelopes):
546+
@pytest.mark.parametrize("sample_rate", [1.0, None])
547+
def test_metric_summaries(sentry_init, capture_envelopes, sample_rate):
540548
sentry_init(
541549
release="[email protected]",
542550
environment="not-fun-env",
543551
enable_tracing=True,
544-
_experiments={"enable_metrics": True, "metrics_summary_sample_rate": 1.0},
552+
_experiments={
553+
"enable_metrics": True,
554+
"metrics_summary_sample_rate": sample_rate,
555+
},
545556
)
546557
ts = time.time()
547558
envelopes = capture_envelopes()
@@ -644,7 +655,7 @@ def test_metrics_summary_disabled(sentry_init, capture_envelopes):
644655
release="[email protected]",
645656
environment="not-fun-env",
646657
enable_tracing=True,
647-
_experiments={"enable_metrics": True},
658+
_experiments={"enable_metrics": True, "metrics_summary_sample_rate": 0.0},
648659
)
649660
ts = time.time()
650661
envelopes = capture_envelopes()
@@ -750,7 +761,7 @@ def test_tag_normalization(sentry_init, capture_envelopes):
750761
sentry_init(
751762
release="[email protected]",
752763
environment="not-fun-env",
753-
_experiments={"enable_metrics": True},
764+
_experiments={"enable_metrics": True, "metric_code_locations": False},
754765
)
755766
ts = time.time()
756767
envelopes = capture_envelopes()
@@ -805,6 +816,7 @@ def before_emit(key, tags):
805816
environment="not-fun-env",
806817
_experiments={
807818
"enable_metrics": True,
819+
"metric_code_locations": False,
808820
"before_emit_metric": before_emit,
809821
},
810822
)
@@ -850,7 +862,7 @@ def test_tag_serialization(sentry_init, capture_envelopes):
850862
sentry_init(
851863
release="fun-release",
852864
environment="not-fun-env",
853-
_experiments={"enable_metrics": True},
865+
_experiments={"enable_metrics": True, "metric_code_locations": False},
854866
)
855867
envelopes = capture_envelopes()
856868

@@ -942,3 +954,26 @@ def bad_capture_envelope(*args, **kwargs):
942954
m = parse_metrics(envelope.items[0].payload.get_bytes())
943955
assert len(m) == 1
944956
assert m[0][1] == "counter@none"
957+
958+
959+
@pytest.mark.forked
960+
@requires_gevent
961+
def test_no_metrics_with_gevent(sentry_init, capture_envelopes):
962+
from gevent import monkey
963+
964+
monkey.patch_all()
965+
966+
sentry_init(
967+
release="fun-release",
968+
environment="not-fun-env",
969+
_experiments={"enable_metrics": True, "metric_code_locations": True},
970+
)
971+
ts = time.time()
972+
envelopes = capture_envelopes()
973+
974+
metrics.incr("foobar", 1.0, tags={"foo": "bar", "blub": "blah"}, timestamp=ts)
975+
metrics.incr("foobar", 2.0, tags={"foo": "bar", "blub": "blah"}, timestamp=ts)
976+
Hub.current.flush()
977+
978+
assert Hub.current.client.metrics_aggregator is None
979+
assert len(envelopes) == 0

tests/test_profiler.py

+1
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ def test_minimum_unique_samples_required(
282282
assert reports == [("insufficient_data", "profile")]
283283

284284

285+
@pytest.mark.forked
285286
@requires_python_version(3, 3)
286287
def test_profile_captured(
287288
sentry_init,

0 commit comments

Comments
 (0)