|
| 1 | +import concurrent.futures as cf |
| 2 | +import sys |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +import sentry_sdk |
| 7 | +from sentry_sdk.integrations.featureflags import ( |
| 8 | + FeatureFlagsIntegration, |
| 9 | + add_feature_flag, |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +def test_featureflags_integration(sentry_init, capture_events, uninstall_integration): |
| 14 | + uninstall_integration(FeatureFlagsIntegration.identifier) |
| 15 | + sentry_init(integrations=[FeatureFlagsIntegration()]) |
| 16 | + |
| 17 | + add_feature_flag("hello", False) |
| 18 | + add_feature_flag("world", True) |
| 19 | + add_feature_flag("other", False) |
| 20 | + |
| 21 | + events = capture_events() |
| 22 | + sentry_sdk.capture_exception(Exception("something wrong!")) |
| 23 | + |
| 24 | + assert len(events) == 1 |
| 25 | + assert events[0]["contexts"]["flags"] == { |
| 26 | + "values": [ |
| 27 | + {"flag": "hello", "result": False}, |
| 28 | + {"flag": "world", "result": True}, |
| 29 | + {"flag": "other", "result": False}, |
| 30 | + ] |
| 31 | + } |
| 32 | + |
| 33 | + |
| 34 | +def test_featureflags_integration_threaded( |
| 35 | + sentry_init, capture_events, uninstall_integration |
| 36 | +): |
| 37 | + uninstall_integration(FeatureFlagsIntegration.identifier) |
| 38 | + sentry_init(integrations=[FeatureFlagsIntegration()]) |
| 39 | + events = capture_events() |
| 40 | + |
| 41 | + # Capture an eval before we split isolation scopes. |
| 42 | + add_feature_flag("hello", False) |
| 43 | + |
| 44 | + def task(flag_key): |
| 45 | + # Creates a new isolation scope for the thread. |
| 46 | + # This means the evaluations in each task are captured separately. |
| 47 | + with sentry_sdk.isolation_scope(): |
| 48 | + add_feature_flag(flag_key, False) |
| 49 | + # use a tag to identify to identify events later on |
| 50 | + sentry_sdk.set_tag("task_id", flag_key) |
| 51 | + sentry_sdk.capture_exception(Exception("something wrong!")) |
| 52 | + |
| 53 | + # Run tasks in separate threads |
| 54 | + with cf.ThreadPoolExecutor(max_workers=2) as pool: |
| 55 | + pool.map(task, ["world", "other"]) |
| 56 | + |
| 57 | + # Capture error in original scope |
| 58 | + sentry_sdk.set_tag("task_id", "0") |
| 59 | + sentry_sdk.capture_exception(Exception("something wrong!")) |
| 60 | + |
| 61 | + assert len(events) == 3 |
| 62 | + events.sort(key=lambda e: e["tags"]["task_id"]) |
| 63 | + |
| 64 | + assert events[0]["contexts"]["flags"] == { |
| 65 | + "values": [ |
| 66 | + {"flag": "hello", "result": False}, |
| 67 | + ] |
| 68 | + } |
| 69 | + assert events[1]["contexts"]["flags"] == { |
| 70 | + "values": [ |
| 71 | + {"flag": "hello", "result": False}, |
| 72 | + {"flag": "other", "result": False}, |
| 73 | + ] |
| 74 | + } |
| 75 | + assert events[2]["contexts"]["flags"] == { |
| 76 | + "values": [ |
| 77 | + {"flag": "hello", "result": False}, |
| 78 | + {"flag": "world", "result": False}, |
| 79 | + ] |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | +@pytest.mark.skipif(sys.version_info < (3, 7), reason="requires python3.7 or higher") |
| 84 | +def test_featureflags_integration_asyncio( |
| 85 | + sentry_init, capture_events, uninstall_integration |
| 86 | +): |
| 87 | + asyncio = pytest.importorskip("asyncio") |
| 88 | + |
| 89 | + uninstall_integration(FeatureFlagsIntegration.identifier) |
| 90 | + sentry_init(integrations=[FeatureFlagsIntegration()]) |
| 91 | + events = capture_events() |
| 92 | + |
| 93 | + # Capture an eval before we split isolation scopes. |
| 94 | + add_feature_flag("hello", False) |
| 95 | + |
| 96 | + async def task(flag_key): |
| 97 | + # Creates a new isolation scope for the thread. |
| 98 | + # This means the evaluations in each task are captured separately. |
| 99 | + with sentry_sdk.isolation_scope(): |
| 100 | + add_feature_flag(flag_key, False) |
| 101 | + # use a tag to identify to identify events later on |
| 102 | + sentry_sdk.set_tag("task_id", flag_key) |
| 103 | + sentry_sdk.capture_exception(Exception("something wrong!")) |
| 104 | + |
| 105 | + async def runner(): |
| 106 | + return asyncio.gather(task("world"), task("other")) |
| 107 | + |
| 108 | + asyncio.run(runner()) |
| 109 | + |
| 110 | + # Capture error in original scope |
| 111 | + sentry_sdk.set_tag("task_id", "0") |
| 112 | + sentry_sdk.capture_exception(Exception("something wrong!")) |
| 113 | + |
| 114 | + assert len(events) == 3 |
| 115 | + events.sort(key=lambda e: e["tags"]["task_id"]) |
| 116 | + |
| 117 | + assert events[0]["contexts"]["flags"] == { |
| 118 | + "values": [ |
| 119 | + {"flag": "hello", "result": False}, |
| 120 | + ] |
| 121 | + } |
| 122 | + assert events[1]["contexts"]["flags"] == { |
| 123 | + "values": [ |
| 124 | + {"flag": "hello", "result": False}, |
| 125 | + {"flag": "other", "result": False}, |
| 126 | + ] |
| 127 | + } |
| 128 | + assert events[2]["contexts"]["flags"] == { |
| 129 | + "values": [ |
| 130 | + {"flag": "hello", "result": False}, |
| 131 | + {"flag": "world", "result": False}, |
| 132 | + ] |
| 133 | + } |
0 commit comments