-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathtest_profile_chunks.py
237 lines (200 loc) · 6.89 KB
/
test_profile_chunks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import uuid
from copy import deepcopy
from pathlib import Path
import pytest
from sentry_sdk.envelope import Envelope, Item, PayloadRef
from sentry_relay.consts import DataCategory
from .asserts import time_within_delta
RELAY_ROOT = Path(__file__).parent.parent.parent
@pytest.mark.parametrize("num_intermediate_relays", [0, 1, 2])
def test_profile_chunk_outcomes(
mini_sentry,
relay,
relay_with_processing,
outcomes_consumer,
profiles_consumer,
num_intermediate_relays,
):
"""
Tests that Relay reports correct outcomes for profile chunks.
Have a chain of many relays that eventually connect to Sentry
and verify that the outcomes sent by the first relay
are properly forwarded up to sentry.
"""
outcomes_consumer = outcomes_consumer(timeout=5)
profiles_consumer = profiles_consumer()
project_id = 42
project_config = mini_sentry.add_full_project_config(project_id)["config"]
project_config.setdefault("features", []).append(
"organizations:continuous-profiling"
)
config = {
"outcomes": {
"emit_outcomes": True,
"batch_size": 1,
"batch_interval": 1,
"aggregator": {
"bucket_interval": 1,
"flush_interval": 1,
},
"source": "processing-relay",
},
"aggregator": {
"bucket_interval": 1,
"initial_delay": 0,
},
}
# The innermost Relay needs to be in processing mode
upstream = relay_with_processing(config)
# build a chain of relays
for i in range(num_intermediate_relays):
config = deepcopy(config)
if i == 0:
# Emulate a PoP Relay
config["outcomes"]["source"] = "pop-relay"
if i == 1:
# Emulate a customer Relay
config["outcomes"]["source"] = "external-relay"
config["outcomes"]["emit_outcomes"] = "as_client_reports"
upstream = relay(upstream, config)
with open(
RELAY_ROOT / "relay-profiling/tests/fixtures/sample/v2/valid.json",
"rb",
) as f:
profile = f.read()
envelope = Envelope()
envelope.add_item(Item(payload=PayloadRef(bytes=profile), type="profile_chunk"))
upstream.send_envelope(project_id, envelope)
# No outcome is emitted in Relay since it's a successful ingestion.
# However, we will emit the outcome for PROFILE_DURATION in sentry.
outcomes_consumer.assert_empty()
assert profiles_consumer.get_profile()
def test_profile_chunk_outcomes_invalid(
mini_sentry,
relay_with_processing,
outcomes_consumer,
profiles_consumer,
):
"""
Tests that Relay reports correct outcomes for invalid profiles as `ProfileChunk`.
"""
outcomes_consumer = outcomes_consumer(timeout=2)
profiles_consumer = profiles_consumer()
project_id = 42
project_config = mini_sentry.add_full_project_config(project_id)["config"]
project_config.setdefault("features", []).append(
"organizations:continuous-profiling"
)
config = {
"outcomes": {
"emit_outcomes": True,
"batch_size": 1,
"batch_interval": 1,
"aggregator": {
"bucket_interval": 1,
"flush_interval": 1,
},
"source": "pop-relay",
},
"aggregator": {
"bucket_interval": 1,
"initial_delay": 0,
},
}
upstream = relay_with_processing(config)
envelope = Envelope()
payload = {
"chunk_id": "11111111111111111111111111111111",
"platform": "thisisnotvalid",
}
envelope.add_item(Item(payload=PayloadRef(json=payload), type="profile_chunk"))
upstream.send_envelope(project_id, envelope)
outcomes = outcomes_consumer.get_outcomes()
outcomes.sort(key=lambda o: sorted(o.items()))
assert outcomes == [
{
"category": DataCategory.PROFILE_CHUNK.value,
"timestamp": time_within_delta(),
"key_id": 123,
"org_id": 1,
"outcome": 3, # Invalid
"project_id": 42,
"quantity": 1,
"reason": "profiling_platform_not_supported",
"source": "pop-relay",
},
]
profiles_consumer.assert_empty()
def test_profile_chunk_outcomes_rate_limited(
mini_sentry,
relay_with_processing,
outcomes_consumer,
profiles_consumer,
):
"""
Tests that Relay reports correct outcomes when profile chunks are rate limited.
This test verifies that when a profile chunk hits a rate limit:
1. The profile chunk is dropped and not forwarded to the profiles consumer
2. A rate limited outcome is emitted with the correct category and reason code
3. The rate limit is enforced at the project level
"""
outcomes_consumer = outcomes_consumer(timeout=2)
profiles_consumer = profiles_consumer()
project_id = 42
project_config = mini_sentry.add_full_project_config(project_id)["config"]
# Enable profiling feature flag
project_config.setdefault("features", []).append(
"organizations:continuous-profiling"
)
# Configure rate limiting quota that blocks all profile chunks
project_config["quotas"] = [
{
"id": f"test_rate_limiting_{uuid.uuid4().hex}",
"categories": ["profile_chunk_ui"], # Target profile chunks specifically
"limit": 0, # Block all profile chunks
"reasonCode": "profile_chunks_exceeded",
}
]
config = {
"outcomes": {
"emit_outcomes": True,
"batch_size": 1,
"batch_interval": 1,
"aggregator": {
"bucket_interval": 1,
"flush_interval": 0,
},
},
"aggregator": {
"bucket_interval": 1,
"initial_delay": 0,
},
}
upstream = relay_with_processing(config)
# Load a valid profile chunk from test fixtures
with open(
RELAY_ROOT / "relay-profiling/tests/fixtures/sample/v2/valid.json",
"rb",
) as f:
profile = f.read()
# Create and send envelope containing the profile chunk
envelope = Envelope()
envelope.add_item(Item(payload=PayloadRef(bytes=profile), type="profile_chunk"))
upstream.send_envelope(project_id, envelope)
# Verify the rate limited outcome was emitted with correct properties
outcomes = outcomes_consumer.get_outcomes()
outcomes.sort(key=lambda o: sorted(o.items()))
assert outcomes == [
{
"category": DataCategory.PROFILE_CHUNK_UI.value,
"timestamp": time_within_delta(),
"key_id": 123,
"org_id": 1,
"outcome": 2, # RateLimited
"project_id": 42,
"quantity": 1,
"reason": "profile_chunks_exceeded",
},
]
# Verify no profiles were forwarded to the consumer
profiles_consumer.assert_empty()