99from sentry_sdk .tracing import TRANSACTION_SOURCE_ROUTE
1010from sentry_sdk .envelope import parse_json
1111
12+ try :
13+ import gevent
14+ except ImportError :
15+ gevent = None
16+
17+
18+ minimum_python_37_with_gevent = pytest .mark .skipif (
19+ gevent and sys .version_info < (3 , 7 ),
20+ reason = "Require Python 3.7 or higher with gevent" ,
21+ )
22+
1223
1324def parse_metrics (bytes ):
1425 rv = []
@@ -41,6 +52,7 @@ def parse_metrics(bytes):
4152 return rv
4253
4354
55+ @minimum_python_37_with_gevent
4456@pytest .mark .forked
4557def test_incr (sentry_init , capture_envelopes , maybe_monkeypatched_threading ):
4658 sentry_init (
@@ -93,6 +105,7 @@ def test_incr(sentry_init, capture_envelopes, maybe_monkeypatched_threading):
93105 }
94106
95107
108+ @minimum_python_37_with_gevent
96109@pytest .mark .forked
97110def test_timing (sentry_init , capture_envelopes , maybe_monkeypatched_threading ):
98111 sentry_init (
@@ -153,6 +166,7 @@ def test_timing(sentry_init, capture_envelopes, maybe_monkeypatched_threading):
153166 )
154167
155168
169+ @minimum_python_37_with_gevent
156170@pytest .mark .forked
157171def test_timing_decorator (
158172 sentry_init , capture_envelopes , maybe_monkeypatched_threading
@@ -248,6 +262,7 @@ def amazing_nano():
248262 assert line .strip () == "assert amazing() == 42"
249263
250264
265+ @minimum_python_37_with_gevent
251266@pytest .mark .forked
252267def test_timing_basic (sentry_init , capture_envelopes , maybe_monkeypatched_threading ):
253268 sentry_init (
@@ -302,6 +317,7 @@ def test_timing_basic(sentry_init, capture_envelopes, maybe_monkeypatched_thread
302317 }
303318
304319
320+ @minimum_python_37_with_gevent
305321@pytest .mark .forked
306322def test_distribution (sentry_init , capture_envelopes , maybe_monkeypatched_threading ):
307323 sentry_init (
@@ -364,6 +380,7 @@ def test_distribution(sentry_init, capture_envelopes, maybe_monkeypatched_thread
364380 )
365381
366382
383+ @minimum_python_37_with_gevent
367384@pytest .mark .forked
368385def test_set (sentry_init , capture_envelopes , maybe_monkeypatched_threading ):
369386 sentry_init (
@@ -417,6 +434,7 @@ def test_set(sentry_init, capture_envelopes, maybe_monkeypatched_threading):
417434 }
418435
419436
437+ @minimum_python_37_with_gevent
420438@pytest .mark .forked
421439def test_gauge (sentry_init , capture_envelopes , maybe_monkeypatched_threading ):
422440 sentry_init (
@@ -450,6 +468,7 @@ def test_gauge(sentry_init, capture_envelopes, maybe_monkeypatched_threading):
450468 }
451469
452470
471+ @minimum_python_37_with_gevent
453472@pytest .mark .forked
454473def test_multiple (sentry_init , capture_envelopes ):
455474 sentry_init (
@@ -504,6 +523,7 @@ def test_multiple(sentry_init, capture_envelopes):
504523 }
505524
506525
526+ @minimum_python_37_with_gevent
507527@pytest .mark .forked
508528def test_transaction_name (
509529 sentry_init , capture_envelopes , maybe_monkeypatched_threading
@@ -544,6 +564,7 @@ def test_transaction_name(
544564 }
545565
546566
567+ @minimum_python_37_with_gevent
547568@pytest .mark .forked
548569@pytest .mark .parametrize ("sample_rate" , [1.0 , None ])
549570def test_metric_summaries (
@@ -654,6 +675,7 @@ def test_metric_summaries(
654675 }
655676
656677
678+ @minimum_python_37_with_gevent
657679@pytest .mark .forked
658680def test_metrics_summary_disabled (
659681 sentry_init , capture_envelopes , maybe_monkeypatched_threading
@@ -698,6 +720,7 @@ def test_metrics_summary_disabled(
698720 assert "_metrics_summary" not in t ["spans" ][0 ]
699721
700722
723+ @minimum_python_37_with_gevent
701724@pytest .mark .forked
702725@pytest .mark .skip (reason = "Temporarily disable to release SDK 2.0a1." )
703726def test_metrics_summary_filtered (
@@ -768,6 +791,7 @@ def should_summarize_metric(key, tags):
768791 } in t ["d:foo@second" ]
769792
770793
794+ @minimum_python_37_with_gevent
771795@pytest .mark .forked
772796def test_tag_normalization (
773797 sentry_init , capture_envelopes , maybe_monkeypatched_threading
@@ -811,6 +835,7 @@ def test_tag_normalization(
811835 }
812836
813837
838+ @minimum_python_37_with_gevent
814839@pytest .mark .forked
815840def test_before_emit_metric (
816841 sentry_init , capture_envelopes , maybe_monkeypatched_threading
@@ -854,6 +879,7 @@ def before_emit(key, tags):
854879 }
855880
856881
882+ @minimum_python_37_with_gevent
857883@pytest .mark .forked
858884def test_aggregator_flush (
859885 sentry_init , capture_envelopes , maybe_monkeypatched_threading
@@ -874,6 +900,7 @@ def test_aggregator_flush(
874900 assert Hub .current .client .metrics_aggregator .buckets == {}
875901
876902
903+ @minimum_python_37_with_gevent
877904@pytest .mark .forked
878905def test_tag_serialization (
879906 sentry_init , capture_envelopes , maybe_monkeypatched_threading
@@ -914,6 +941,7 @@ def test_tag_serialization(
914941 }
915942
916943
944+ @minimum_python_37_with_gevent
917945@pytest .mark .forked
918946def test_flush_recursion_protection (
919947 sentry_init , capture_envelopes , monkeypatch , maybe_monkeypatched_threading
@@ -946,11 +974,12 @@ def bad_capture_envelope(*args, **kwargs):
946974 assert m [0 ][1 ] == "counter@none"
947975
948976
977+ @minimum_python_37_with_gevent
949978@pytest .mark .forked
950979def test_flush_recursion_protection_background_flush (
951980 sentry_init , capture_envelopes , monkeypatch , maybe_monkeypatched_threading
952981):
953- monkeypatch .setattr (metrics .MetricsAggregator , "FLUSHER_SLEEP_TIME" , 0.1 )
982+ monkeypatch .setattr (metrics .MetricsAggregator , "FLUSHER_SLEEP_TIME" , 0.01 )
954983 sentry_init (
955984 release = "fun-release" ,
956985 environment = "not-fun-env" ,
@@ -977,3 +1006,29 @@ def bad_capture_envelope(*args, **kwargs):
9771006 m = parse_metrics (envelope .items [0 ].payload .get_bytes ())
9781007 assert len (m ) == 1
9791008 assert m [0 ][1 ] == "counter@none"
1009+
1010+
1011+ @pytest .mark .skipif (
1012+ not gevent or sys .version_info >= (3 , 7 ),
1013+ reason = "Python 3.6 or lower and gevent required" ,
1014+ )
1015+ @pytest .mark .forked
1016+ def test_disable_metrics_for_old_python_with_gevent (
1017+ sentry_init , capture_envelopes , maybe_monkeypatched_threading
1018+ ):
1019+ if maybe_monkeypatched_threading != "greenlet" :
1020+ pytest .skip ("Test specifically for gevent/greenlet" )
1021+
1022+ sentry_init (
1023+ release = "fun-release" ,
1024+ environment = "not-fun-env" ,
1025+ _experiments = {"enable_metrics" : True },
1026+ )
1027+ envelopes = capture_envelopes ()
1028+
1029+ metrics .incr ("counter" )
1030+
1031+ Hub .current .flush ()
1032+
1033+ assert Hub .current .client .metrics_aggregator is None
1034+ assert not envelopes
0 commit comments