o
    i                  	   @   s  d dl Z d dlmZ d dlm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 eeZd
Zedd ejjddd D Zdadd ZdefddZdee fddZdee de de dee fddZ!dd Z"dd Z#dS )     N)Any)Optional)config)get_hostname)
get_logger)otel_config)telemetry_writer)TELEMETRY_NAMESPACE)      r   c                 c       | ]}t |V  qd S Nint.0x r   Z/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/internal/opentelemetry/metrics.py	<genexpr>       r   .   Fc                  C   sP   t  sdS t } | du rdS tjj}t|}|du rdS t||| s$dS dadS )zDSet up the OpenTelemetry Metrics exporter if not already configured.NT)"_should_configure_metrics_exporter_build_resourcer   exporterMETRICS_PROTOCOL_import_exporter_initialize_metricsDD_METRICS_PROVIDER_CONFIGURED)resourceprotocolexporter_classr   r   r   set_otel_meter_provider   s   r#   returnc               
   C   s   t r	td dS ttk r&tdddd tD ddd tD  dS zdd	lm}  | d
ur9td W dS W n tyU } ztdt	| W Y d
}~dS d
}~ww t
d dS )zACheck if the OpenTelemetry Metrics exporter should be configured.zQOpenTelemetry Metrics exporter was already configured by ddtrace, skipping setup.FzOpenTelemetry API requires version %s or higher to enable metrics collection. Found version %s. Please upgrade the opentelemetry-api package before enabling ddtrace OpenTelemetry Metrics support.r   c                 s   r   r   strr   r   r   r   r   7   r   z5_should_configure_metrics_exporter.<locals>.<genexpr>c                 s   r   r   r%   r   r   r   r   r   8   r   r   )_METER_PROVIDERNzSOpenTelemetry Metrics provider was configured before ddtrace setup, skipping setup.z3OpenTelemetry Metrics support is not available: %s.zPOpenTelemetry Metrics exporter is not configured, proceeding with ddtrace setup.T)r   logwarningAPI_VERSIONMINIMUM_SUPPORTED_VERSIONjoinopentelemetry.metrics._internalr'   ImportErrorr&   debug)meter_providerer   r   r   r   -   s4   


r   c                  C   s~   z/ddl m}  i tjtjtjtjd}tjr!d|vr!t |d< dd |	 D }| 
|W S  ty>   td Y dS w )	zKBuild an OpenTelemetry Resource using DD_TAGS and OTEL_RESOURCE_ATTRIBUTES.r   )Resource)zservice.namezservice.versionzdeployment.environmentz	host.namec                 S   s&   i | ]\}}||d urt |ndqS )N r%   )r   kvr   r   r   
<dictcomp>]   s   & z#_build_resource.<locals>.<dictcomp>zOpenTelemetry SDK is not installed, opentelemetry metrics will not be enabled. Please install the OpenTelemetry SDK before enabling ddtrace OpenTelemetry Metrics support.N)opentelemetry.sdk.resourcesr2   r   tagsserviceversionenv_report_hostnamer   itemscreater.   r(   r)   )r2   resource_attributesr   r   r   r   N   s&   
r   otel_exporterr!   encodingc                    s   G  fddd| }|S )zZCreate a custom OpenTelemetry Metrics exporter that adds telemetry metrics and debug logs.c                
       s:   e Zd ZdZdededededef
 fddZ  ZS )	z/_dd_metrics_exporter.<locals>.DDMetricsExporterzSA custom OpenTelemetry Metrics exporter that adds telemetry metrics and debug logs.metrics_datatimeout_millisargskwargsr$   c                    s   t tjdddfdff td t j||g|R i |}|jdks.|jdkrDt tj|jdkr9dndddfdff |S )	z,Export metrics and queues telemetry metrics.zotel.metrics_export_attemptsr
   r!   rA   z@Exporting OpenTelemetry Metrics with %s protocol and %s encodingr   zotel.metrics_export_successeszotel.metrics_export_failures)	r   add_count_metricr	   TRACERSr(   r/   superexportvalue)selfrB   rC   rD   rE   result)	__class__rA   r!   r   r   rI   n   s(   

z6_dd_metrics_exporter.<locals>.DDMetricsExporter.export)__name__
__module____qualname____doc__r   rI   __classcell__r   rA   r!   )rM   r   DDMetricsExporterk   s    .rT   r   )r@   r!   rA   rT   r   rS   r   _dd_metrics_exporterh   s   rU   c              
   C   s   zW| dkrddl m} ddlm} n| dkr#ddlm} ddlm} n	td|  W dS tdd	 |	d
dd D t
k rItd| t
| W dS | dkrOdnd}t||dW S  tyz } ztd| | dkridndt| W Y d}~dS d}~ww )zOImport the appropriate OpenTelemetry Metrics exporter based on the set protocolgrpcr   )OTLPMetricExporter)__version__zhttp/protobufz]OpenTelemetry Metrics exporter protocol '%s' is not supported. Use 'grpc' or 'http/protobuf'.Nc                 s   r   r   r   r   r   r   r   r      r   z#_import_exporter.<locals>.<genexpr>r   r   zOpenTelemetry Metrics exporter for %s requires version %r or higher, but found version %r. Please upgrade the appropriate opentelemetry-exporter package.httpprotobufzOpenTelemetry Metrics exporter for %s is not available. Please install a supported package (ex: opentelemetry-exporter-otlp-proto-%s): %s)6opentelemetry.exporter.otlp.proto.grpc.metric_exporterrW   .opentelemetry.exporter.otlp.proto.grpc.versionrX   6opentelemetry.exporter.otlp.proto.http.metric_exporter.opentelemetry.exporter.otlp.proto.http.versionr(   r)   tuplesplitr+   rU   r.   r&   )r!   rW   exporter_versionprotocol_namer1   r   r   r   r      s@   $r   c              
   C   s   z;ddl m} dtjvrdtjvrtjjtjd< tjjtjd< ttjj	tjd< ttjj
tjd< ||| i|d W d	S  tyV } ztd
t| W Y d}~dS d}~ww )z:Configures and sets up the OpenTelemetry Metrics exporter.r   )_init_metricsOTEL_EXPORTER_OTLP_ENDPOINT#OTEL_EXPORTER_OTLP_METRICS_ENDPOINT1OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCEOTEL_METRIC_EXPORT_INTERVALOTEL_METRIC_EXPORT_TIMEOUT)r    TzThe installed OpenTelemetry SDK is missing required components: %s. Metrics exporter not initialized. Please file an issue at github.com/Datadog/dd-trace-py.NF) opentelemetry.sdk._configurationrc   osenvironr   r   METRICS_ENDPOINTMETRICS_TEMPORALITY_PREFERENCEr&   %METRICS_METRIC_READER_EXPORT_INTERVAL$METRICS_METRIC_READER_EXPORT_TIMEOUTr.   r(   r)   )r"   r!   r    rc   r1   r   r   r   r      s$   r   )$rj   typingr   r   opentelemetry.versionopentelemetryddtracer   ddtrace.internal.hostnamer   ddtrace.internal.loggerr   (ddtrace.internal.settings._opentelemetryr   ddtrace.internal.telemetryr   $ddtrace.internal.telemetry.constantsr	   rN   r(   r+   r_   r:   rX   r`   r*   r   r#   boolr   r   typer&   rU   r   r   r   r   r   r   <module>   s(    $!"%(