|
| 1 | +from sentry_sdk import configure_scope |
| 2 | +from sentry_sdk.hub import Hub |
| 3 | +from sentry_sdk.integrations import Integration |
| 4 | +from sentry_sdk.utils import capture_internal_exceptions |
| 5 | + |
| 6 | +from sentry_sdk._types import MYPY |
| 7 | + |
| 8 | +if MYPY: |
| 9 | + from typing import Any |
| 10 | + from typing import Optional |
| 11 | + |
| 12 | + from sentry_sdk._types import Event, Hint |
| 13 | + |
| 14 | + |
| 15 | +class SparkIntegration(Integration): |
| 16 | + identifier = "spark" |
| 17 | + |
| 18 | + @staticmethod |
| 19 | + def setup_once(): |
| 20 | + # type: () -> None |
| 21 | + patch_spark_context_init() |
| 22 | + |
| 23 | + |
| 24 | +def _set_app_properties(): |
| 25 | + # type: () -> None |
| 26 | + """ |
| 27 | + Set properties in driver that propagate to worker processes, allowing for workers to have access to those properties. |
| 28 | + This allows worker integration to have access to app_name and application_id. |
| 29 | + """ |
| 30 | + from pyspark import SparkContext |
| 31 | + |
| 32 | + sparkContext = SparkContext._active_spark_context |
| 33 | + if sparkContext: |
| 34 | + sparkContext.setLocalProperty("sentry_app_name", sparkContext.appName) |
| 35 | + sparkContext.setLocalProperty( |
| 36 | + "sentry_application_id", sparkContext.applicationId |
| 37 | + ) |
| 38 | + |
| 39 | + |
| 40 | +def _start_sentry_listener(sc): |
| 41 | + # type: (Any) -> None |
| 42 | + """ |
| 43 | + Start java gateway server to add custom `SparkListener` |
| 44 | + """ |
| 45 | + from pyspark.java_gateway import ensure_callback_server_started |
| 46 | + |
| 47 | + gw = sc._gateway |
| 48 | + ensure_callback_server_started(gw) |
| 49 | + listener = SentryListener() |
| 50 | + sc._jsc.sc().addSparkListener(listener) |
| 51 | + |
| 52 | + |
| 53 | +def patch_spark_context_init(): |
| 54 | + # type: () -> None |
| 55 | + from pyspark import SparkContext |
| 56 | + |
| 57 | + spark_context_init = SparkContext._do_init |
| 58 | + |
| 59 | + def _sentry_patched_spark_context_init(self, *args, **kwargs): |
| 60 | + # type: (SparkContext, *Any, **Any) -> Optional[Any] |
| 61 | + init = spark_context_init(self, *args, **kwargs) |
| 62 | + |
| 63 | + if Hub.current.get_integration(SparkIntegration) is None: |
| 64 | + return init |
| 65 | + |
| 66 | + _start_sentry_listener(self) |
| 67 | + _set_app_properties() |
| 68 | + |
| 69 | + with configure_scope() as scope: |
| 70 | + |
| 71 | + @scope.add_event_processor |
| 72 | + def process_event(event, hint): |
| 73 | + # type: (Event, Hint) -> Optional[Event] |
| 74 | + with capture_internal_exceptions(): |
| 75 | + if Hub.current.get_integration(SparkIntegration) is None: |
| 76 | + return event |
| 77 | + |
| 78 | + event.setdefault("user", {}).setdefault("id", self.sparkUser()) |
| 79 | + |
| 80 | + event.setdefault("tags", {}).setdefault( |
| 81 | + "executor.id", self._conf.get("spark.executor.id") |
| 82 | + ) |
| 83 | + event["tags"].setdefault( |
| 84 | + "spark-submit.deployMode", |
| 85 | + self._conf.get("spark.submit.deployMode"), |
| 86 | + ) |
| 87 | + event["tags"].setdefault( |
| 88 | + "driver.host", self._conf.get("spark.driver.host") |
| 89 | + ) |
| 90 | + event["tags"].setdefault( |
| 91 | + "driver.port", self._conf.get("spark.driver.port") |
| 92 | + ) |
| 93 | + event["tags"].setdefault("spark_version", self.version) |
| 94 | + event["tags"].setdefault("app_name", self.appName) |
| 95 | + event["tags"].setdefault("application_id", self.applicationId) |
| 96 | + event["tags"].setdefault("master", self.master) |
| 97 | + event["tags"].setdefault("spark_home", self.sparkHome) |
| 98 | + |
| 99 | + event.setdefault("extra", {}).setdefault("web_url", self.uiWebUrl) |
| 100 | + |
| 101 | + return event |
| 102 | + |
| 103 | + return init |
| 104 | + |
| 105 | + SparkContext._do_init = _sentry_patched_spark_context_init |
| 106 | + |
| 107 | + |
| 108 | +class SparkListener(object): |
| 109 | + def onApplicationEnd(self, applicationEnd): |
| 110 | + # type: (Any) -> None |
| 111 | + pass |
| 112 | + |
| 113 | + def onApplicationStart(self, applicationStart): |
| 114 | + # type: (Any) -> None |
| 115 | + pass |
| 116 | + |
| 117 | + def onBlockManagerAdded(self, blockManagerAdded): |
| 118 | + # type: (Any) -> None |
| 119 | + pass |
| 120 | + |
| 121 | + def onBlockManagerRemoved(self, blockManagerRemoved): |
| 122 | + # type: (Any) -> None |
| 123 | + pass |
| 124 | + |
| 125 | + def onBlockUpdated(self, blockUpdated): |
| 126 | + # type: (Any) -> None |
| 127 | + pass |
| 128 | + |
| 129 | + def onEnvironmentUpdate(self, environmentUpdate): |
| 130 | + # type: (Any) -> None |
| 131 | + pass |
| 132 | + |
| 133 | + def onExecutorAdded(self, executorAdded): |
| 134 | + # type: (Any) -> None |
| 135 | + pass |
| 136 | + |
| 137 | + def onExecutorBlacklisted(self, executorBlacklisted): |
| 138 | + # type: (Any) -> None |
| 139 | + pass |
| 140 | + |
| 141 | + def onExecutorBlacklistedForStage(self, executorBlacklistedForStage): |
| 142 | + # type: (Any) -> None |
| 143 | + pass |
| 144 | + |
| 145 | + def onExecutorMetricsUpdate(self, executorMetricsUpdate): |
| 146 | + # type: (Any) -> None |
| 147 | + pass |
| 148 | + |
| 149 | + def onExecutorRemoved(self, executorRemoved): |
| 150 | + # type: (Any) -> None |
| 151 | + pass |
| 152 | + |
| 153 | + def onJobEnd(self, jobEnd): |
| 154 | + # type: (Any) -> None |
| 155 | + pass |
| 156 | + |
| 157 | + def onJobStart(self, jobStart): |
| 158 | + # type: (Any) -> None |
| 159 | + pass |
| 160 | + |
| 161 | + def onNodeBlacklisted(self, nodeBlacklisted): |
| 162 | + # type: (Any) -> None |
| 163 | + pass |
| 164 | + |
| 165 | + def onNodeBlacklistedForStage(self, nodeBlacklistedForStage): |
| 166 | + # type: (Any) -> None |
| 167 | + pass |
| 168 | + |
| 169 | + def onNodeUnblacklisted(self, nodeUnblacklisted): |
| 170 | + # type: (Any) -> None |
| 171 | + pass |
| 172 | + |
| 173 | + def onOtherEvent(self, event): |
| 174 | + # type: (Any) -> None |
| 175 | + pass |
| 176 | + |
| 177 | + def onSpeculativeTaskSubmitted(self, speculativeTask): |
| 178 | + # type: (Any) -> None |
| 179 | + pass |
| 180 | + |
| 181 | + def onStageCompleted(self, stageCompleted): |
| 182 | + # type: (Any) -> None |
| 183 | + pass |
| 184 | + |
| 185 | + def onStageSubmitted(self, stageSubmitted): |
| 186 | + # type: (Any) -> None |
| 187 | + pass |
| 188 | + |
| 189 | + def onTaskEnd(self, taskEnd): |
| 190 | + # type: (Any) -> None |
| 191 | + pass |
| 192 | + |
| 193 | + def onTaskGettingResult(self, taskGettingResult): |
| 194 | + # type: (Any) -> None |
| 195 | + pass |
| 196 | + |
| 197 | + def onTaskStart(self, taskStart): |
| 198 | + # type: (Any) -> None |
| 199 | + pass |
| 200 | + |
| 201 | + def onUnpersistRDD(self, unpersistRDD): |
| 202 | + # type: (Any) -> None |
| 203 | + pass |
| 204 | + |
| 205 | + class Java: |
| 206 | + implements = ["org.apache.spark.scheduler.SparkListenerInterface"] |
| 207 | + |
| 208 | + |
| 209 | +class SentryListener(SparkListener): |
| 210 | + def __init__(self): |
| 211 | + # type: () -> None |
| 212 | + self.hub = Hub.current |
| 213 | + |
| 214 | + def onJobStart(self, jobStart): |
| 215 | + # type: (Any) -> None |
| 216 | + message = "Job {} Started".format(jobStart.jobId()) |
| 217 | + self.hub.add_breadcrumb(level="info", message=message) |
| 218 | + _set_app_properties() |
| 219 | + |
| 220 | + def onJobEnd(self, jobEnd): |
| 221 | + # type: (Any) -> None |
| 222 | + level = "" |
| 223 | + message = "" |
| 224 | + data = {"result": jobEnd.jobResult().toString()} |
| 225 | + |
| 226 | + if jobEnd.jobResult().toString() == "JobSucceeded": |
| 227 | + level = "info" |
| 228 | + message = "Job {} Ended".format(jobEnd.jobId()) |
| 229 | + else: |
| 230 | + level = "warning" |
| 231 | + message = "Job {} Failed".format(jobEnd.jobId()) |
| 232 | + |
| 233 | + self.hub.add_breadcrumb(level=level, message=message, data=data) |
| 234 | + |
| 235 | + def onStageSubmitted(self, stageSubmitted): |
| 236 | + # type: (Any) -> None |
| 237 | + stageInfo = stageSubmitted.stageInfo() |
| 238 | + message = "Stage {} Submitted".format(stageInfo.stageId()) |
| 239 | + data = {"attemptId": stageInfo.attemptId(), "name": stageInfo.name()} |
| 240 | + self.hub.add_breadcrumb(level="info", message=message, data=data) |
| 241 | + _set_app_properties() |
| 242 | + |
| 243 | + def onStageCompleted(self, stageCompleted): |
| 244 | + # type: (Any) -> None |
| 245 | + from py4j.protocol import Py4JJavaError # type: ignore |
| 246 | + |
| 247 | + stageInfo = stageCompleted.stageInfo() |
| 248 | + message = "" |
| 249 | + level = "" |
| 250 | + data = {"attemptId": stageInfo.attemptId(), "name": stageInfo.name()} |
| 251 | + |
| 252 | + # Have to Try Except because stageInfo.failureReason() is typed with Scala Option |
| 253 | + try: |
| 254 | + data["reason"] = stageInfo.failureReason().get() |
| 255 | + message = "Stage {} Failed".format(stageInfo.stageId()) |
| 256 | + level = "warning" |
| 257 | + except Py4JJavaError: |
| 258 | + message = "Stage {} Completed".format(stageInfo.stageId()) |
| 259 | + level = "info" |
| 260 | + |
| 261 | + self.hub.add_breadcrumb(level=level, message=message, data=data) |
0 commit comments