|
| 1 | +import contextlib |
1 | 2 | import dataclasses
|
2 | 3 | import functools
|
| 4 | +from unittest import mock |
3 | 5 |
|
4 | 6 | import pytest
|
5 | 7 | from snuba_sdk import Entity, Join
|
|
21 | 23 |
|
22 | 24 |
|
23 | 25 | @pytest.fixture(autouse=True)
|
24 |
| -def control_metrics_access(monkeypatch, request, set_sentry_option): |
| 26 | +def control_metrics_access(request, set_sentry_option): |
25 | 27 | from snuba_sdk import MetricsQuery
|
26 | 28 |
|
27 | 29 | from sentry.sentry_metrics import indexer
|
28 | 30 | from sentry.sentry_metrics.indexer.mock import MockIndexer
|
29 | 31 | from sentry.snuba import tasks
|
30 | 32 | from sentry.utils import snuba
|
31 | 33 |
|
32 |
| - if "sentry_metrics" in {mark.name for mark in request.node.iter_markers()}: |
33 |
| - mock_indexer = MockIndexer() |
34 |
| - monkeypatch.setattr("sentry.sentry_metrics.indexer.backend", mock_indexer) |
35 |
| - monkeypatch.setattr("sentry.sentry_metrics.indexer.bulk_record", mock_indexer.bulk_record) |
36 |
| - monkeypatch.setattr("sentry.sentry_metrics.indexer.record", mock_indexer.record) |
37 |
| - monkeypatch.setattr("sentry.sentry_metrics.indexer.resolve", mock_indexer.resolve) |
38 |
| - monkeypatch.setattr( |
39 |
| - "sentry.sentry_metrics.indexer.reverse_resolve", mock_indexer.reverse_resolve |
40 |
| - ) |
41 |
| - monkeypatch.setattr( |
42 |
| - "sentry.sentry_metrics.indexer.bulk_reverse_resolve", mock_indexer.bulk_reverse_resolve |
43 |
| - ) |
44 |
| - |
45 |
| - old_resolve = indexer.resolve |
46 |
| - |
47 |
| - def new_resolve(use_case_id, org_id, string): |
48 |
| - if ( |
49 |
| - use_case_id == UseCaseID.TRANSACTIONS |
50 |
| - and string in STRINGS_THAT_LOOK_LIKE_TAG_VALUES |
51 |
| - ): |
52 |
| - pytest.fail( |
53 |
| - f"stop right there, thief! you're about to resolve the string {string!r}. that looks like a tag value, but in this test mode, tag values are stored in clickhouse. the indexer might not have the value!" |
| 34 | + with contextlib.ExitStack() as ctx: |
| 35 | + if "sentry_metrics" in {mark.name for mark in request.node.iter_markers()}: |
| 36 | + mock_indexer = MockIndexer() |
| 37 | + |
| 38 | + ctx.enter_context( |
| 39 | + mock.patch.multiple( |
| 40 | + indexer, |
| 41 | + backend=mock_indexer, |
| 42 | + bulk_record=mock_indexer.bulk_record, |
| 43 | + record=mock_indexer.record, |
| 44 | + resolve=mock_indexer.resolve, |
| 45 | + reverse_resolve=mock_indexer.reverse_resolve, |
| 46 | + bulk_reverse_resolve=mock_indexer.bulk_reverse_resolve, |
54 | 47 | )
|
55 |
| - return old_resolve(use_case_id, org_id, string) |
56 |
| - |
57 |
| - monkeypatch.setattr(indexer, "resolve", new_resolve) |
| 48 | + ) |
58 | 49 |
|
59 |
| - old_build_results = snuba._apply_cache_and_build_results |
| 50 | + old_resolve = indexer.resolve |
| 51 | + |
| 52 | + def new_resolve(use_case_id, org_id, string): |
| 53 | + if ( |
| 54 | + use_case_id == UseCaseID.TRANSACTIONS |
| 55 | + and string in STRINGS_THAT_LOOK_LIKE_TAG_VALUES |
| 56 | + ): |
| 57 | + pytest.fail( |
| 58 | + f"stop right there, thief! you're about to resolve the string {string!r}. that looks like a tag value, but in this test mode, tag values are stored in clickhouse. the indexer might not have the value!" |
| 59 | + ) |
| 60 | + return old_resolve(use_case_id, org_id, string) |
| 61 | + |
| 62 | + ctx.enter_context(mock.patch.object(indexer, "resolve", new_resolve)) |
| 63 | + |
| 64 | + old_build_results = snuba._apply_cache_and_build_results |
| 65 | + |
| 66 | + def new_build_results(*args, **kwargs): |
| 67 | + if isinstance(args[0][0].request, dict): |
| 68 | + # We only support snql queries, and metrics only go through snql |
| 69 | + return old_build_results(*args, **kwargs) |
| 70 | + query = args[0][0].request.query |
| 71 | + is_performance_metrics = False |
| 72 | + is_metrics = False |
| 73 | + if not isinstance(query, MetricsQuery) and not isinstance(query.match, Join): |
| 74 | + is_performance_metrics = query.match.name.startswith("generic") |
| 75 | + is_metrics = "metrics" in query.match.name |
| 76 | + |
| 77 | + if is_performance_metrics: |
| 78 | + _validate_query(query, True) |
| 79 | + elif is_metrics: |
| 80 | + _validate_query(query, False) |
60 | 81 |
|
61 |
| - def new_build_results(*args, **kwargs): |
62 |
| - if isinstance(args[0][0].request, dict): |
63 |
| - # We only support snql queries, and metrics only go through snql |
64 | 82 | return old_build_results(*args, **kwargs)
|
65 |
| - query = args[0][0].request.query |
66 |
| - is_performance_metrics = False |
67 |
| - is_metrics = False |
68 |
| - if not isinstance(query, MetricsQuery) and not isinstance(query.match, Join): |
69 |
| - is_performance_metrics = query.match.name.startswith("generic") |
70 |
| - is_metrics = "metrics" in query.match.name |
71 |
| - |
72 |
| - if is_performance_metrics: |
73 |
| - _validate_query(query, True) |
74 |
| - elif is_metrics: |
75 |
| - _validate_query(query, False) |
76 |
| - |
77 |
| - return old_build_results(*args, **kwargs) |
78 |
| - |
79 |
| - monkeypatch.setattr(snuba, "_apply_cache_and_build_results", new_build_results) |
80 |
| - |
81 |
| - old_create_snql_in_snuba = tasks._create_snql_in_snuba |
82 |
| - |
83 |
| - def new_create_snql_in_snuba(subscription, snuba_query, snql_query, entity_subscription): |
84 |
| - query = snql_query.query |
85 |
| - is_performance_metrics = False |
86 |
| - is_metrics = False |
87 |
| - if isinstance(query.match, Entity): |
88 |
| - is_performance_metrics = query.match.name.startswith("generic") |
89 |
| - is_metrics = "metrics" in query.match.name |
90 |
| - |
91 |
| - if is_performance_metrics: |
92 |
| - _validate_query(query, True) |
93 |
| - elif is_metrics: |
94 |
| - _validate_query(query, False) |
95 |
| - |
96 |
| - return old_create_snql_in_snuba( |
97 |
| - subscription, snuba_query, snql_query, entity_subscription |
| 83 | + |
| 84 | + ctx.enter_context( |
| 85 | + mock.patch.object(snuba, "_apply_cache_and_build_results", new_build_results) |
98 | 86 | )
|
99 | 87 |
|
100 |
| - monkeypatch.setattr(tasks, "_create_snql_in_snuba", new_create_snql_in_snuba) |
101 |
| - yield |
102 |
| - else: |
103 |
| - should_fail = False |
| 88 | + old_create_snql_in_snuba = tasks._create_snql_in_snuba |
104 | 89 |
|
105 |
| - def fail(old_fn, *args, **kwargs): |
106 |
| - nonlocal should_fail |
107 |
| - should_fail = True |
108 |
| - return old_fn(*args, **kwargs) |
| 90 | + def new_create_snql_in_snuba( |
| 91 | + subscription, snuba_query, snql_query, entity_subscription |
| 92 | + ): |
| 93 | + query = snql_query.query |
| 94 | + is_performance_metrics = False |
| 95 | + is_metrics = False |
| 96 | + if isinstance(query.match, Entity): |
| 97 | + is_performance_metrics = query.match.name.startswith("generic") |
| 98 | + is_metrics = "metrics" in query.match.name |
| 99 | + |
| 100 | + if is_performance_metrics: |
| 101 | + _validate_query(query, True) |
| 102 | + elif is_metrics: |
| 103 | + _validate_query(query, False) |
| 104 | + |
| 105 | + return old_create_snql_in_snuba( |
| 106 | + subscription, snuba_query, snql_query, entity_subscription |
| 107 | + ) |
109 | 108 |
|
110 |
| - monkeypatch.setattr(indexer, "resolve", functools.partial(fail, indexer.resolve)) |
111 |
| - monkeypatch.setattr(indexer, "bulk_record", functools.partial(fail, indexer.bulk_record)) |
| 109 | + ctx.enter_context( |
| 110 | + mock.patch.object(tasks, "_create_snql_in_snuba", new_create_snql_in_snuba) |
| 111 | + ) |
| 112 | + yield |
| 113 | + else: |
| 114 | + should_fail = False |
112 | 115 |
|
113 |
| - yield |
| 116 | + def fail(old_fn, *args, **kwargs): |
| 117 | + nonlocal should_fail |
| 118 | + should_fail = True |
| 119 | + return old_fn(*args, **kwargs) |
114 | 120 |
|
115 |
| - if should_fail: |
116 |
| - pytest.fail( |
117 |
| - "Your test accesses sentry metrics without declaring it in " |
118 |
| - "metadata. Add this to your testfile:\n\n" |
119 |
| - "pytestmark = pytest.mark.sentry_metrics" |
| 121 | + ctx.enter_context( |
| 122 | + mock.patch.object(indexer, "resolve", functools.partial(fail, indexer.resolve)) |
| 123 | + ) |
| 124 | + ctx.enter_context( |
| 125 | + mock.patch.object( |
| 126 | + indexer, "bulk_record", functools.partial(fail, indexer.bulk_record) |
| 127 | + ) |
120 | 128 | )
|
121 | 129 |
|
| 130 | + yield |
| 131 | + |
| 132 | + if should_fail: |
| 133 | + pytest.fail( |
| 134 | + "Your test accesses sentry metrics without declaring it in " |
| 135 | + "metadata. Add this to your testfile:\n\n" |
| 136 | + "pytestmark = pytest.mark.sentry_metrics" |
| 137 | + ) |
| 138 | + |
122 | 139 |
|
123 | 140 | def _validate_query(query, tag_values_are_strings):
|
124 | 141 | def _walk(node):
|
|
0 commit comments