o
    ai*6                     @   s  d dl Z d dlZd dlZd dlmZ d dlZd dlmZ d dlm	Z	m
Z
mZ d dlmZ d dlmZ d dlmZ d dlmZmZmZmZ zd d	l mZ W n eyW   ed
w zd dlmZ d dlmZ d dlmZ d dl m!Z!m"Z" W n ey   edw z
d dl#m$Z%m&Z' W n ey   dZ%dZ'Y nw d dl(m)Z) e)rd dl(m*Z*m+Z+m,Z,m-Z-m.Z. d dl/m0Z0m1Z1 d dl m2Z2 d dl3m4Z4 d dl5m6Z6m7Z7 ed G dd de
Z8d1ddZ9G dd deZ:G dd  d e:Z;d1d!d"Z<	#	$		%d2d&d'Z=	(	)		%d3d*d+Z>d4d/d0Z?dS )5    Nisawaitable)OP)_check_minimum_versionIntegrationDidNotEnable)ignore_logger)should_send_default_pii)TransactionSource)capture_internal_exceptionsensure_integration_enabledevent_from_exceptionpackage_version)cached_propertyz;strawberry-graphql integration requires Python 3.8 or newer)Schema)SchemaExtension)should_skip_tracing)async_base_viewsync_base_viewz#strawberry-graphql is not installed)SentryTracingExtensionSentryTracingExtensionSync)TYPE_CHECKING)AnyCallable	GeneratorListOptional)GraphQLErrorGraphQLResolveInfo)GraphQLHTTPResponse)ExecutionContext)EventEventProcessorzstrawberry.executionc                   @   s4   e Zd ZdZde ZddddZedd	d
ZdS )StrawberryIntegration
strawberryzauto.graphql.Nasync_executionOptional[bool]returnc                 C   s    |dvrt d||| _d S )N)NFTz6Invalid value for async_execution: "{}" (must be bool))
ValueErrorformatr%   )selfr%    r+   `/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/sentry_sdk/integrations/strawberry.py__init__@   s   
zStrawberryIntegration.__init__c                  C   s$   t d} tt| d t  t  d S )Nzstrawberry-graphql)r   r   r#   _patch_schema_init_patch_views)versionr+   r+   r,   
setup_onceI   s   
z StrawberryIntegration.setup_onceN)r%   r&   r'   Nr'   N)__name__
__module____qualname__
identifieroriginr-   staticmethodr1   r+   r+   r+   r,   r#   <   s    
	r#   r'   c                     s8   t j t 								 d	 fdd} | t _d S )
Nr*   r   argsr   kwargsr'   c                    s   t  t}|d u r | g|R i |S |dpg }d }|jd ur(|j}nt|}|d u r9tjddd d}dd |D }|	|rFt
nt ||d<  | g|R i |S )N
extensionszwAssuming strawberry is running sync. If not, initialize the integration as StrawberryIntegration(async_execution=True).   )
stacklevelFc                 S   s   g | ]
}|t tfvr|qS r+   StrawberrySentryAsyncExtensionStrawberrySentrySyncExtension).0	extensionr+   r+   r,   
<listcomp>n   s    zK_patch_schema_init.<locals>._sentry_patched_schema_init.<locals>.<listcomp>)
sentry_sdk
get_clientget_integrationr#   getr%   _guess_if_using_asyncwarningswarnappendSentryAsyncExtensionSentrySyncExtension)r*   r:   r;   integrationr<   should_use_async_extensionold_schema_initr+   r,   _sentry_patched_schema_initU   s,   

z7_patch_schema_init.<locals>._sentry_patched_schema_init)r*   r   r:   r   r;   r   r'   N)r   r-   	functoolswraps)rS   r+   rQ   r,   r.   R   s   
(r.   c                   @   s   e Zd Zdd					d#dd	Zedefd
dZdedefddZd$ddZd$ddZ	d$ddZ
dddddefddZdddddddeddddfdd Zdddddddeddddfd!d"ZdS )%rM   Nexecution_contextr*   r   rW   Optional[ExecutionContext]r'   c                C   s   |r|| _ d S d S r2   rV   )r*   rW   r+   r+   r,   r-      s   
zSentryAsyncExtension.__init__c                 C   s*   |  | jj}| jjrd| jj|S |S )Nz{}:{})
hash_queryrW   queryoperation_namer)   )r*   
query_hashr+   r+   r,   _resource_name   s   z#SentryAsyncExtension._resource_namerZ   c                 C   s   t |d S )Nzutf-8)hashlibmd5encode	hexdigest)r*   rZ   r+   r+   r,   rY      s   zSentryAsyncExtension.hash_queryGenerator[None, None, None]c                 c   sT   | j j| _d}tj}| j jd u rd| j _| j j dr$d}tj}n| j j dr2d}tj	}|}| jr?|d
| j7 }tjd| j|dd t }t| j }|| tj||tjd	| _| j  | jd
| | jd| j | jd| j j | jd| j d V  | jj}|r| j jr| j j|_tj|_||_| jd d d  d S )NrZ    mutationsubscriptionz {}zgraphql.operation)r[   operation_type)categorydataopnamer8   zgraphql.operation.typezgraphql.operation.namezgraphql.documentzgraphql.resource_name)rW   r[   _operation_namer   GRAPHQL_QUERYrZ   strip
startswithGRAPHQL_MUTATIONGRAPHQL_SUBSCRIPTIONr)   rE   add_breadcrumbget_isolation_scope_make_request_event_processoradd_event_processor
start_spanr#   r8   graphql_span	__enter__set_datar]   containing_transactionrk   r
   	COMPONENTsourcerj   __exit__)r*   rf   rj   descriptionscopeevent_processortransactionr+   r+   r,   on_operation   sR   




z!SentryAsyncExtension.on_operationc                 c   .    | j jtjdtjd| _d V  | j  d S )N
validationri   )rw   start_childr   GRAPHQL_VALIDATEr#   r8   validation_spanfinishr*   r+   r+   r,   on_validate      z SentryAsyncExtension.on_validatec                 c   r   )Nparsingri   )rw   r   r   GRAPHQL_PARSEr#   r8   parsing_spanr   r   r+   r+   r,   on_parse   r   zSentryAsyncExtension.on_parse_nextz2Callable[[Any, GraphQLResolveInfo, Any, Any], Any]infor   c                 C   s
   t ||S r2   )strawberry_should_skip_tracing)r*   r   r   r+   r+   r,   r      s   
z(SentryAsyncExtension.should_skip_tracingrootr:   r;   c                    s0   |||g|R i |}t |r|I d H }|S r2   r   )r*   r   r   r   r:   r;   resultr+   r+   r,   _resolve   s
   
zSentryAsyncExtension._resolvec              
      s   |  ||r| j|||g|R i |I d H S d|j|j}| jjtjd|t	j
d=}|d|j |d|jj |d| |ddtt|j  | j|||g|R i |I d H W  d    S 1 snw   Y  d S 	Nz{}.{}zresolving {}ri   zgraphql.field_namezgraphql.parent_typezgraphql.field_pathzgraphql.path.)r   r   r)   parent_type
field_namerw   r   r   GRAPHQL_RESOLVEr#   r8   ry   rk   joinmapstrpathas_listr*   r   r   r   r:   r;   
field_pathspanr+   r+   r,   resolve   s    " $zSentryAsyncExtension.resolve)r*   r   rW   rX   r'   N)r'   rb   )r4   r5   r6   r-   r   r   r]   rY   r   r   r   boolr   r   r   r+   r+   r+   r,   rM      s`    


6


rM   c                   @   s.   e Zd Zdddddddeddd	dfd
dZdS )rN   r   z#Callable[[Any, Any, Any, Any], Any]r   r   r   r   r:   r;   r'   c              
   O   s   |  ||r|||g|R i |S d|j|j}| jjtjd|tj	d8}|
d|j |
d|jj |
d| |
ddtt|j  |||g|R i |W  d    S 1 scw   Y  d S r   )r   r)   r   r   rw   r   r   r   r#   r8   ry   rk   r   r   r   r   r   r   r+   r+   r,   r     s   $zSentrySyncExtension.resolveN)r4   r5   r6   r   r   r+   r+   r+   r,   rN     s    rN   c                     s   t jjtjj								 d fdd	} 								 d fd
d}tt								 ddd | t j_|tj_d S )Nr*   r   errorsList[GraphQLError]response_datar   r'   c                       | ||  | || d S r2   r+   r*   r   r   )_sentry_patched_handle_errorsold_async_view_handle_errorsr+   r,   (_sentry_patched_async_view_handle_errors1     z>_patch_views.<locals>._sentry_patched_async_view_handle_errorsc                    r   r2   r+   r   )r   old_sync_view_handle_errorsr+   r,   '_sentry_patched_sync_view_handle_errors7  r   z=_patch_views.<locals>._sentry_patched_sync_view_handle_errorsc                 S   s   |sd S t  }t|}|| t % |D ]}t|t  jtj	ddd\}}t j
||d qW d    d S 1 s<w   Y  d S )NF)typehandled)client_options	mechanism)hint)rE   rs   _make_response_event_processorru   r   r   rF   optionsr#   r7   capture_event)r*   r   r   r   r   erroreventr   r+   r+   r,   r   =  s"   

"z3_patch_views.<locals>._sentry_patched_handle_errors)r*   r   r   r   r   r   r'   N)r   AsyncBaseHTTPView_handle_errorsr   SyncBaseHTTPViewr   r#   )r   r   r+   )r   r   r   r,   r/   -  sD   
r/   rW   r    r"   c                       d fdd}|S )	Nr   r!   r   dict[str, Any]r'   c              
      s   t  X t r0| di }d|d< |ds/d ji} jr# j|d<  jr+ j|d< ||d< nz| d d= W n ttfyB   Y nw W d    | S W d    | S W d    | S 1 s^w   Y  | S )Nrequestgraphql
api_targetrh   rZ   	variablesoperationName)	r   r	   
setdefaultrH   rZ   r   r[   KeyError	TypeError)r   r   request_datarh   rV   r+   r,   inner_  s8   






z,_make_request_event_processor.<locals>.innerr   r!   r   r   r'   r!   r+   )rW   r   r+   rV   r,   rt   \  s   rt   r   r   c                    r   )	Nr   r!   r   r   r'   c                    sZ   t    t r| di }d i|d< W d    | S W d    | S 1 s&w   Y  | S )Ncontextsrh   response)r   r	   r   )r   r   r   r   r+   r,   r   |  s   

z-_make_response_event_processor.<locals>.innerr   r+   )r   r   r+   r   r,   r   y  s   r   r<   List[SchemaExtension]r&   c                 C   s   t | v rdS t| v rdS d S )NTFr?   )r<   r+   r+   r,   rI     s
   rI   r3   )rW   r    r'   r"   )r   r   r'   r"   )r<   r   r'   r&   )@rT   r^   rJ   inspectr   rE   sentry_sdk.constsr   sentry_sdk.integrationsr   r   r   sentry_sdk.integrations.loggingr   sentry_sdk.scoper	   sentry_sdk.tracingr
   sentry_sdk.utilsr   r   r   r   r   ImportErrorr$   r   strawberry.extensionsr   #strawberry.extensions.tracing.utilsr   r   strawberry.httpr   r   strawberry.extensions.tracingr   r@   r   rA   typingr   r   r   r   r   r   r   r   r   r   strawberry.typesr    sentry_sdk._typesr!   r"   r#   r.   rM   rN   r/   rt   r   rI   r+   r+   r+   r,   <module>   sn    
/ 
/

