o
    piQ                     @  s   d Z ddlmZ ddlZddlZddlZddlZddlmZm	Z	 ddlm
Z
 ddlmZmZmZmZmZmZmZ ddlmZ ddlmZ dd	lmZmZmZmZ dd
lmZ ddlmZm Z m!Z! ddl"m#Z#m$Z$ ddl%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z- ddl.m/Z/ ddl0m1Z1m2Z2m3Z3 ddl4m5Z5m6Z6 ddl7m8Z8m9Z9m:Z: ddl;m<Z<m=Z= ddl>m?Z? ddl@mAZA ddlBmCZC ddlDmEZE ddlFmGZG dZHdZIdZJeIeJdZKeeedZLe*e(e'dZMdZNeNZOdZPeQeRZSeeee= ee1 ee2 ee$ f eeTef f ZUG dd  d eZVG d!d" d"eZWd}d)d*ZXd~d,d-ZYdd.d/ZZd~d0d1Z[dd5d6Z\dd8d9Z]							dddJdKZ^		dddNdOZ_		P			dddXdYZ`dd]d^Zadd`daZbddfdgZcddjdkZdddldmZeddpdqZf														dddwdxZgG dydz dzeZhG d{d| d|ehZidS )zF
OpenTelemetry SDK Configurator for Easy Instrumentation with Distros
    )annotationsN)ABCabstractmethod)environ)AnyCallableMappingProtocolSequenceTypeUnion)Literal)set_logger_provider)OTEL_LOGS_EXPORTEROTEL_METRICS_EXPORTEROTEL_PYTHON_ID_GENERATOROTEL_TRACES_EXPORTER)set_meter_provider)LoggerProviderLoggingHandlerLogRecordProcessor)BatchLogRecordProcessorLogRecordExporter)1_OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED OTEL_EXPORTER_OTLP_LOGS_PROTOCOL#OTEL_EXPORTER_OTLP_METRICS_PROTOCOLOTEL_EXPORTER_OTLP_PROTOCOL"OTEL_EXPORTER_OTLP_TRACES_PROTOCOLOTEL_PYTHON_TRACER_CONFIGURATOROTEL_TRACES_SAMPLEROTEL_TRACES_SAMPLER_ARG)MeterProvider)MetricExporterMetricReaderPeriodicExportingMetricReader)
AttributesResource)SpanProcessorTracerProvider_TracerConfiguratorT)BatchSpanProcessorSpanExporter)IdGenerator)Sampler)ResourceAttributes)set_tracer_provider)entry_pointsotlpotlp_proto_grpcotlp_proto_http)grpczhttp/protobuf)tracesmetricslogsrandomopentelemetry_traces_samplerc                   @     e Zd ZdddZdS )	$_ConfigurationExporterSpanProcessorTspan_exporterr+   returnr'   c                 O     d S N )selfr<   argskwargsr@   r@   ]/home/ubuntu/.local/lib/python3.10/site-packages/opentelemetry/sdk/_configuration/__init__.py__call__v      z-_ConfigurationExporterSpanProcessorT.__call__N)r<   r+   r=   r'   __name__
__module____qualname__rE   r@   r@   r@   rD   r;   u       r;   c                   @  r:   )	)_ConfigurationExporterLogRecordProcessorTexporterr   r=   r   c                 O  r>   r?   r@   )rA   rM   rB   rC   r@   r@   rD   rE   |   rF   z2_ConfigurationExporterLogRecordProcessorT.__call__N)rM   r   r=   r   rG   r@   r@   r@   rD   rL   {   rK   rL   selected_componentsSequence[str]entry_point_namestrr=   list[tuple[str, Type]]c              
   C  sx   g }| D ]5}z| |ttt||d f W q ty(   td| d ty9   td| d| dw |S )N)groupnamezRequested entry point 'z' not foundzRequested component 'z' not found in entry point '')appendnextiterr0   loadKeyErrorRuntimeErrorStopIteration)rN   rP   component_implementationsselected_componentr@   r@   rD   _import_config_components   s6   
r_   
str | Nonec                   C     t td S r?   )r   getr   r@   r@   r@   rD   _get_sampler      rc   c                   C  s   t ttS r?   )r   rb   r   _DEFAULT_ID_GENERATORr@   r@   r@   rD   _get_id_generator   rd   rf   c                   C  ra   r?   )r   rb   r   r@   r@   r@   rD   _get_tracer_configurator   rd   rg   exporter_namesignal_type$Literal['traces', 'metrics', 'logs']c                 C  s   | t ttfvr	| S tt| ptt}|s| t krtS | S | }| t kr7|tvr3t	d| dt| S t|}|rI| |krIt
d||  | S )NzUnsupported OTLP protocol 'z' is configuredz<Conflicting values for %s OTLP exporter protocol, using '%s')_EXPORTER_OTLP_EXPORTER_OTLP_PROTO_GRPC_EXPORTER_OTLP_PROTO_HTTPr   rb   _PROTOCOL_ENV_BY_SIGNAL_TYPEr   strip_EXPORTER_BY_OTLP_PROTOCOLr[   _loggerwarning)rh   ri   otlp_protocolexporter_name_by_envr@   r@   rD   _get_exporter_entry_point   s<   

ru   	list[str]c                   sB   t t d}|r|  dkrg S  fdd|dD S )N nonec                   s   g | ]	}t |  qS r@   )ru   ro   ).0	_exporterri   r@   rD   
<listcomp>   s    z'_get_exporter_names.<locals>.<listcomp>,)r   rb   _EXPORTER_ENV_BY_SIGNAL_TYPElowerro   split)ri   namesr@   r{   rD   _get_exporter_names   s   
r   	exportersdict[str, Type[SpanExporter]]id_generatorIdGenerator | NonesamplerSampler | NoneresourceResource | Noneexporter_args_mapExporterArgsMap | Nonespan_processorsSequence[SpanProcessor] | Noneexport_span_processor+_ConfigurationExporterSpanProcessorT | Nonetracer_configurator_TracerConfiguratorT | Nonec                 C  s~   t ||||d}t| |pi }|pt}	|pg }|D ]}
||
 q|  D ]\}}||i }||	|di | q&d S )N)r   r   r   _tracer_configuratorr@   )r(   r/   r*   add_span_processoritemsrb   )r   r   r   r   r   r   r   r   providerexport_processorspan_processor_exporter_classexporter_argsr@   r@   rD   _init_tracing   s$   
r   exporters_or_readers:dict[str, Union[Type[MetricExporter], Type[MetricReader]]]c                 C  sz   g }|pi }|   D ]&\}}||i }t|tr$||di | q
|t|di | q
t||d}t| d S )N)r   metric_readersr@   )r   rb   
issubclassr#   rV   r$   r!   r   )r   r   r   r   r   exporter_or_reader_classr   r   r@   r@   rD   _init_metrics  s   
r   T"dict[str, Type[LogRecordExporter]]setup_logging_handlerboollog_record_processors#Sequence[LogRecordProcessor] | Noneexport_log_record_processor0_ConfigurationExporterLogRecordProcessorT | Nonec                 C  s  t |d}t| |pi }|pt}|pg }|D ]}|| q|  D ]\}	}
||
i }|||
d
i | q#t $ tjdt	d ddl
m} ddlm} ||d}|| W d    n1 sew   Y  |rtdt	 ttj|d	}t | t| d S d S )N)r   ignore)categoryr   )set_event_logger_provider)EventLoggerProvider)logger_providerzThe `OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED` environment variable and the `LoggingHandler` in `opentelemetry-sdk` that it controls are deprecated.Install `opentelemetry-instrumentation-logging` package instead.)levelr   r@   )r   r   r   add_log_record_processorr   rb   warningscatch_warningssimplefilterDeprecationWarningopentelemetry._eventsr   opentelemetry.sdk._eventsr   warnr   loggingNOTSET	getLogger
addHandler_overwrite_logging_config_fns)r   r   r   r   r   r   r   r   log_record_processorr   r   r   r   r   event_logger_providerhandlerr@   r@   rD   _init_logging!  s<   
	


r   r   r   Nonec                   sH   t  d fdd}|t jjt j_|t jjt j_|t jt _d S )N	config_fnr   r=   c                   s    fdd}|S )Nc               	     sX   d}j v rd}j  z | i | W |r! d S d S |r+ w w )NFT)handlersremover   )rB   rC   removed_handler)r   r   rootr@   rD   overwritten_config_fn\  s   
zM_overwrite_logging_config_fns.<locals>.wrapper.<locals>.overwritten_config_fnr@   )r   r   r   r   )r   rD   wrapper[  s   z._overwrite_logging_config_fns.<locals>.wrapper)r   r   r=   r   )r   r   config
fileConfig
dictConfigbasicConfig)r   r   r@   r   rD   r   X  s
   r   tracer_configurator_namec              
   C  s\   | sd S zt |  gdd \}}W |S  ty- } ztd| | W Y d }~d S d }~ww )N"_opentelemetry_tracer_configuratorr   zMUsing default tracer configurator. Failed to load tracer configurator, %s: %s)r_   ro   	Exceptionrq   rr   )r   r   tracer_configurator_implexcr@   r@   rD   _import_tracer_configuratorq  s&   
r   trace_exporter_namesmetric_exporter_nameslog_exporter_namestuple[dict[str, Type[SpanExporter]], dict[str, Union[Type[MetricExporter], Type[MetricReader]]], dict[str, Type[LogRecordExporter]]]c                 C  s   i }i }i }t | dD ]\}}t|tr|||< qt| dt |dD ]\}}t|ttfr5|||< q%t| dt |dD ]\}}t|trO|||< qAt| d|||fS )Nopentelemetry_traces_exporterz is not a trace exporteropentelemetry_metrics_exporterz is not a metric exporteropentelemetry_logs_exporterz is not a log exporter)r_   r   r+   r[   r"   r#   r   )r   r   r   trace_exportersmetric_exporterslog_exportersrh   exporter_implr@   r@   rD   _import_exporters  s>   	





r   sampler_name'Callable[[float | str | None], Sampler]c                 C  s   t |  gtd \}}|S )Nr   )r_   ro   _OTEL_SAMPLER_ENTRY_POINT_GROUP)r   r   sampler_implr@   r@   rD   _import_sampler_factory  s   
r   c              
   C  s   | sd S zMt | }d }| dv r/z
tttd}W n ttfy+   td d}Y nw |}ntt}||}t	|t
sOd| d| d}t| t||W S  tyk } ztd| | W Y d }~d S d }~ww )	N)traceidratioparentbased_traceidratiorw   zGCould not convert TRACES_SAMPLER_ARG to float. Using default value 1.0.g      ?zSampler factory, z, produced output, z, which is not a Sampler.z;Using default sampler. Failed to initialize sampler, %s: %s)r   floatosgetenvr    
ValueError	TypeErrorrq   rr   
isinstancer-   r   )r   sampler_factoryargrater   messager   r@   r@   rD   _import_sampler  s>   


r   id_generator_namer,   c                 C  s6   t |  gdd \} }t|tr| S t|  d)Nopentelemetry_id_generatorr   z is not an IdGenerator)r_   ro   r   r,   r[   )r   id_generator_implr@   r@   rD   _import_id_generator  s   

r   auto_instrumentation_versionlist[str] | Noneresource_attributesAttributes | Nonebool | Nonec              
   C  s  |d u rg }|d u rg }|d u rg }t |td |td |td \}}}|d u r2t }t|}|d u r=t }t|}|d u rCi }| rJ| |tj< |d u rUt }t	|}t
|}t||||||	|
|d t|||d |d u r}ttd  dk}t||||||d d S )	Nr5   r6   r7   )r   r   r   r   r   r   r   r   )r   falsetrue)r   r   r   )r   r   rc   r   rf   r   r.   TELEMETRY_AUTO_VERSIONrg   r   r&   creater   r   r   r   r   ro   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   span_exportersr   r   r   r   r   r   r@   r@   rD   _initialize_components  st   






r   c                   @  s4   e Zd ZdZdZdZdd Zedd Zdd	 Z	dS )
_BaseConfiguratorzAn ABC for configurators

    Configurators are used to configure
    SDKs (i.e. TracerProvider, MeterProvider, Processors...)
    to reduce the amount of manual configuration required.
    NFc                 O  s*   | j d u rtj| g|R i || _ | j S r?   )	_instanceobject__new__)clsrB   rC   r@   r@   rD   r  M  s   
z_BaseConfigurator.__new__c                 K  s   dS )Configure the SDKNr@   rA   rC   r@   r@   rD   
_configureS  s    z_BaseConfigurator._configurec                 K  s   | j di | dS )r  Nr@   )r  r  r@   r@   rD   	configureW  s   z_BaseConfigurator.configure)
rH   rI   rJ   __doc__r  _is_instrumentedr  r   r  r	  r@   r@   r@   rD   r  B  s    
r  c                   @  s   e Zd ZdZdd ZdS )_OTelSDKConfiguratora  A basic Configurator by OTel Python for initializing OTel SDK components

    Initializes several crucial OTel SDK components (i.e. TracerProvider,
    MeterProvider, Processors...) according to a default implementation. Other
    Configurators can subclass and slightly alter this initialization.

    NOTE: This class should not be instantiated nor should it become an entry
    point on the `opentelemetry-sdk` package. Instead, distros should subclass
    this Configurator and enhance it as needed.
    c                 K  s   t di | d S )Nr@   )r   r  r@   r@   rD   r  h  s   z_OTelSDKConfigurator._configureN)rH   rI   rJ   r
  r  r@   r@   r@   rD   r  \  s    r  )rN   rO   rP   rQ   r=   rR   )r=   r`   )r=   rQ   )rh   rQ   ri   rj   )ri   rj   r=   rv   )NNNNNNN)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )NN)r   r   r   r   r   r   )NTNNN)r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r=   r   )r   r`   r=   r   )r   rO   r   rO   r   rO   r=   r   )r   rQ   r=   r   )r   r`   r=   r   )r   rQ   r=   r,   )NNNNNNNNNNNNNN)r   r`   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )jr
  
__future__r   r   logging.configr   r   abcr   r   r   typingr   r   r   r	   r
   r   r   typing_extensionsr   opentelemetry._logsr   #opentelemetry.environment_variablesr   r   r   r   opentelemetry.metricsr   opentelemetry.sdk._logsr   r   r   opentelemetry.sdk._logs.exportr   r   'opentelemetry.sdk.environment_variablesr   r   r   r   r   r   r   r    opentelemetry.sdk.metricsr!    opentelemetry.sdk.metrics.exportr"   r#   r$   opentelemetry.sdk.resourcesr%   r&   opentelemetry.sdk.tracer'   r(   r)   opentelemetry.sdk.trace.exportr*   r+   $opentelemetry.sdk.trace.id_generatorr,    opentelemetry.sdk.trace.samplingr-   opentelemetry.semconv.resourcer.   opentelemetry.tracer/   &opentelemetry.util._importlib_metadatar0   rk   rl   rm   rp   r~   rn   _RANDOM_ID_GENERATORre   r   r   rH   rq   rQ   ExporterArgsMapr;   rL   r_   rc   rf   rg   ru   r   r   r   r   r   r   r   r   r   r   r   r  r  r@   r@   r@   rD   <module>   s   $(




!



,$
7


3
	
!T