o
    iQS                  
   @   s6  d dl mZ d dl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 d dlmZ d dlmZ z
d dlmZ dZ W n e!y}   dZ Y nw 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l0m1Z1 d dl2m3Z3 ddl4m5Z5 ddl4m6Z6 dd l4m7Z7 dd!l4m8Z8 dd"l4m9Z9 dd#l4m:Z: zd d$l;m<Z< W n e!y   e=Z<Y nw e.e>Z?d%Z@h d&ZAe&Bd'eCed'ddd(dd) d*eDfd+d,ZEd*eCeDeDf fd-d.ZFd*eDfd/d0ZGe r.G d1d2 d2ejHeZIeIZJnejHZJeE ZKe3eKZLeG ZMe3eMZNG d3d4 d4e,ZOd5d6 ZPd7d8 ZQe8d9d: ZRd;d< ZSd=d> ZTd?d@ ZUdAdB ZVdCdD ZWdEdF ZXdGdH ZYdIdJ ZZdKdL Z[dMdN Z\dOdP Z]dQdR Z^dSdT Z_dUdV Z`dWdX ZadYdZ Zbd[d\ ZcdS )]    )unwrapN)
BadRequest)NotFound)abort)trace_utils)	SpanTypes)core)	COMPONENT)endpoint_collectionget_version_for_package)schematize_service_name)schematize_url_operation)SpanDirection)get_blocked)http)set_blocked)	JSONMixinTF)wrap_function_wrapper)config)Pin)_DDWSGIMiddlewareBase)
get_logger)get_argument_value)	func_name)parse_version   )_wrap_call_with_pin_check)get_current_app)simple_call_wrapperwith_instance_pinwrap_function)	wrap_view)JSONDecodeErrorzflask.version>   PUTPOSTPATCHDELETEflaskz<memory>)_default_servicecollect_view_argsdistributed_tracing_enabledtemplate_default_nametrace_signalsreturnc                   C      t dS )Nr*   r    r2   r2   X/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/contrib/internal/flask/patch.pyget_versionL      r4   c                   C   s   ddiS )Nr*   z>=1.1.4r2   r2   r2   r2   r3   _supported_versionsP   r5   r6   c                   C   r1   )Nwerkzeugr   r2   r2   r2   r3   get_werkzeug_versionT   r5   r8   c                   @   s   e Zd ZdS )RequestWithJsonN)__name__
__module____qualname__r2   r2   r2   r3   r9   Z   s    r9   c                   @   s8   e Zd ZeddejdZdZdZdddZ	dd	d
Z
dS )_FlaskWSGIMiddlewareflask.requestr   )protocol	directionzflask.applicationzflask.responseNc                 C   s   t dtj|tj||f t s\t dd t  }rUt ddj}|r2|j\}}	}
|t	|	|
}n|j
dkr9g nd|jfg}
|t	|j|
}t d|tj|
|jf |S |||}|S |||}|S )	Nzflask.start_response.prezflask.start_response)Flaskzflask.block.request.contentr2   nonezcontent-typezflask.start_response.blocked)r   dispatchr*   requestr   r   dispatch_with_resultsblock_requestedvaluestrtypecontent_typestatus_code)selfstart_responsectxrK   headersexc_infoblock_configresult_content_statusresponse_headersresultr2   r2   r3   _wrapped_start_responsey   s.   


z,_FlaskWSGIMiddleware._wrapped_start_responsec                 C   sZ   | d}t|}d }td|tj||tttt	fj
}|r |j}td|tj||f d S )Nenvironzflask.request_call_modifierz flask.request_call_modifier.post)get_item_RequestTyper   rE   r   r*   _HAS_JSON_MIXINFLASK_VERSIONflask_version_strr   request_bodyrG   rC   )rL   rN   parsed_headersrX   rD   req_bodyrV   r2   r2   r3   _request_call_modifier   s(   
z+_FlaskWSGIMiddleware._request_call_modifierN)r:   r;   r<   r   r   INBOUND_request_call_name_application_call_name_response_call_namerW   ra   r2   r2   r2   r3   r=   t   s    
r=   c                  C   s  t tddrdS dt_t tj tdtf t	ddt
 t	ddtd	 t	dd
td t	ddt t	ddt t	ddt tdkrLt	ddt nt	ddt t	ddt t	ddt g d} tdk rk| d | D ]}t	dd|t qmt	ddt g d}tdk r|d |D ]}t	dd|td| qt	ddtd t	ddt t	d d!t t	dd"td" t	dd#td# td$krzt	d%d&t W n% ty   td' Y nw zt	d%d(t W n ty   td) Y nw g d*}tdk r|d+ |D ]}t	dd,|t qtjd- rYg d.}td/kr&|d0 td1kr:|d2 |d3 |d4 |D ]}d}td5krL|d6krLd7}t	|d8|t | q<dS dS )9z*
    Patch `flask` module for tracing
    _datadog_patchFNTzflask.patchr*   Flask.wsgi_appFlask.dispatch_requestdispatch_requestFlask.preprocess_requestpreprocess_requestFlask.add_url_ruleFlask.endpointFlask.finalize_request   r   r   Flask.register_error_handlerFlask._register_error_handlerBlueprint.registerBlueprint.add_url_rule)before_requestafter_requestteardown_requestteardown_appcontextrq      r   before_first_requestzFlask.{}after_this_request)process_responsehandle_exceptionhandle_http_exceptionhandle_user_exceptiondo_teardown_requestdo_teardown_appcontextsend_static_filerq   rq   r   *try_trigger_before_first_request_functionszflask.{}	send_filezflask.send_filejsonifyzflask.templating_renderrender_templaterender_template_string)rq   r   r   zwerkzeug.debug.tbtoolsz#DebugTraceback.render_debugger_htmlz[Failed to patch DebugTraceback.render_debugger_html, not supported by this werkzeug versionzTraceback.render_summaryzPFailed to patch Traceback.render_summary, not supported by this werkzeug version)after_app_requestrw   before_app_requestrv   rx   teardown_app_requestbefore_app_first_requestzBlueprint.{}r/   )template_renderedrequest_startedrequest_finishedrequest_tearing_downgot_request_exceptionappcontext_tearing_downr      before_render_templater   
   appcontext_pushedappcontext_poppedmessage_flashedr   	   r   zflask.signalsz{}.receivers_for)!getattrr*   rg   r   ontorA   r   rC   flask_version_wpatched_wsgi_apprequest_patcherpatched_add_url_rulepatched_endpointpatched_finalize_requestpatched_register_error_handlerpatched__register_error_handlerpatched_blueprint_registerpatched_blueprint_add_url_ruleappendformatpatched_flask_hookr   patched_jsonifypatched_render_build_render_template_wrapperwerkzeug_versionpatched_render_debugger_htmlAttributeErrorlogdebugr   patched_signal_receivers_for)flask_hookshookflask_app_tracesnamebp_hookssignalssignalmoduler2   r2   r3   patch   s~   
	



	




r   c                  C   s  t tddsd S dt_g d} | d tdkr| d n| d tdkr,| d	 td
kr?| d | d | d tdk rH| d tdk rV| d | d | D ]'}t}tdkrg|dkrgtj}d|v rz|d\}}}t ||t }t|| qXd S )Nrg   F)$rh   ri   rm   rn   rk   zFlask.process_responsezFlask.handle_exceptionzFlask.handle_http_exceptionzFlask.handle_user_exceptionzFlask.do_teardown_requestzFlask.do_teardown_appcontextzFlask.send_static_filezFlask.before_requestzFlask.after_requestzFlask.teardown_requestzFlask.teardown_appcontextrt   ru   zBlueprint.after_app_requestzBlueprint.after_requestzBlueprint.before_app_requestzBlueprint.before_requestzBlueprint.teardown_requestzBlueprint.teardown_app_requestztemplate_rendered.receivers_forzrequest_started.receivers_forzrequest_finished.receivers_forz"request_tearing_down.receivers_forz#got_request_exception.receivers_for%appcontext_tearing_down.receivers_forr}   r   r   r   r   ztemplating._renderro   rp   rr   rs   r   z$before_render_template.receivers_forr   zappcontext_pushed.receivers_forzappcontext_popped.receivers_forzmessage_flashed.receivers_forr   z0Flask.try_trigger_before_first_request_functionsrz   zFlask.before_first_requestz"Blueprint.before_app_first_requestr   r   .)	r   r*   rg   r   r   r   	partitionobject_u)propspropobjattrrS   r2   r2   r3   unpatch'  s8   
-







r   c                 C   s"   |\}}t |d tj| }|||S rb   )r=   r   r*   )pinwrappedinstanceargskwargsrX   rM   
middlewarer2   r2   r3   r     s   
r   c                 C   s2   | |i |}t |ddrtd|j|jf |S )z6
    Wrapper for flask.app.Flask.finalize_request
    is_sequenceFzflask.finalize_request.post)r   r   rC   responserO   )r   r   r   r   rvr2   r2   r3   r     s   r   c                 C   sF   t ||dd}t|}|st|}|r| | | |i |S )z
    Wrapper for flask.blueprints.Blueprint.register

    This wrapper just ensures the blueprint has a pin, either set manually on
    itself from the user or inherited from the application
    r   app)r   r   get_fromcloner   )r   r   r   r   r   r   r2   r2   r3   r     s   

r   c                    s<   t |  s|i |S d fdd	}||i |S )Nc                    s(   |r	   | | f||d|S )N)endpoint	view_func)r   r   )ruler   r   r   r   r   r2   r3   _wrap  s   z-patched_blueprint_add_url_rule.<locals>._wrap)NN)r   _findr   r   r   r   r   r2   r   r3   r     s
   r   c                 C   s    | |i |}t d|f |S )Nzwerkzeug.render_debugger_html)r   rC   )r   r   r   r   resr2   r2   r3   r     s   r   c                    s   d fdd	}||i |S )zOWrapper for flask.app.Flask.add_url_rule to wrap all views attached to this appNc                    s   d }|d urt dt|f t ||| d}|ddg}zt|}W n ty0   dg}Y nw |D ]
}tj|| dd q3| f|||d|S )Nservice_entrypoint.patch)r   resourcemethodsGETr>   )operation_name)r   r   provide_automatic_options)	r   rC   r   r$   getiter	Exceptionr
   add_endpoint)r   r   r   r   r   wrapped_viewr   methodr   r   r2   r3   r     s*   
z#patched_add_url_rule.<locals>._wrap)NNNr2   r   r2   r   r3   r     s   r   c                    s$   | d|d   fdd}|S )zHWrapper for flask.app.Flask.endpoint to ensure all endpoints are wrappedr   r   c                    s(   t dt| f  t|  dS )Nr   )r   )r   rC   r   r#   )funcr   r   r   r2   r3   _wrapper  s   z"patched_endpoint.<locals>._wrapper)r   )r   r   r   r   r   r2   r   r3   r     s   r   c                 C   s   t ||dd}| t||S )Nr   f)r   r#   )r   r   r   r   r   r2   r2   r3   r     s   r   c                 C      t d| |||S )Nr   r   r   r   r   r   r2   r2   r3   traced_render_template     r   c                 C   r   )Nr   r   r   r2   r2   r3   traced_render_template_string  r   r   c                    s   d    fdd}|S )Nzflask.%sc              	      s   t | |t }|r| s| |i |S tjd |tjttjj	it
jd*}|j | |i |W  d    W  d    S 1 sCw   Y  W d    d S 1 sSw   Y  d S )Nzflask.render_template)	span_namer   flask_configtags	span_type)r   r   r   enabledr   context_with_datar   r*   r	   integration_namer   TEMPLATEspanr   r   r   r   r   rN   r   r2   r3   traced_render  s"   
Rz5_build_render_template_wrapper.<locals>.traced_renderr2   )r   r  r2   r  r3   r     s   r   c                    sB   t |t }|js i S  fdd}| i S )Nc                    s    t d| tjf  i S )Nzflask.render)r   rC   r   r*   )templatecontextr   r   r   r   r2   r3   r     s   zpatched_render.<locals>._wrap)r   r   r   r   )r   r   r   r   r   r   r2   r  r3   r     s
   r   c                        fdd}||i |S )Nc                    s   | |t  |S rb   r"   )keycode_or_exceptionr   r   r2   r3   r     r   z.patched__register_error_handler.<locals>._wrapr2   r   r2   r   r3   r        r   c                    r  )Nc                    s   | t  |S rb   r"   )r  r   r   r2   r3   r     s   z-patched_register_error_handler.<locals>._wrapr2   r   r2   r   r3   r     r	  r   c                 C   sV   t   td| f t }|r|jnd}|r|jnd}ttjt	
|||dd d S )Nzflask.blocked_request_callablezapplication/jsonz	(default)i  )rJ   rT   )r   r   rC   r   rJ   block_idr   r*   Response
http_utils_get_blocked_template)callrQ   ctyper
  r2   r2   r3   _block_request_callable"  s    r  c                    s   t  fdd}|S )Nc                    s   t jddd f| t| tj| tjtjtt	t
tjjid	1}|j t d|f ||i |W  d    W  d    S 1 sCw   Y  W d    d S 1 sSw   Y  d S )Nzflask._patched_requestr   r*   )r   r   servicer   flask_requestblock_request_callableignored_exception_typer   )r   r   joinr   int_servicer   r*   rD   r  r   r	   r   r   rC   )r   r   r   r   r   rN   r  r2   r3   _patched_request,  s$   
Rz)request_patcher.<locals>._patched_requestr    )r   r  r2   r  r3   r   +  s   r   c                    s    fdd}|S )Nc                 3   sR    t ||dd}d }t|tjr|}| |i |D ]}t||t| dV  qd S )Nr   senderr   )r   
isinstancer*   rA   r   r   )r   r   r   r   r  r   receiverr  r2   r3   outerC  s   z+patched_signal_receivers_for.<locals>.outerr2   )r   r  r2   r  r3   r   B  s   	r   c              	   C   s   t | |t }|r| s| |i |S tjddtjttjj	i|d*}|j
 | |i |W  d    W  d    S 1 sAw   Y  W d    d S 1 sQw   Y  d S )Nzflask.jsonify)r   r   r   r   )r   r   r   r   r   r   r   r*   r	   r   r   r   r2   r2   r3   r   O  s    

Rr   )dinspectr   r*   r7   werkzeug.exceptionsr   r   r   ddtrace.contribr   ddtrace.extr   ddtrace.internalr   ddtrace.internal.constantsr	   ddtrace.internal.endpointsr
   ddtrace.internal.packagesr   ddtrace.internal.schemar   r   -ddtrace.internal.schema.span_attribute_schemar   ddtrace.internal.utilsr   r   r  r   werkzeug.wrappers.jsonr   r[   ImportErrorwraptr   r   ddtracer   ddtrace._trace.pinr   $ddtrace.contrib.internal.trace_utilsr   "ddtrace.contrib.internal.wsgi.wsgir   ddtrace.internal.loggerr   r    ddtrace.internal.utils.importlibr   ddtrace.internal.utils.versionr   wrappersr   r   r   r!   r#   r$   jsonr%   
ValueErrorr:   r   r\   _BODY_METHODS_adddictrH   r4   r6   r8   Requestr9   rZ   r]   r   werkzeug_version_strr   r=   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r   r   r   r2   r2   r2   r3   <module>   s    8{]
	
	