Skip to content

Commit e8be8ed

Browse files
authored
fix(pyspark): Grab attemptId more defensively (#4130)
Closes #1099
1 parent 50b1919 commit e8be8ed

File tree

2 files changed

+86
-2
lines changed

2 files changed

+86
-2
lines changed

sentry_sdk/integrations/spark/spark_driver.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,12 @@ def onStageSubmitted(self, stageSubmitted): # noqa: N802,N803
260260
# type: (Any) -> None
261261
stage_info = stageSubmitted.stageInfo()
262262
message = "Stage {} Submitted".format(stage_info.stageId())
263-
data = {"attemptId": stage_info.attemptId(), "name": stage_info.name()}
263+
264+
data = {"name": stage_info.name()}
265+
attempt_id = _get_attempt_id(stage_info)
266+
if attempt_id is not None:
267+
data["attemptId"] = attempt_id
268+
264269
self._add_breadcrumb(level="info", message=message, data=data)
265270
_set_app_properties()
266271

@@ -271,7 +276,11 @@ def onStageCompleted(self, stageCompleted): # noqa: N802,N803
271276
stage_info = stageCompleted.stageInfo()
272277
message = ""
273278
level = ""
274-
data = {"attemptId": stage_info.attemptId(), "name": stage_info.name()}
279+
280+
data = {"name": stage_info.name()}
281+
attempt_id = _get_attempt_id(stage_info)
282+
if attempt_id is not None:
283+
data["attemptId"] = attempt_id
275284

276285
# Have to Try Except because stageInfo.failureReason() is typed with Scala Option
277286
try:
@@ -283,3 +292,18 @@ def onStageCompleted(self, stageCompleted): # noqa: N802,N803
283292
level = "info"
284293

285294
self._add_breadcrumb(level=level, message=message, data=data)
295+
296+
297+
def _get_attempt_id(stage_info):
298+
# type: (Any) -> Optional[int]
299+
try:
300+
return stage_info.attemptId()
301+
except Exception:
302+
pass
303+
304+
try:
305+
return stage_info.attemptNumber()
306+
except Exception:
307+
pass
308+
309+
return None

tests/integrations/spark/test_spark.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from py4j.protocol import Py4JJavaError
1616

17+
1718
################
1819
# DRIVER TESTS #
1920
################
@@ -166,6 +167,65 @@ def stageInfo(self): # noqa: N802
166167
assert mock_hub.kwargs["data"]["name"] == "run-job"
167168

168169

170+
def test_sentry_listener_on_stage_submitted_no_attempt_id(sentry_listener):
171+
listener = sentry_listener
172+
with patch.object(listener, "_add_breadcrumb") as mock_add_breadcrumb:
173+
174+
class StageInfo:
175+
def stageId(self): # noqa: N802
176+
return "sample-stage-id-submit"
177+
178+
def name(self):
179+
return "run-job"
180+
181+
def attemptNumber(self): # noqa: N802
182+
return 14
183+
184+
class MockStageSubmitted:
185+
def stageInfo(self): # noqa: N802
186+
stageinf = StageInfo()
187+
return stageinf
188+
189+
mock_stage_submitted = MockStageSubmitted()
190+
listener.onStageSubmitted(mock_stage_submitted)
191+
192+
mock_add_breadcrumb.assert_called_once()
193+
mock_hub = mock_add_breadcrumb.call_args
194+
195+
assert mock_hub.kwargs["level"] == "info"
196+
assert "sample-stage-id-submit" in mock_hub.kwargs["message"]
197+
assert mock_hub.kwargs["data"]["attemptId"] == 14
198+
assert mock_hub.kwargs["data"]["name"] == "run-job"
199+
200+
201+
def test_sentry_listener_on_stage_submitted_no_attempt_id_or_number(sentry_listener):
202+
listener = sentry_listener
203+
with patch.object(listener, "_add_breadcrumb") as mock_add_breadcrumb:
204+
205+
class StageInfo:
206+
def stageId(self): # noqa: N802
207+
return "sample-stage-id-submit"
208+
209+
def name(self):
210+
return "run-job"
211+
212+
class MockStageSubmitted:
213+
def stageInfo(self): # noqa: N802
214+
stageinf = StageInfo()
215+
return stageinf
216+
217+
mock_stage_submitted = MockStageSubmitted()
218+
listener.onStageSubmitted(mock_stage_submitted)
219+
220+
mock_add_breadcrumb.assert_called_once()
221+
mock_hub = mock_add_breadcrumb.call_args
222+
223+
assert mock_hub.kwargs["level"] == "info"
224+
assert "sample-stage-id-submit" in mock_hub.kwargs["message"]
225+
assert "attemptId" not in mock_hub.kwargs["data"]
226+
assert mock_hub.kwargs["data"]["name"] == "run-job"
227+
228+
169229
@pytest.fixture
170230
def get_mock_stage_completed():
171231
def _inner(failure_reason):

0 commit comments

Comments
 (0)