
    lh$                         d dl Z d dlmZ d dlmZmZ d dlmZ erd dlmZ d dlm	Z	 d dl
mZmZ d dlmZ  G d	 d
e      Zd Zd Zd Zd Zd Zd Z G d d      Z G d de      Zd Zy)    N)Integration)capture_internal_exceptionsensure_integration_enabled)TYPE_CHECKING)Any)Optional)EventHintSparkContextc                        e Zd ZdZed        Zy)SparkIntegrationsparkc                      t                y N)_setup_sentry_tracing     |/var/www/pru.catia.catastroantioquia-mas.com/tasa/lib/python3.12/site-packages/sentry_sdk/integrations/spark/spark_driver.py
setup_oncezSparkIntegration.setup_once   s
     	r   N)__name__
__module____qualname__
identifierstaticmethodr   r   r   r   r   r      s    J   r   r   c                      ddl m}  | j                  }|r9|j                  d|j                         |j                  d|j
                         yy)z
    Set properties in driver that propagate to worker processes, allowing for workers to have access to those properties.
    This allows worker integration to have access to app_name and application_id.
    r   r   sentry_app_namesentry_application_idN)pysparkr   _active_spark_contextsetLocalPropertyappNameapplicationId)r   spark_contexts     r   _set_app_propertiesr%      sP     % 66M&&!!	
 	&&#''	
 r   c                     ddl m} | j                  } ||       t               }| j                  j                         j                  |       y)zA
    Start java gateway server to add custom `SparkListener`
    r   )ensure_callback_server_startedN)pyspark.java_gatewayr'   _gatewaySentryListener_jscscaddSparkListener)r,   r'   gwlisteners       r   _start_sentry_listenerr0   ,   s9    
 D	B"2&HGGJJL!!(+r   c                 V     t        j                         }|j                   fd       }y )Nc                 R   t               5  t        j                         j                  t              | cd d d        S j
                  | cd d d        S | j                  di       j                  dj                                | j                  di       j                  dj                  j                  d             | d   j                  dj                  j                  d             | d   j                  dj                  j                  d	             | d   j                  d
j                  j                  d             | d   j                  dj                         | d   j                  dj                         | d   j                  dj                         | d   j                  dj                         | d   j                  dj                         | j                  di       j                  dj                         d d d        | S # 1 sw Y   | S xY w)Nuseridtagszexecutor.idzspark.executor.idzspark-submit.deployModezspark.submit.deployModezdriver.hostzspark.driver.hostzdriver.portzspark.driver.portspark_versionapp_nameapplication_idmaster
spark_homeextraweb_url)r   
sentry_sdk
get_clientget_integrationr   r    
setdefault	sparkUser_confgetversionr"   r#   r9   	sparkHomeuiWebUrl)eventhintr,   s     r   process_eventz+_add_event_processor.<locals>.process_event=   s    )* 	M$$&667GHP	M 	M ''/	M 	M VR(33D",,.IVR(33rxx||,?@ &M$$)67 &M$$]BHHLLAT4UV&M$$]BHHLLAT4UV&M$$_bjjA&M$$Z<&M$$%5r7G7GH&M$$Xryy9&M$$\2<<@Wb)44YL1	M4 5	M4 s   )HHF<HH&)r=   get_isolation_scopeadd_event_processor)r,   scoperI   s   `  r   _add_event_processorrM   9   s)    **,E
 r   c                 D    t        |        t                t        |        y r   )r0   r%   rM   )r,   s    r   _activate_integrationrO   ]   s     2r   c                  h    ddl m}  | j                  t        t              fd       }|| _        y )Nr   r   c                 6     | g|i |}t        |        |S r   )rO   )selfargskwargsrvspark_context_inits       r   "_sentry_patched_spark_context_initzE_patch_spark_context_init.<locals>._sentry_patched_spark_context_initk   s&      6t6v6d#	r   )r   r   _do_initr   r   )r   rW   rV   s     @r   _patch_spark_context_initrY   e   s8    $%.. 02DE F ?Lr   c                  h    ddl m}  | j                  t        | j                         y t	                y )Nr   r   )r   r   r    rO   rY   r   s    r   r   r   u   s(    $))5l@@Ar   c                       e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z G d d      Zy)SparkListenerc                      y r   r   )rR   applicationEnds     r   onApplicationEndzSparkListener.onApplicationEnd       r   c                      y r   r   )rR   applicationStarts     r   onApplicationStartz SparkListener.onApplicationStart   r`   r   c                      y r   r   )rR   blockManagerAddeds     r   onBlockManagerAddedz!SparkListener.onBlockManagerAdded   r`   r   c                      y r   r   )rR   blockManagerRemoveds     r   onBlockManagerRemovedz#SparkListener.onBlockManagerRemoved   r`   r   c                      y r   r   )rR   blockUpdateds     r   onBlockUpdatedzSparkListener.onBlockUpdated   r`   r   c                      y r   r   )rR   environmentUpdates     r   onEnvironmentUpdatez!SparkListener.onEnvironmentUpdate   r`   r   c                      y r   r   )rR   executorAddeds     r   onExecutorAddedzSparkListener.onExecutorAdded   r`   r   c                      y r   r   )rR   executorBlacklisteds     r   onExecutorBlacklistedz#SparkListener.onExecutorBlacklisted   r`   r   c                      y r   r   )rR   executorBlacklistedForStages     r   onExecutorBlacklistedForStagez+SparkListener.onExecutorBlacklistedForStage   s     	r   c                      y r   r   )rR   executorMetricsUpdates     r   onExecutorMetricsUpdatez%SparkListener.onExecutorMetricsUpdate   r`   r   c                      y r   r   )rR   executorRemoveds     r   onExecutorRemovedzSparkListener.onExecutorRemoved   r`   r   c                      y r   r   )rR   jobEnds     r   onJobEndzSparkListener.onJobEnd   r`   r   c                      y r   r   )rR   jobStarts     r   
onJobStartzSparkListener.onJobStart   r`   r   c                      y r   r   )rR   nodeBlacklisteds     r   onNodeBlacklistedzSparkListener.onNodeBlacklisted   r`   r   c                      y r   r   )rR   nodeBlacklistedForStages     r   onNodeBlacklistedForStagez'SparkListener.onNodeBlacklistedForStage   r`   r   c                      y r   r   )rR   nodeUnblacklisteds     r   onNodeUnblacklistedz!SparkListener.onNodeUnblacklisted   r`   r   c                      y r   r   )rR   rG   s     r   onOtherEventzSparkListener.onOtherEvent   r`   r   c                      y r   r   )rR   speculativeTasks     r   onSpeculativeTaskSubmittedz(SparkListener.onSpeculativeTaskSubmitted   r`   r   c                      y r   r   )rR   stageCompleteds     r   onStageCompletedzSparkListener.onStageCompleted   r`   r   c                      y r   r   )rR   stageSubmitteds     r   onStageSubmittedzSparkListener.onStageSubmitted   r`   r   c                      y r   r   )rR   taskEnds     r   	onTaskEndzSparkListener.onTaskEnd   r`   r   c                      y r   r   )rR   taskGettingResults     r   onTaskGettingResultz!SparkListener.onTaskGettingResult   r`   r   c                      y r   r   )rR   	taskStarts     r   onTaskStartzSparkListener.onTaskStart   r`   r   c                      y r   r   )rR   unpersistRDDs     r   onUnpersistRDDzSparkListener.onUnpersistRDD   r`   r   c                       e Zd ZdgZy)SparkListener.Javaz1org.apache.spark.scheduler.SparkListenerInterfaceN)r   r   r   
implementsr   r   r   Javar      s
    IJ
r   r   N)r   r   r   r_   rc   rf   ri   rl   ro   rr   ru   rx   r{   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r\   r\      s    K Kr   r\   c                   .    e Zd Z	 ddZd Zd Zd Zd Zy)r*   Nc                 P    t        j                         j                  |||       y )Nlevelmessagedata)r=   rJ   add_breadcrumb)rR   r   r   r   s       r   _add_breadcrumbzSentryListener._add_breadcrumb   s'     	&&(77t 	8 	
r   c                     t        j                         j                          dj                  |j	                               }| j                  d|       t                y )NzJob {} Startedinfo)r   r   )r=   rJ   clear_breadcrumbsformatjobIdr   r%   )rR   r   r   s      r   r   zSentryListener.onJobStart   sG    &&(::<"))(..*:;67;r   c                 <   d}d}d|j                         j                         i}|j                         j                         dk(  r"d}dj                  |j                               }n!d}dj                  |j                               }| j	                  |||       y )	N resultJobSucceededr   zJob {} EndedwarningzJob {} Failedr   )	jobResulttoStringr   r   r   )rR   r   r   r   r   s        r   r   zSentryListener.onJobEnd   s    &**,5578&&(N:E$++FLLN;GE%,,V\\^<G5'Er   c                     |j                         }dj                  |j                               }d|j                         i}t	        |      }|||d<   | j                  d||       t                y )NzStage {} Submittedname	attemptIdr   r   )	stageInfor   stageIdr   _get_attempt_idr   r%   )rR   r   
stage_infor   r   
attempt_ids         r   r   zSentryListener.onStageSubmitted	  so    #--/
&--j.@.@.BC
)*$Z0
! *D67Fr   c                    ddl m} |j                         }d}d}d|j                         i}t	        |      }|||d<   	 |j                         j                         |d<   dj                  |j                               }d}| j                  |||       y # |$ r$ d	j                  |j                               }d
}Y =w xY w)Nr   )Py4JJavaErrorr   r   r   reasonzStage {} Failedr   zStage {} Completedr   r   )
py4j.protocolr   r   r   r   failureReasonrC   r   r   r   )rR   r   r   r   r   r   r   r   s           r   r   zSentryListener.onStageCompleted  s    /#--/

)*$Z0
! *D	'557;;=DN'..z/A/A/CDGE
 	5'E	  	*11*2D2D2FGGE	s    AB &C ?C r   )r   r   r   r   r   r   r   r   r   r   r   r*   r*      s"    
 		
FFr   r*   c                     	 | j                         S # t        $ r Y nw xY w	 | j                         S # t        $ r Y y w xY wr   )r   	ExceptionattemptNumber)r   s    r   r   r   /  sQ    ##%% '')) s    	2 	>>)r=   sentry_sdk.integrationsr   sentry_sdk.utilsr   r   typingr   r   r   sentry_sdk._typesr	   r
   r   r   r   r%   r0   rM   rO   rY   r   r\   r*   r   r   r   r   <module>r      sp     / T  -$ {  
(
,!H?  dK dKNFF] FFRr   