forked from getsentry/sentry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_spark.py
More file actions
348 lines (253 loc) · 10.6 KB
/
Copy pathtest_spark.py
File metadata and controls
348 lines (253 loc) · 10.6 KB
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
import sys
from unittest.mock import patch
import pytest
from py4j.protocol import Py4JJavaError
from pyspark import SparkConf, SparkContext
from sentry_sdk.integrations.spark.spark_driver import (
SentryListener,
SparkIntegration,
_set_app_properties,
_start_sentry_listener,
)
from sentry_sdk.integrations.spark.spark_worker import SparkWorkerIntegration
################
# DRIVER TESTS #
################
@pytest.fixture(scope="function")
def sentry_init_with_reset(sentry_init):
from sentry_sdk.integrations import _processed_integrations
yield lambda: sentry_init(integrations=[SparkIntegration()])
_processed_integrations.discard("spark")
@pytest.fixture(scope="session")
def create_spark_context():
conf = SparkConf().set("spark.driver.bindAddress", "127.0.0.1")
sc = SparkContext(conf=conf, appName="Testing123")
yield lambda: sc
sc.stop()
def test_set_app_properties(create_spark_context):
spark_context = create_spark_context()
_set_app_properties()
assert spark_context.getLocalProperty("sentry_app_name") == "Testing123"
# applicationId generated by sparkContext init
assert (
spark_context.getLocalProperty("sentry_application_id")
== spark_context.applicationId
)
def test_start_sentry_listener(create_spark_context):
spark_context = create_spark_context()
gateway = spark_context._gateway
assert gateway._callback_server is None
_start_sentry_listener(spark_context)
assert gateway._callback_server is not None
@patch("sentry_sdk.integrations.spark.spark_driver._patch_spark_context_init")
def test_initialize_spark_integration_before_spark_context_init(
mock_patch_spark_context_init,
sentry_init_with_reset,
):
# As we are using the same SparkContext connection for the whole session,
# we clean it during this test.
original_context = SparkContext._active_spark_context
SparkContext._active_spark_context = None
try:
sentry_init_with_reset()
mock_patch_spark_context_init.assert_called_once()
finally:
# Restore the original one.
SparkContext._active_spark_context = original_context
@patch("sentry_sdk.integrations.spark.spark_driver._activate_integration")
def test_initialize_spark_integration_after_spark_context_init(
mock_activate_integration,
create_spark_context,
sentry_init_with_reset,
):
create_spark_context()
sentry_init_with_reset()
mock_activate_integration.assert_called_once()
@pytest.fixture
def sentry_listener():
listener = SentryListener()
return listener
def test_sentry_listener_on_job_start(sentry_listener):
listener = sentry_listener
with patch.object(listener, "_add_breadcrumb") as mock_add_breadcrumb:
class MockJobStart:
def jobId(self): # noqa: N802
return "sample-job-id-start"
mock_job_start = MockJobStart()
listener.onJobStart(mock_job_start)
mock_add_breadcrumb.assert_called_once()
mock_hub = mock_add_breadcrumb.call_args
assert mock_hub.kwargs["level"] == "info"
assert "sample-job-id-start" in mock_hub.kwargs["message"]
@pytest.mark.parametrize(
"job_result, level", [("JobSucceeded", "info"), ("JobFailed", "warning")]
)
def test_sentry_listener_on_job_end(sentry_listener, job_result, level):
listener = sentry_listener
with patch.object(listener, "_add_breadcrumb") as mock_add_breadcrumb:
class MockJobResult:
def toString(self): # noqa: N802
return job_result
class MockJobEnd:
def jobId(self): # noqa: N802
return "sample-job-id-end"
def jobResult(self): # noqa: N802
result = MockJobResult()
return result
mock_job_end = MockJobEnd()
listener.onJobEnd(mock_job_end)
mock_add_breadcrumb.assert_called_once()
mock_hub = mock_add_breadcrumb.call_args
assert mock_hub.kwargs["level"] == level
assert mock_hub.kwargs["data"]["result"] == job_result
assert "sample-job-id-end" in mock_hub.kwargs["message"]
def test_sentry_listener_on_stage_submitted(sentry_listener):
listener = sentry_listener
with patch.object(listener, "_add_breadcrumb") as mock_add_breadcrumb:
class StageInfo:
def stageId(self): # noqa: N802
return "sample-stage-id-submit"
def name(self):
return "run-job"
def attemptId(self): # noqa: N802
return 14
class MockStageSubmitted:
def stageInfo(self): # noqa: N802
stageinf = StageInfo()
return stageinf
mock_stage_submitted = MockStageSubmitted()
listener.onStageSubmitted(mock_stage_submitted)
mock_add_breadcrumb.assert_called_once()
mock_hub = mock_add_breadcrumb.call_args
assert mock_hub.kwargs["level"] == "info"
assert "sample-stage-id-submit" in mock_hub.kwargs["message"]
assert mock_hub.kwargs["data"]["attemptId"] == 14
assert mock_hub.kwargs["data"]["name"] == "run-job"
def test_sentry_listener_on_stage_submitted_no_attempt_id(sentry_listener):
listener = sentry_listener
with patch.object(listener, "_add_breadcrumb") as mock_add_breadcrumb:
class StageInfo:
def stageId(self): # noqa: N802
return "sample-stage-id-submit"
def name(self):
return "run-job"
def attemptNumber(self): # noqa: N802
return 14
class MockStageSubmitted:
def stageInfo(self): # noqa: N802
stageinf = StageInfo()
return stageinf
mock_stage_submitted = MockStageSubmitted()
listener.onStageSubmitted(mock_stage_submitted)
mock_add_breadcrumb.assert_called_once()
mock_hub = mock_add_breadcrumb.call_args
assert mock_hub.kwargs["level"] == "info"
assert "sample-stage-id-submit" in mock_hub.kwargs["message"]
assert mock_hub.kwargs["data"]["attemptId"] == 14
assert mock_hub.kwargs["data"]["name"] == "run-job"
def test_sentry_listener_on_stage_submitted_no_attempt_id_or_number(sentry_listener):
listener = sentry_listener
with patch.object(listener, "_add_breadcrumb") as mock_add_breadcrumb:
class StageInfo:
def stageId(self): # noqa: N802
return "sample-stage-id-submit"
def name(self):
return "run-job"
class MockStageSubmitted:
def stageInfo(self): # noqa: N802
stageinf = StageInfo()
return stageinf
mock_stage_submitted = MockStageSubmitted()
listener.onStageSubmitted(mock_stage_submitted)
mock_add_breadcrumb.assert_called_once()
mock_hub = mock_add_breadcrumb.call_args
assert mock_hub.kwargs["level"] == "info"
assert "sample-stage-id-submit" in mock_hub.kwargs["message"]
assert "attemptId" not in mock_hub.kwargs["data"]
assert mock_hub.kwargs["data"]["name"] == "run-job"
@pytest.fixture
def get_mock_stage_completed():
def _inner(failure_reason):
class JavaException:
def __init__(self):
self._target_id = "id"
class FailureReason:
def get(self):
if failure_reason:
return "failure-reason"
else:
raise Py4JJavaError("msg", JavaException())
class StageInfo:
def stageId(self): # noqa: N802
return "sample-stage-id-submit"
def name(self):
return "run-job"
def attemptId(self): # noqa: N802
return 14
def failureReason(self): # noqa: N802
return FailureReason()
class MockStageCompleted:
def stageInfo(self): # noqa: N802
return StageInfo()
return MockStageCompleted()
return _inner
def test_sentry_listener_on_stage_completed_success(
sentry_listener, get_mock_stage_completed
):
listener = sentry_listener
with patch.object(listener, "_add_breadcrumb") as mock_add_breadcrumb:
mock_stage_completed = get_mock_stage_completed(failure_reason=False)
listener.onStageCompleted(mock_stage_completed)
mock_add_breadcrumb.assert_called_once()
mock_hub = mock_add_breadcrumb.call_args
assert mock_hub.kwargs["level"] == "info"
assert "sample-stage-id-submit" in mock_hub.kwargs["message"]
assert mock_hub.kwargs["data"]["attemptId"] == 14
assert mock_hub.kwargs["data"]["name"] == "run-job"
assert "reason" not in mock_hub.kwargs["data"]
def test_sentry_listener_on_stage_completed_failure(
sentry_listener, get_mock_stage_completed
):
listener = sentry_listener
with patch.object(listener, "_add_breadcrumb") as mock_add_breadcrumb:
mock_stage_completed = get_mock_stage_completed(failure_reason=True)
listener.onStageCompleted(mock_stage_completed)
mock_add_breadcrumb.assert_called_once()
mock_hub = mock_add_breadcrumb.call_args
assert mock_hub.kwargs["level"] == "warning"
assert "sample-stage-id-submit" in mock_hub.kwargs["message"]
assert mock_hub.kwargs["data"]["attemptId"] == 14
assert mock_hub.kwargs["data"]["name"] == "run-job"
assert mock_hub.kwargs["data"]["reason"] == "failure-reason"
################
# WORKER TESTS #
################
def test_spark_worker(monkeypatch, sentry_init, capture_events, capture_exceptions):
import pyspark.daemon as original_daemon
import pyspark.worker as original_worker
from pyspark.taskcontext import TaskContext
task_context = TaskContext._getOrCreate()
def mock_main():
task_context._stageId = 0
task_context._attemptNumber = 1
task_context._partitionId = 2
task_context._taskAttemptId = 3
try:
raise ZeroDivisionError
except ZeroDivisionError:
sys.exit(-1)
monkeypatch.setattr(original_worker, "main", mock_main)
sentry_init(integrations=[SparkWorkerIntegration()])
events = capture_events()
exceptions = capture_exceptions()
original_daemon.worker_main()
# SystemExit called, but not recorded as part of event
assert type(exceptions.pop()) == SystemExit
assert len(events[0]["exception"]["values"]) == 1
assert events[0]["exception"]["values"][0]["type"] == "ZeroDivisionError"
assert events[0]["tags"] == {
"stageId": "0",
"attemptNumber": "1",
"partitionId": "2",
"taskAttemptId": "3",
}