/
test_rh_metrics_multiprocess_steps.py
587 lines (492 loc) · 18.4 KB
/
test_rh_metrics_multiprocess_steps.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
from __future__ import annotations
import logging
import pickle
import time
from collections.abc import MutableMapping, Sequence
from copy import deepcopy
from datetime import datetime, timezone
from typing import Any
from unittest.mock import Mock, call
import pytest
from arroyo.backends.kafka import KafkaPayload
from arroyo.dlq import InvalidMessage
from arroyo.processing.strategies import MessageRejected
from arroyo.types import BrokerValue, Message, Partition, Topic, Value
from sentry.ratelimits.cardinality import CardinalityLimiter
from sentry.sentry_metrics.configuration import IndexerStorage, UseCaseKey, get_ingest_config
from sentry.sentry_metrics.consumers.indexer.batch import valid_metric_name
from sentry.sentry_metrics.consumers.indexer.common import (
BatchMessages,
IndexerOutputMessageBatch,
MetricsBatchBuilder,
)
from sentry.sentry_metrics.consumers.indexer.processing import MessageProcessor
from sentry.sentry_metrics.indexer.limiters.cardinality import (
TimeseriesCardinalityLimiter,
cardinality_limiter_factory,
)
from sentry.sentry_metrics.indexer.mock import MockIndexer, RawSimpleIndexer
from sentry.sentry_metrics.use_case_id_registry import UseCaseID
from sentry.snuba.metrics.naming_layer.mri import SessionMRI
from sentry.utils import json
logger = logging.getLogger(__name__)
pytestmark = pytest.mark.sentry_metrics
MESSAGE_PROCESSOR = MessageProcessor(
get_ingest_config(UseCaseKey.RELEASE_HEALTH, IndexerStorage.POSTGRES)
)
BROKER_TIMESTAMP = datetime.now(tz=timezone.utc)
@pytest.fixture(autouse=True)
def update_sentry_settings(settings):
settings.SENTRY_METRICS_INDEXER_RAISE_VALIDATION_ERRORS = True
def compare_messages_ignoring_mapping_metadata(actual: Message, expected: Message) -> None:
assert actual.committable == expected.committable
actual_payload = actual.payload
expected_payload = expected.payload
if isinstance(actual_payload, InvalidMessage):
assert actual_payload == expected_payload
return
assert actual_payload.key == expected_payload.key
actual_deserialized = json.loads(actual_payload.value)
expected_deserialized = json.loads(expected_payload.value)
del actual_deserialized["mapping_meta"]
assert actual_deserialized == expected_deserialized
def compare_message_batches_ignoring_metadata(
actual: IndexerOutputMessageBatch, expected: Sequence[Message]
) -> None:
assert len(actual.data) == len(expected)
for a, e in zip(actual.data, expected):
compare_messages_ignoring_mapping_metadata(a, e)
def _batch_message_set_up(next_step: Mock, max_batch_time: float = 100.0, max_batch_size: int = 2):
# batch time is in seconds
batch_messages_step = BatchMessages(
next_step=next_step, max_batch_time=max_batch_time, max_batch_size=max_batch_size
)
message1 = Message(
BrokerValue(
KafkaPayload(None, b"some value", []),
Partition(Topic("topic"), 0),
1,
BROKER_TIMESTAMP,
)
)
message2 = Message(
BrokerValue(
KafkaPayload(None, b"another value", []),
Partition(Topic("topic"), 0),
2,
BROKER_TIMESTAMP,
)
)
return (batch_messages_step, message1, message2)
def test_batch_messages() -> None:
next_step = Mock()
batch_messages_step, message1, message2 = _batch_message_set_up(next_step)
# submit the first message, batch builder should should be created
# and the messaged added to the batch
batch_messages_step.submit(message=message1)
assert len(batch_messages_step._BatchMessages__batch) == 1
# neither batch_size or batch_time as been met so poll shouldn't
# do anything yet (aka shouldn't flush and call next_step.submit)
batch_messages_step.poll()
assert len(batch_messages_step._BatchMessages__batch) == 1
assert not next_step.submit.called
# submit the second message, message should be added to the batch
# which will now saturate the batch_size (2). This will trigger
# __flush which in turn calls next.submit and reset the batch to None
batch_messages_step.submit(message=message2)
assert next_step.submit.call_args == call(
Message(Value([message1, message2], message2.committable)),
)
assert batch_messages_step._BatchMessages__batch is None
def test_batch_messages_rejected_message():
next_step = Mock()
next_step.submit.side_effect = MessageRejected()
batch_messages_step, message1, message2 = _batch_message_set_up(next_step)
batch_messages_step.poll()
batch_messages_step.submit(message=message1)
# if we try to submit a batch when the next step is
# not ready to accept more messages we'll get a
# MessageRejected error. This will be reraised for
# to the stream processor on the subsequent call to submit
batch_messages_step.submit(message=message2)
with pytest.raises(MessageRejected):
batch_messages_step.submit(message=message2)
# when poll is called, we still try to flush the batch
# caust its full but we handled the MessageRejected error
batch_messages_step.poll()
assert next_step.submit.called
def test_batch_messages_join():
next_step = Mock()
batch_messages_step, message1, _ = _batch_message_set_up(next_step)
batch_messages_step.poll()
batch_messages_step.submit(message=message1)
# A rebalance, restart, scale up or any other event
# that causes partitions to be revoked will call join
batch_messages_step.join(timeout=3)
# we don't flush the batch
assert not next_step.submit.called
def test_metrics_batch_builder():
max_batch_time = 3.0 # seconds
max_batch_size = 2
# 1. Ready when max_batch_size is reached
batch_builder_size = MetricsBatchBuilder(
max_batch_size=max_batch_size, max_batch_time=max_batch_time
)
assert not batch_builder_size.ready()
message1 = Message(
BrokerValue(
KafkaPayload(None, b"some value", []), Partition(Topic("topic"), 0), 1, datetime.now()
)
)
batch_builder_size.append(message1)
assert not batch_builder_size.ready()
message2 = Message(
BrokerValue(
KafkaPayload(None, b"another value", []),
Partition(Topic("topic"), 0),
2,
datetime.now(),
)
)
batch_builder_size.append(message2)
assert batch_builder_size.ready()
# 2. Ready when max_batch_time is reached
batch_builder_time = MetricsBatchBuilder(
max_batch_size=max_batch_size, max_batch_time=max_batch_time
)
assert not batch_builder_time.ready()
message1 = Message(
BrokerValue(
KafkaPayload(None, b"some value", []), Partition(Topic("topic"), 0), 1, datetime.now()
)
)
batch_builder_time.append(message1)
assert not batch_builder_time.ready()
time.sleep(3)
assert batch_builder_time.ready()
# 3. Adding the same message twice to the same batch
batch_builder_time = MetricsBatchBuilder(
max_batch_size=max_batch_size, max_batch_time=max_batch_time
)
message1 = Message(
BrokerValue(
KafkaPayload(None, b"some value", []), Partition(Topic("topic"), 0), 1, datetime.now()
)
)
batch_builder_time.append(message1)
ts = int(datetime.now(tz=timezone.utc).timestamp())
counter_payload: dict[str, Any] = {
"name": SessionMRI.RAW_SESSION.value,
"tags": {
"environment": "production",
"session.status": "init",
},
"timestamp": ts,
"type": b"c",
"value": 1.0,
"org_id": 1,
"project_id": 3,
"retention_days": 90,
}
distribution_payload: dict[str, Any] = {
"name": SessionMRI.RAW_DURATION.value,
"tags": {
"environment": "production",
"session.status": "healthy",
},
"timestamp": ts,
"type": b"d",
"value": [4, 5, 6],
"org_id": 1,
"project_id": 3,
"retention_days": 90,
}
set_payload: dict[str, Any] = {
"name": SessionMRI.RAW_ERROR.value,
"tags": {
"environment": "production",
"session.status": "errored",
},
"timestamp": ts,
"type": b"s",
"value": [3],
"org_id": 1,
"project_id": 3,
"retention_days": 90,
}
def __translated_payload(
payload,
) -> dict[str, str | int | list[int] | MutableMapping[int, int]]:
"""
Translates strings to ints using the MockIndexer
in addition to adding the retention_days
"""
indexer = MockIndexer()
payload = deepcopy(payload)
org_id = payload["org_id"]
new_tags = {
indexer.resolve(UseCaseKey.RELEASE_HEALTH, org_id, k): indexer.resolve(
UseCaseKey.RELEASE_HEALTH, org_id, v
)
for k, v in payload["tags"].items()
}
payload["metric_id"] = indexer.resolve(UseCaseKey.RELEASE_HEALTH, org_id, payload["name"])
payload["retention_days"] = 90
payload["tags"] = new_tags
payload["use_case_id"] = "sessions"
payload["sentry_received_timestamp"] = BROKER_TIMESTAMP.timestamp()
payload.pop("unit", None)
del payload["name"]
return payload
@pytest.mark.django_db
def test_process_messages() -> None:
message_payloads = [counter_payload, distribution_payload, set_payload]
message_batch = [
Message(
BrokerValue(
KafkaPayload(None, json.dumps(payload).encode("utf-8"), []),
Partition(Topic("topic"), 0),
i + 1,
BROKER_TIMESTAMP,
)
)
for i, payload in enumerate(message_payloads)
]
# the outer message uses the last message's partition, offset, and timestamp
last = message_batch[-1]
outer_message = Message(Value(message_batch, last.committable))
new_batch = MESSAGE_PROCESSOR.process_messages(outer_message=outer_message)
expected_new_batch = []
for i, m in enumerate(message_batch):
assert isinstance(m.value, BrokerValue)
expected_new_batch.append(
Message(
BrokerValue(
KafkaPayload(
None,
json.dumps(__translated_payload(message_payloads[i])).encode("utf-8"),
[
("metric_type", message_payloads[i]["type"]),
],
),
m.value.partition,
m.value.offset,
m.value.timestamp,
)
)
)
compare_message_batches_ignoring_metadata(new_batch, expected_new_batch)
invalid_payloads = [
(
{
"name": SessionMRI.RAW_ERROR.value,
"tags": {
"environment": "production" * 21,
"session.status": "errored",
},
"timestamp": ts,
"type": "s",
"value": [3],
"org_id": 1,
"project_id": 3,
"retention_days": 90,
},
"invalid_tags",
True,
),
(
{
"name": SessionMRI.RAW_ERROR.value * 21,
"tags": {
"environment": "production",
"session.status": "errored",
},
"timestamp": ts,
"type": "s",
"value": [3],
"org_id": 1,
"project_id": 3,
"retention_days": 90,
},
"invalid_metric_name",
True,
),
(
b"invalid_json_payload",
"invalid_json",
False,
),
]
@pytest.mark.django_db
@pytest.mark.parametrize("invalid_payload, error_text, format_payload", invalid_payloads)
def test_process_messages_invalid_messages(
invalid_payload, error_text, format_payload, caplog
) -> None:
"""
Test the following kinds of invalid payloads:
* tag key > 200 char
* metric name > 200 char
* invalid json
Each outer_message that is passed into process_messages is a batch of messages. If
there is an invalid payload for one of the messages, we just drop that message,
not the entire batch.
The `counter_payload` in these tests is always a valid payload, and the test arg
`invalid_payload` has a payload that fits the scenarios outlined above.
"""
formatted_payload = (
json.dumps(invalid_payload).encode("utf-8") if format_payload else invalid_payload
)
message_batch = [
Message(
BrokerValue(
KafkaPayload(None, json.dumps(counter_payload).encode("utf-8"), []),
Partition(Topic("topic"), 0),
0,
BROKER_TIMESTAMP,
)
),
Message(
BrokerValue(
KafkaPayload(None, formatted_payload, []),
Partition(Topic("topic"), 0),
1,
BROKER_TIMESTAMP,
)
),
]
# the outer message uses the last message's partition, offset, and timestamp
last = message_batch[-1]
outer_message = Message(Value(message_batch, last.committable))
with caplog.at_level(logging.ERROR):
new_batch = MESSAGE_PROCESSOR.process_messages(outer_message=outer_message)
# we expect just the valid counter_payload msg to be left
expected_msg = message_batch[0]
expected_new_batch = [
Message(
Value(
KafkaPayload(
None,
json.dumps(__translated_payload(counter_payload)).encode("utf-8"),
[("metric_type", b"c")],
),
expected_msg.committable,
)
),
Message(
Value(
InvalidMessage(Partition(Topic("topic"), 0), 1),
message_batch[1].committable,
)
),
]
compare_message_batches_ignoring_metadata(new_batch, expected_new_batch)
assert error_text in caplog.text
@pytest.mark.django_db
def test_process_messages_rate_limited(caplog, settings) -> None:
"""
Test handling of `None`-values coming from the indexer service, which
happens when postgres writes are being rate-limited.
"""
settings.SENTRY_METRICS_INDEXER_DEBUG_LOG_SAMPLE_RATE = 1.0
rate_limited_payload = deepcopy(distribution_payload)
rate_limited_payload["tags"]["custom_tag"] = "rate_limited_test"
message_batch = [
Message(
BrokerValue(
KafkaPayload(None, json.dumps(counter_payload).encode("utf-8"), []),
Partition(Topic("topic"), 0),
0,
BROKER_TIMESTAMP,
)
),
Message(
BrokerValue(
KafkaPayload(None, json.dumps(rate_limited_payload).encode("utf-8"), []),
Partition(Topic("topic"), 0),
1,
BROKER_TIMESTAMP,
)
),
]
# the outer message uses the last message's partition, offset, and timestamp
last = message_batch[-1]
outer_message = Message(Value(message_batch, last.committable))
message_processor = MessageProcessor(
get_ingest_config(UseCaseKey.RELEASE_HEALTH, IndexerStorage.MOCK)
)
# Insert a None-value into the mock-indexer to simulate a rate-limit.
mock_indexer = message_processor._indexer
assert isinstance(mock_indexer, MockIndexer)
raw_simple_string_indexer = mock_indexer.indexer
assert isinstance(raw_simple_string_indexer, RawSimpleIndexer)
raw_simple_string_indexer._strings[UseCaseID.SESSIONS][1]["rate_limited_test"] = None
with caplog.at_level(logging.ERROR):
new_batch = message_processor.process_messages(outer_message=outer_message)
# we expect just the counter_payload msg to be left, as that one didn't
# cause/depend on string writes that have been rate limited
expected_msg = message_batch[0]
assert isinstance(expected_msg.value, BrokerValue)
expected_new_batch = [
Message(
BrokerValue(
KafkaPayload(
None,
json.dumps(__translated_payload(counter_payload)).encode("utf-8"),
[("metric_type", b"c")],
),
expected_msg.value.partition,
expected_msg.value.offset,
expected_msg.value.timestamp,
)
)
]
compare_message_batches_ignoring_metadata(new_batch, expected_new_batch)
assert "dropped_message" in caplog.text
@pytest.mark.django_db
def test_process_messages_cardinality_limited(
caplog, settings, monkeypatch, set_sentry_option
) -> None:
"""
Test that the message processor correctly calls the cardinality limiter.
"""
settings.SENTRY_METRICS_INDEXER_DEBUG_LOG_SAMPLE_RATE = 1.0
# set any limit at all to ensure we actually use the underlying rate limiter
with set_sentry_option(
"sentry-metrics.cardinality-limiter.limits.releasehealth.per-org",
[{"window_seconds": 3600, "granularity_seconds": 60, "limit": 0}],
), set_sentry_option("sentry-metrics.cardinality-limiter-rh.orgs-rollout-rate", 1.0):
class MockCardinalityLimiter(CardinalityLimiter):
def check_within_quotas(self, requested_quotas):
# Grant nothing, limit everything
return 123, []
def use_quotas(self, grants, timestamp):
pass
monkeypatch.setitem(
cardinality_limiter_factory.rate_limiters,
"releasehealth",
TimeseriesCardinalityLimiter("releasehealth", MockCardinalityLimiter()),
)
message_payloads = [counter_payload, distribution_payload, set_payload]
message_batch = [
Message(
BrokerValue(
KafkaPayload(None, json.dumps(payload).encode("utf-8"), []),
Partition(Topic("topic"), 0),
i + 1,
datetime.now(),
)
)
for i, payload in enumerate(message_payloads)
]
last = message_batch[-1]
outer_message = Message(Value(message_batch, last.committable))
with caplog.at_level(logging.ERROR):
new_batch = MESSAGE_PROCESSOR.process_messages(outer_message=outer_message)
compare_message_batches_ignoring_metadata(new_batch, [])
def test_valid_metric_name() -> None:
assert valid_metric_name("") is True
assert valid_metric_name("blah") is True
assert valid_metric_name("invalid" * 200) is False
def test_process_messages_is_pickleable():
# needed so that the parallel transform step starts up properly
pickle.dumps(MESSAGE_PROCESSOR.process_messages)