o
    i4                  	   @  sL  d dl mZ d dlZd dlZd dlZd dlZd dlZd dl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mZ d d	lmZ d d
lmZ d dlmZ d dlZddhZi ddddddddddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/i d0d1d2d3d4d5d6d7d8d9d:d9d;d<d=d>d?d@dAdBdCdDdEdFdGdHdIdJdKddLdMdNdOdPddQdRdSdTdUdVZ eG dWdX dXZ!dd]d^Z"eddddeZ#ddgdhZ$ddmdnZ%ddpdqZ&ddudvZ'ddydzZ(dd}d~Z)dddddZ*dddZ+dddZ,dddZ-dS )    )annotationsN)	Generator)contextmanager)	dataclass)cast)ROUNDED)ConsoleGroup)Panel)Rule)Texturllibsqlite3z&opentelemetry-instrumentation-aio_pikaaio_pikaz,opentelemetry-instrumentation-aiohttp-clientaiohttp_clientz,opentelemetry-instrumentation-aiohttp-serveraiohttp_serverz#opentelemetry-instrumentation-aiopgaiopgz%opentelemetry-instrumentation-asyncpgasyncpgz"opentelemetry-instrumentation-botobotoz&opentelemetry-instrumentation-botocorebotocorez$opentelemetry-instrumentation-celeryceleryz-opentelemetry-instrumentation-confluent-kafkaconfluent_kafkaz$opentelemetry-instrumentation-djangodjangoz+opentelemetry-instrumentation-elasticsearchelasticsearchz$opentelemetry-instrumentation-falconfalconz%opentelemetry-instrumentation-fastapifastapiz#opentelemetry-instrumentation-flaskflaskz"opentelemetry-instrumentation-grpcgrpcz#opentelemetry-instrumentation-httpxhttpxz$opentelemetry-instrumentation-jinja2jinja2z*opentelemetry-instrumentation-kafka-pythonkafka_pythonz#opentelemetry-instrumentation-mysqlmysqlz)opentelemetry-instrumentation-mysqlclientmysqlclientz"opentelemetry-instrumentation-pikapikaz%opentelemetry-instrumentation-psycopgpsycopgz&opentelemetry-instrumentation-psycopg2z(opentelemetry-instrumentation-pymemcache
pymemcachez%opentelemetry-instrumentation-pymongopymongoz%opentelemetry-instrumentation-pymysqlpymysqlz%opentelemetry-instrumentation-pyramidpyramidz#opentelemetry-instrumentation-redisredisz'opentelemetry-instrumentation-remoulade	remouladez&opentelemetry-instrumentation-requestsrequestsz(opentelemetry-instrumentation-sqlalchemy
sqlalchemyz%opentelemetry-instrumentation-sqlite3'opentelemetry-instrumentation-starlette	starlettez%opentelemetry-instrumentation-tornadotornadotortoise_ormurllib3pydantic_ai	anthropicopenaiopenai_agents)z)opentelemetry-instrumentation-tortoiseormz$opentelemetry-instrumentation-urllib%opentelemetry-instrumentation-urllib3zpydantic-ai-slimr3   r4   zopenai-agentsc                   @  s.   e Zd ZU ded< ded< ded< ded< dS )	InstrumentationContextdict[str, str]instrument_pkg_mapset[str]installed_pkgsinstalled_otel_pkgsset[tuple[str, str]]recommendationsN)__name__
__module____qualname____annotations__ rC   rC   _/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/logfire/_internal/cli/run.pyr7   I   s
   
 r7   argsargparse.NamespacereturnNonec                 C  s  t   tt| j}ttt | j}t|}t	|j
|j}|r5ttjd}t|j
||j}t|||jd | j}tjdt  | j }rw|}	td| g|	 d| dd|	  tj|ddd	 W d    d S 1 spw   Y  d S |r|d }
tjtj|
}|tjvrtjd| t|d
d|  tj|
dd W d    d S 1 sw   Y  d S t d t!d d S )N)file)consoleinstrumented_packages_textr>   r   z-m z
python -m  __main__T)run_name	alter_syszpython )rN   zHUsage: logfire run [-m MODULE] [args...] OR logfire run SCRIPT [args...]   )"logfire	configurer   boolsummarysetstrexcludecollect_instrumentation_contextinstrument_packagesr<   r9   r   sysstderrrK   r;   print_otel_summaryr>   script_and_argspathinsertosgetcwdmodulealter_sys_argvjoinrunpy
run_moduledirnameabspathrun_pathprintexit)rE   rT   rW   ctxinstrumented_packagesrJ   instrumentation_textr]   module_namemodule_argsscript_path
script_dirrC   rC   rD   	parse_runQ   s<   

*"
"rs   argv	list[str]cmdrV   Generator[None, None, None]c                 c  s<    t j }| t _ztjd|d d V  W |t _d S |t _w )NzRunning command: {cmd_str})cmd_str)rZ   rt   copyrQ   info)rt   rv   	orig_argvrC   rC   rD   rc      s   
rc   rS   c                   C  s   t dduS )zCCheck if uv package manager is installed and available in the PATH.uvN)shutilwhichrC   rC   rC   rD   is_uv_installed   s   r   installed_otel_packagesr:   r9   r8   c              	   C  sl   g }t jdd t jdd | D ]!}|dd}|| }zt| W n	 ty-   Y qw || q|S )zCall every `logfire.instrument_x()` we can based on what's installed.

    Returns a list of packages that were successfully instrumented.
    LANGSMITH_OTEL_ENABLEDtrueLANGSMITH_TRACING_ENABLEDopentelemetry-instrumentation- )r`   environ
setdefaultreplaceinstrument_package	Exceptionappend)r   r9   instrumentedotel_pkg_namebase_pkgimport_namerC   rC   rD   rY      s   rY   r   c                 C  s   d|  }t t|  d S )Ninstrument_)getattrrQ   )r   instrument_attrrC   rC   rD   r      s   
r   r<   r;   r=   c                 C  sh   t  }|  D ]\}}||v rq||v s|tv r|||f qd|v r)|d d|v r2|d |S )a  Determine which OpenTelemetry instrumentation packages are recommended for installation.

    Args:
        instrument_pkg_map: Mapping of instrumentation package names to the packages they instrument.
        installed_otel_pkgs: Set of already installed instrumentation package names.
        installed_pkgs: Set of all installed package names.

    Returns:
        Set of tuples where each tuple is (instrumentation_package, target_package) that
            is recommended for installation.
    r   )r-   r.   r+   )r6   r1   )rU   itemsSTANDARD_LIBRARY_PACKAGESadddiscard)r9   r<   r;   r>   otel_pkgrequired_pkgrC   rC   rD   ,find_recommended_instrumentations_to_install   s   

r   rm   r   c                 C  s   d|v r	|  d d|v r|  d td}t| D ]#}|dd}||v r2|jd| d	d
d q|jd| ddd q|S )Nr   r-   r+   r6   z!Your instrumentation checklist:

r   r   u   ✓ z (installed and instrumented)
greenstyleu   ⚠️ z0 (installed but not automatically instrumented)
yellow)r   r   sortedr   r   )r<   rm   r;   textpkg_namer   rC   rC   rD   rK      s   

rK   r>   tuple[Text, Text]c                 C  sz   t | }t }|D ]\}}|jd| d| ddd q	|d t }| r9|d |jt|dd |d ||fS )	zEReturn (recommended_packages_text, install_all_text) as Text objects.u   ☐ z (need to install z)
grey50r   
z3To install all recommended packages at once, run:

bold)r   r   r   _full_install_command)r>   sorted_recommendationsrecommended_textr   instrumented_pkginstall_textrC   rC   rD   get_recommendation_texts   s   


r   )rK   rJ   r   Text | Nonec           
      C  s   t ddd}|jddd |jddd tdd}t|\}}|r&|| }n|}t||||}t|ddtd	d
}	| d | |	 |   d S )Nz 
To hide this summary box, use: italicr   zlogfire run --no-summaryzitalic bold.bluez&[bold blue]Logfire Summary[/bold blue])rP      )titleborder_styleboxpaddingr   )r   r   r   r   r	   r
   r   rj   )
rJ   rK   r>   	hide_notefooter_rulerecommended_packages_textinstall_all_textpackages_textcontentpanelrC   rC   rD   r\      s0   



r\   c                  C  s   zdd t j D W S  ttfy@   t  tjdtd ddl	} dd | j
D W  d    Y S 1 s8w   Y  Y dS w )z$Get a set of all installed packages.c                 S  s   h | ]	}|j d   qS )Name)metadatalower).0distrC   rC   rD   	<setcomp>,  s    z%installed_packages.<locals>.<setcomp>ignore)categoryr   Nc                 S  s   h | ]}|j qS rC   )keyr   pkgrC   rC   rD   r   3  s    )	importlibr   distributionsImportErrorAttributeErrorwarningscatch_warningsfilterwarningsUserWarningpkg_resourcesworking_set)r   rC   rC   rD   installed_packages(  s   
*r   list[tuple[str, str]]c                 C  s<   | sdS dd | D }t  rdd| S dd| S )z?Generate a command to install all recommended packages at once.r   c                 S  s   g | ]\}}|qS rC   rC   )r   r   _rC   rC   rD   
<listcomp>;  s    z)_full_install_command.<locals>.<listcomp>zuv add rL   zpip install )r   rd   )r>   package_namesrC   rC   rD   r   6  s   r   rW   c                   sN    fddt  D }t fdd| D }t||}t|||dS )zFCollects all relevant context for instrumentation and recommendations.c                   s   i | ]\}}| vr||qS rC   rC   )r   r   r   )rW   rC   rD   
<dictcomp>G  s    z3collect_instrumentation_context.<locals>.<dictcomp>c                   s   h | ]}| v r|qS rC   rC   r   )r;   rC   rD   r   I  s    z2collect_instrumentation_context.<locals>.<setcomp>)r9   r;   r<   r>   )OTEL_INSTRUMENTATION_MAPr   r   keysr   r7   )rW   r9   r<   r>   rC   )rW   r;   rD   rX   E  s   rX   )rE   rF   rG   rH   )rt   ru   rv   rV   rG   rw   )rG   rS   )r   r:   r9   r8   rG   ru   )r   rV   )r9   r8   r<   r:   r;   r:   rG   r=   )r<   r:   rm   ru   r;   r:   rG   r   )r>   r=   rG   r   )rJ   r   rK   r   r>   r=   rG   rH   )rG   r:   )r>   r   rG   rV   )rW   r:   rG   r7   ).
__future__r   _annotationsargparser   importlib.metadatar`   re   r}   rZ   r   collections.abcr   
contextlibr   dataclassesr   typingr   rich.boxr   rich.consoler   r	   
rich.panelr
   	rich.ruler   	rich.textr   rQ   r   r   r7   rs   rc   r   rY   r   r   rK   r   r\   r   r   rX   rC   rC   rC   rD   <module>   s    	
 !"#.
3





%

-
