o
    iC                  
   @   sr  U 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 eeZi Zeeef ed< dd Z dd Z!e!eed)dedee"e#df ddfddZ$e!edd Z%e!edd Z&e!ed*dd Z'e!ed!d" Z(e!ed#d$ Z)e!ed%d& Z*d'd( Z+dS )+    )Text)Union)IAST)TELEMETRY_INFORMATION_VERBOSITY)TELEMETRY_MANDATORY_VERBOSITY)deduplication)_num_objects_tainted_in_request)
OriginTypeorigin_to_str)_is_iast_debug_enabled)	telemetry)
get_logger)config)TELEMETRY_NAMESPACE_IAST_SPAN_METRICSc                  O   s2   t j }d}tjD ]\}}||kr|  S q
|S )N   )
asm_config_iast_telemetry_report_lvlupperr   METRICS_REPORT_LVLS)argskwargsreport_lvl_name
report_lvllvllvl_name r   Q/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/appsec/_iast/_metrics.pyget_iast_metrics_report_lvl   s   
r   c                    s    fdd}|S )Nc                    s@    t  krz| W S  ty   tjddd Y dd S w dd S )Nz&iast::metrics::error::metric_verbosityT)exc_infoc                   S   s   d S Nr   r   r   r   r   <lambda>'   s    z3metric_verbosity.<locals>.wrapper.<locals>.<lambda>)r   	Exceptionlogwarning)fr   r   r   wrapper!   s   
z!metric_verbosity.<locals>.wrapperr   )r   r(   r   r'   r   metric_verbosity    s   r)   Nmsgexcreturnc                 C   s   t  rtjj| |d dS dS )aU  This was originally implemented to analyze which services were triggering this issue, and we used that insight
    to refactor how IAST creates and destroys context. However, after that refactor, this information no longer
    provides value and only adds noise. So now, those telemetry metrics are only emitted if IAST is in debug mode
    )r+   N)r   r   telemetry_writeradd_error_log)r*   r+   r   r   r   _set_iast_error_metric,   s   r/   c                 C   .   ddl m} tjtjddd|| ff d S )N   r
   zinstrumented.sourcesource_type_taint_trackingr   r   r-   add_count_metricr   r   r2   r   r   r   r   $_set_metric_iast_instrumented_source7      r7   c                   C   s   t jtjdd d S )Nzinstrumented.propagationr1   r   r-   r5   r   r   r   r   r   r   )_set_metric_iast_instrumented_propagation@   s   r:   r1   c                 C   s   t jtjd|d| ff d S )Nzinstrumented.sinkvulnerability_typer9   )r;   counterr   r   r   "_set_metric_iast_instrumented_sinkE      r=   c                 C   r0   )Nr1   r
   zexecuted.sourcer2   r3   r6   r   r   r    _set_metric_iast_executed_sourceL   r8   r?   c                 C   s   t jtjddd| ff d S )Nzexecuted.sinkr1   r;   r9   )r;   r   r   r   _set_metric_iast_executed_sinkU   r>   r@   c                  C   s(   t  } | dkrtjtjd|  d S d S )Nr   zrequest.tainted)r   r   r-   r5   r   r   )total_objects_taintedr   r   r    _set_metric_iast_request_tainted\   s   rB   c                 C   s&   t | tr	t| } | dd} |  S )N._)
isinstancer	   r   replacelower)keyr   r   r   _metric_key_as_snake_casec   s   
rI   r!   )r1   ),typingr   r   ddtrace.appsec._constantsr   r   r   ddtrace.appsec._deduplicationsr   /ddtrace.appsec._iast._iast_request_context_baser   $ddtrace.appsec._iast._taint_trackingr	   r   ddtrace.appsec._iast._utilsr   ddtrace.internalr   ddtrace.internal.loggerr   ddtrace.internal.settings.asmr   r   $ddtrace.internal.telemetry.constantsr   __name__r$   r   dictstrint__annotations__r   r)   BaseExceptiontupler/   r7   r:   r=   r?   r@   rB   rI   r   r   r   r   <module>   sD    	&	




