o
    i7                     @   s  d 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mZmZ ddlmZ dd	lmZmZ dd
lmZ eZejeef ZeeZ ej!Z!ej"Z"ej#Z#ej$Z$ej%Z%ej&Z&ej'Z'ej(Z(ej)Z)ej*Z*ej+Z+ej,Z,ej-Z-ej.Z.ej/Z/ej0Z0ej1Z1ej2Z2ej3Z4ej5Z6ej7Z8ej9Z:ej;Z<ej=Z>ej?Z@ejAZBejCZDejEZFejGZHejIZJejKZLejMZNejOZPejQZRejSZTejUZUejVZVejWZWejXZXejYZYejZZZej[Z[ej\Z\ej]Z]ej^Z^ej_Z_ej`Z`ejaZaejbZbejcZcejdZdejeZeejfZfejgZgejhZhedZiG dd dZjeji ZkejehdeedefeiiZlG dd dejmZnG dd denZoG dd denZp		ddejqd dejrej ddfddZsdS )a  
This package implements `OpenTelemetry Resources
<https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/resource/sdk.md#resource-sdk>`_:

    *A Resource is an immutable representation of the entity producing
    telemetry. For example, a process producing telemetry that is running in
    a container on Kubernetes has a Pod name, it is in a namespace and
    possibly is part of a Deployment which also has a name. All three of
    these attributes can be included in the Resource.*

Resource objects are created with `Resource.create`, which accepts attributes
(key-values). Resources should NOT be created via constructor, and working with
`Resource` objects should only be done via the Resource API methods. Resource
attributes can also be passed at process invocation in the
:envvar:`OTEL_RESOURCE_ATTRIBUTES` environment variable. You should register
your resource with the  `mysql.opentelemetry.sdk.trace.TracerProvider` by passing
them into their constructors. The `Resource` passed to a provider is available
to the exporter, which can send on this information as it sees fit.

.. code-block:: python

    trace.set_tracer_provider(
        TracerProvider(
            resource=Resource.create({
                "service.name": "shoppingcart",
                "service.instance.id": "instance-12",
            }),
        ),
    )
    print(trace.get_tracer_provider().resource.attributes)

    {'telemetry.sdk.language': 'python',
    'telemetry.sdk.name': 'opentelemetry',
    'telemetry.sdk.version': '0.13.dev0',
    'service.name': 'shoppingcart',
    'service.instance.id': 'instance-12'}

Note that the OpenTelemetry project documents certain `"standard attributes"
<https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/resource/semantic_conventions/README.md>`_
that have prescribed semantic meanings, for example ``service.name`` in the
above example.
     N)dumps)environ)parse)BoundedAttributes)$OTEL_EXPERIMENTAL_RESOURCE_DETECTORSOTEL_RESOURCE_ATTRIBUTESOTEL_SERVICE_NAME)ResourceAttributes)entry_pointsversion)AttributeValuezopentelemetry-sdkc                   @   s   e Zd ZdZddedeje fddZe			ddeje deje dd fdd	Z
e	dd
dZedefddZedefddZdddZdedefddZdd ZddefddZdS )ResourcezZA Resource is an immutable representation of the entity producing telemetry as Attributes.N
attributes
schema_urlc                 C   s"   t |d| _|d u rd}|| _d S )N)r    )r   _attributes_schema_url)selfr   r    r   e/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/mysql/opentelemetry/sdk/resources/__init__.py__init__   s   
zResource.__init__returnc              	   C   s   | si } g }t }ttdd}d|vr|d |D ]}|tttd|	 d
   qt|t t| |}|jtds^d}|jtd}|rT|d| 7 }|tt|i|}|S )zCreates a new `Resource` from attributes.

        Args:
            attributes: Optional zero or more key-value pairs.
            schema_url: Optional URL pointing to the schema

        Returns:
            The newly-created Resource.
        otel,opentelemetry_resource_detector)groupnameNunknown_service:)_DEFAULT_RESOURCEr   getr   splitappendnextiterr
   striploadget_aggregated_resourcesmerger   r   SERVICE_NAMEPROCESS_EXECUTABLE_NAME)r   r   resource_detectorsresource$otel_experimental_resource_detectorsresource_detectordefault_service_nameprocess_executable_namer   r   r   create   sP   
zResource.createc                   C   s   t S N)_EMPTY_RESOURCEr   r   r   r   	get_empty   s   zResource.get_emptyc                 C      | j S r2   )r   r   r   r   r   r         zResource.attributesc                 C   r5   r2   )r   r6   r   r   r   r      r7   zResource.schema_urlotherc                 C   sn   | j  }||j  | jdkr|j}n|jdkr| j}n| j|jkr'|j}ntd| j|j | S t||S )a^  Merges this resource and an updating resource into a new `Resource`.

        If a key exists on both the old and updating resource, the value of the
        updating resource will override the old resource value.

        The updating resource's `schema_url` will be used only if the old
        `schema_url` is empty. Attempting to merge two resources with
        different, non-empty values for `schema_url` will result in an error
        and return the old resource.

        Args:
            other: The other resource to be merged.

        Returns:
            The newly-created Resource.
        r   zEFailed to merge resources: The two schemas %s and %s are incompatible)r   copyupdater   loggererrorr   )r   r8   merged_attributesr   r   r   r   r(      s   



zResource.mergec                 C   s&   t |tsdS | j|jko| j|jkS )NF)
isinstancer   r   r   )r   r8   r   r   r   __eq__  s
   

zResource.__eq__c                 C   s"   t t| j dd d| j S )NT)	sort_keys|)hashr   r   r9   r   r6   r   r   r   __hash__	  s   zResource.__hash__   c                 C   s   t t| j| jd|dS )N)r   r   )indent)r   dictr   r   )r   rE   r   r   r   to_json  s   zResource.to_jsonr2   )NNr   r   )r8   r   r   r   )rD   )__name__
__module____qualname____doc__
AttributestypingOptionalstrr   staticmethodr1   r4   propertyr   r   r(   objectboolr?   rC   rG   r   r   r   r   r      s.    8
$r   pythonopentelemetryc                   @   s&   e Zd Zd	ddZejd
ddZdS )ResourceDetectorFc                 C   s
   || _ d S r2   )raise_on_error)r   rX   r   r   r   r   #  s   
zResourceDetector.__init__r   r   c                 C   s   t  r2   )NotImplementedErrorr6   r   r   r   detect&  r7   zResourceDetector.detectN)FrH   )rI   rJ   rK   r   abcabstractmethodrZ   r   r   r   r   rW   "  s    
rW   c                   @      e Zd ZdddZdS )OTELResourceDetectorr   r   c           	      C   s   t t}i }|rC|dD ]4}z|jddd\}}W n ty4 } ztd|| W Y d }~qd }~ww t|	 }|||	 < qt t
}|rN||t< t|S )Nr   =   )maxsplitz0Invalid key value resource attribute pair %s: %s)r   r    r   r!   
ValueErrorr;   warningr   unquoter%   r   r)   r   )	r   env_resources_itemsenv_resource_mapitemkeyvalueexcvalue_url_decodedservice_namer   r   r   rZ   -  s*   

zOTELResourceDetector.detectNrH   rI   rJ   rK   rZ   r   r   r   r   r^   +      r^   c                   @   r]   )ProcessResourceDetectorr   r   c                 C   sN   d tttjjdkrtjjstjd d ntj}tttj	t
tjjt|iS )N.final   )joinmaprP   sysversion_inforeleaselevelserialr   PROCESS_RUNTIME_DESCRIPTIONr   PROCESS_RUNTIME_NAMEimplementationr   PROCESS_RUNTIME_VERSION)r   _runtime_versionr   r   r   rZ   G  s    
zProcessResourceDetector.detectNrH   rm   r   r   r   r   ro   E  rn   ro      	detectorsinitial_resourcer   c           
         s   |pt  }tjjddU  fdd| D }t|D ]>\}}| | }z0z|j|d}W n  tyL }	 zt}|j	r;|	t
d|	| W Y d}	~	nd}	~	ww W ||}q||}w W d   |S 1 sew   Y  |S )a'  Retrieves resources from detectors in the order that they were passed

    :param detectors: List of resources in order of priority
    :param initial_resource: Static resource. This has highest priority
    :param timeout: Number of seconds to wait for each detector to return
    :return:
    rD   )max_workersc                    s   g | ]}  |jqS r   )submitrZ   ).0detectorexecutorr   r   
<listcomp>j  s    z,get_aggregated_resources.<locals>.<listcomp>)timeoutz%Exception %s in detector %s, ignoringN)r   r1   
concurrentfuturesThreadPoolExecutor	enumerateresult	Exceptionr3   rX   r;   rc   r(   )
r   r   r   detectors_merged_resourcer   detector_indfuturer   detected_resourceexr   r   r   r'   [  s2   

r'   )Nr~   )trL   r[   concurrent.futuresr   loggingru   rN   jsonr   osr   urllibr   mysql.opentelemetry.attributesr   -mysql.opentelemetry.sdk.environment_variablesr   r   r   $mysql.opentelemetry.semconv.resourcer	   ,mysql.opentelemetry.util._importlib_metadatar
   r   mysql.opentelemetry.util.typesr   
LabelValueDictrP   rM   	getLoggerrI   r;   CLOUD_PROVIDERCLOUD_ACCOUNT_IDCLOUD_REGIONCLOUD_AVAILABILITY_ZONECONTAINER_NAMECONTAINER_IDCONTAINER_IMAGE_NAMECONTAINER_IMAGE_TAGDEPLOYMENT_ENVIRONMENT	FAAS_NAMEFAAS_IDFAAS_VERSIONFAAS_INSTANCE	HOST_NAME	HOST_TYPEHOST_IMAGE_NAMEHOST_IMAGE_IDHOST_IMAGE_VERSIONK8S_CLUSTER_NAMEKUBERNETES_CLUSTER_NAMEK8S_NAMESPACE_NAMEKUBERNETES_NAMESPACE_NAMEK8S_POD_UIDKUBERNETES_POD_UIDK8S_POD_NAMEKUBERNETES_POD_NAMEK8S_CONTAINER_NAMEKUBERNETES_CONTAINER_NAMEK8S_REPLICASET_UIDKUBERNETES_REPLICA_SET_UIDK8S_REPLICASET_NAMEKUBERNETES_REPLICA_SET_NAMEK8S_DEPLOYMENT_UIDKUBERNETES_DEPLOYMENT_UIDK8S_DEPLOYMENT_NAMEKUBERNETES_DEPLOYMENT_NAMEK8S_STATEFULSET_UIDKUBERNETES_STATEFUL_SET_UIDK8S_STATEFULSET_NAMEKUBERNETES_STATEFUL_SET_NAMEK8S_DAEMONSET_UIDKUBERNETES_DAEMON_SET_UIDK8S_DAEMONSET_NAMEKUBERNETES_DAEMON_SET_NAMEK8S_JOB_UIDKUBERNETES_JOB_UIDK8S_JOB_NAMEKUBERNETES_JOB_NAMEK8S_CRONJOB_UIDKUBERNETES_CRON_JOB_UIDK8S_CRONJOB_NAMEKUBERNETES_CRON_JOB_NAMEOS_TYPEOS_DESCRIPTIONPROCESS_PIDr*   PROCESS_EXECUTABLE_PATHPROCESS_COMMANDPROCESS_COMMAND_LINEPROCESS_COMMAND_ARGSPROCESS_OWNERrz   r|   ry   r)   SERVICE_NAMESPACESERVICE_INSTANCE_IDSERVICE_VERSIONTELEMETRY_SDK_NAMETELEMETRY_SDK_VERSIONTELEMETRY_AUTO_VERSIONTELEMETRY_SDK_LANGUAGE_OPENTELEMETRY_SDK_VERSIONr   r3   r   ABCrW   r^   ro   ListrO   r'   r   r   r   r   <module>   s   +
 
		