Skip to content

Commit 400387e

Browse files
committed
fix(pyspark): Grab attemptId more defensively
1 parent a97c53c commit 400387e

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

sentry_sdk/integrations/spark/spark_driver.py

Lines changed: 32 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,24 @@ 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+
from py4j.protocol import Py4JJavaError # type: ignore
299+
300+
try:
301+
return stage_info.attemptId()
302+
except Py4JJavaError:
303+
pass
304+
305+
try:
306+
return stage_info.attemptNumber()
307+
except Py4JJavaError:
308+
pass
309+
310+
try:
311+
return stage_info.currentAttemptId()
312+
except Py4JJavaError:
313+
pass
314+
315+
return None

0 commit comments

Comments
 (0)