o
    i                     @   s   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 eeZG dd dZdS )    N)Any)Optional)config)RateSampler)_SPAN_MEASURED_KEY)int_service)	SpanTypes)
get_logger)IntegrationConfig)INTEGRATION)PROXY_REQUEST)LLMObs)Span)tracerc                   @   s"  e Zd ZdZdeddfddZedefddZd	e	defd
dZ
ejd	e	ddfddZd!dedede	fddZ		d"d	e	dee deeef dee deddfddZej		d"d	e	dee deeef dee deddfddZdeeef dee fddZd#dee defdd ZdS )$BaseLLMIntegrationbaseLLMintegration_configreturnNc                 C   s   || _ ttjd| _d S )N)sample_rate)r   r   r   _llmobs_sample_rate_llmobs_pc_sampler)selfr    r   U/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/llmobs/_integrations/base.py__init__   s   zBaseLLMIntegration.__init__c                 C   s   t jS )z5Return whether submitting llmobs payloads is enabled.)r   enabled)r   r   r   r   llmobs_enabled   s   z!BaseLLMIntegration.llmobs_enabledspanc                 C   s   | j sdS | j|S NF)r   r   sample)r   r   r   r   r   is_pc_sampled_llmobs"   s   z'BaseLLMIntegration.is_pc_sampled_llmobsc                 K   s   dS )z.Set default LLM span attributes when possible.Nr   )r   r   kwargsr   r   r   _set_base_span_tags(   s   z&BaseLLMIntegration._set_base_span_tagsFoperation_idsubmit_to_llmobsc           	      K   s   | ddpd| j}|r| jrtjnd}| dptj }tj	||t
d| j||dd}td|j | jd	i |}| |rI|td |td | j|fi | | jrb|t| j |S )
z
        Start a LLM request span.
        Reuse the service of the application since we'll tag downstream request spans with the LLM name.
        Eventually those should also be internal service spans once peer.service is implemented.
        	span_nameNz
{}.requestparent_contextT)child_ofserviceresource	span_typeactivatezCreating LLM span with type %s   r   )getformat_integration_namer   r   LLMr   context_provideractive
start_spanr   r   logdebugr*   _get_base_url_is_instrumented_proxy_url_set_ctx_itemr   
set_metricr   r"   r   )	r   r#   r$   r!   r%   r*   r&   r   base_urlr   r   r   trace-   s(   
	
zBaseLLMIntegration.trace argsr!   response	operationc                 C   sR   | j r| |s
dS z| ||||| W dS  ty(   tjd|dd Y dS w )zYExtract input/output information from the request and response to be submitted to LLMObs.NzHError extracting LLMObs fields for span %s, likely due to malformed dataT)exc_info)r   r    _llmobs_set_tags	Exceptionr4   errorr   r   r=   r!   r>   r?   r   r   r   llmobs_set_tagsM   s   	z"BaseLLMIntegration.llmobs_set_tagsc                 C   s   t  N)NotImplementedErrorrD   r   r   r   rA   ]   s   	z#BaseLLMIntegration._llmobs_set_tagsc                 K   s   d S rF   r   )r   r!   r   r   r   r6   h   s   z BaseLLMIntegration._get_base_urlr:   c                 C   s   |sdS t jp	t }||v S r   )r   _llmobs_instrumented_proxy_urlsset)r   r:   instrumented_proxy_urlsr   r   r   r7   k   s   z-BaseLLMIntegration._is_instrumented_proxy_url)F)Nr<   rF   )__name__
__module____qualname__r/   r
   r   propertyboolr   r   r    abcabstractmethodr"   strr;   listr   dictr   rE   rA   r6   r7   r   r   r   r   r      sT    %



r   )rP   typingr   r   ddtracer   ddtrace._trace.samplerr   ddtrace.constantsr   $ddtrace.contrib.internal.trace_utilsr   ddtrace.extr   ddtrace.internal.loggerr	   %ddtrace.internal.settings.integrationr
   ddtrace.llmobs._constantsr   r   ddtrace.llmobs._llmobsr   ddtrace.tracer   r   rK   r4   r   r   r   r   r   <module>   s"    