o
    i                     @   s   d dl mZ d dlZd dlmZ d dlZd dlmZ d dl	m
Z
 d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZ d dlmZ ddlmZ ddlmZ ddlmZ eeZdZ dd Z!dd Z"dd Z#dd Z$dS )    )HTTPExceptionN)asboolconfig)	SpanTypes)core)
is_wrapted)	COMPONENT)
get_logger)schematize_service_name)schematize_url_operation)SpanDirection)tracer   )SETTINGS_DISTRIBUTED_TRACING)SETTINGS_SERVICE)SETTINGS_TRACE_ENABLEDz+ddtrace.contrib.pyramid:trace_tween_factoryc                 C   s   |  d d S )Nzddtrace.contrib.pyramid)includer    r   Z/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/contrib/internal/pyramid/trace.pytrace_pyramid   s   r   c                 C   s6   | j ttjjd ttjjjst	
ddt d S d S )N)overzpyramid.rendererszRendererHelper.render)	add_tweenDD_TWEEN_NAMEpyramidtweensEXCVIEWr   	renderersRendererHelperrenderwraptwrap_function_wrappertrace_renderr   r   r   r   	includeme!   s   r#   c                 C   sz   | di }|std | |i |S tjdtjd}|tt	j
j | |i |W  d    S 1 s6w   Y  d S )Nrequestz/No request passed to render, will not be tracedzpyramid.render)	span_type)getlogdebugr   tracer   TEMPLATE_set_tag_strr	   r   r   integration_name)funcinstanceargskwargsr$   spanr   r   r   r"   )   s   
$r"   c                    sX   |j }|tptdt|ttj}t|tdt	j
_|r* fdd}|S  S )Nr   Tc                    s  t jdtddtjdtjdi | jtj	ddd
}|j
}|d| t d|tj	f d }d }z]z | }W n tyF } z|} d }~w tyO   d	} w W | jrfd
| j| jj|_|d| jj |ro|j}|j}nd }t d|tj	| j| j|| j| j|| jr| jjnd df
 n=| jrd
| j| jj|_|d| jj |r|j}|j}nd }t d|tj	| j| j|| j| j|| jr| jjnd df
 w |W  d    W  d    S 1 sw   Y  W d    d S 1 sw   Y  d S )Nzpyramid.requesthttp)protocol	direction404T)		span_namer%   serviceresourcetagsdistributed_headersintegration_configactivate_distributed_headersheaders_case_sensitivereq_spanzweb.request.starti  z{} {}zpyramid.route.namezweb.request.finishF)r   context_with_datar   r   INBOUNDr   WEBheadersr   r   r1   set_itemdispatchr   BaseExceptionmatched_routeformatmethodnamer8   r+   status_codepath_urlquery_stringpattern)r$   ctxr>   responsestatuseresponse_headershandlerr7   r   r   trace_tweenA   s   Rz(trace_tween_factory.<locals>.trace_tween)settingsr&   r   r   r   r   r   enabledr   r   r   distributed_tracing_enabled)rT   registryrV   rW   rU   r   rS   r   trace_tween_factory6   s   BrZ   )%pyramid.httpexceptionsr   pyramid.renderersr   pyramid.settingsr   r    ddtracer   ddtrace.extr   ddtrace.internalr   ddtrace.internal.compatr   ddtrace.internal.constantsr	   ddtrace.internal.loggerr
   ddtrace.internal.schemar   r   -ddtrace.internal.schema.span_attribute_schemar   ddtrace.tracer   	constantsr   r   r   __name__r'   r   r   r#   r"   rZ   r   r   r   r   <module>   s.    