
    gUK              	          U 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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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 dd
lmZ ddlmZmZ ddl m!Z! dZ"ee   e#d<   	 ddl"Z$e$Z"e!Z&e	jN                  e(e&f   Z) ejT                  e+      Z,ejZ                  Z-ej\                  Z.ej^                  Z/ej`                  Z0ejb                  Z1ejd                  Z2ejf                  Z3ejh                  Z4ejj                  Z5ejl                  Z6ejn                  Z7ejp                  Z8ejr                  Z9ejt                  Z:ejv                  Z;ejx                  Z<ejz                  Z=ej|                  Z>ej~                  Z?ej                  ZAej                  ZCej                  ZEej                  ZGej                  ZIej                  ZKej                  ZMej                  ZOej                  ZQej                  ZSej                  ZUej                  ZWej                  ZYej                  Z[ej                  Z]ej                  Z_ej                  Zaej                  Zbej                  Zcej                  Zdej                  Zeej                  Zfej                  Zgej                  Zhej                  Ziej                  Zjej                  Zkej                  Zlej                  Zmej                  Znej                  Zoej                  Zpej                  Zqej                  Zrej                  Zsej                  Ztej                  Zuej                  Zvej                  Zw ed      Zxe(e#d<    G d d      Zy eyi       Zz eyewdetdeuexi      Z{ G d dej                        Z} G d de}      Z~ G d de}      Z G d de}      Z G d de}      Z	 	 d#de	j                  d   de	j"                  ey   d ed!dfd"Zy# e%$ r Y }w xY w)$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  `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)
ModuleType)ListMutableMappingOptionalcast)parse)BoundedAttributes)$OTEL_EXPERIMENTAL_RESOURCE_DETECTORSOTEL_RESOURCE_ATTRIBUTESOTEL_SERVICE_NAME)ResourceAttributes)entry_pointsversion)AttributeValuepsutilzopentelemetry-sdk_OPENTELEMETRY_SDK_VERSIONc                       e Zd ZU dZeed<   eed<   	 ddedej                  e   fdZ
e	 	 ddej                  e   dej                  e   dd fd	       Zedd
       Zedefd       Zedefd       ZddZdedefdZdefdZddedefdZy)ResourcezZA Resource is an immutable representation of the entity producing telemetry as Attributes._attributes_schema_urlN
attributes
schema_urlc                 <    t        |      | _        |d}|| _        y )N)r    )r   r   r   )selfr   r   s      Y/var/www/openai/venv/lib/python3.12/site-packages/opentelemetry/sdk/resources/__init__.py__init__zResource.__init__   s$     -
CJ%    returnc                    | si } dhj                  t        j                  t        d      j	                  d      D ch c]  }|r|j                          c}      }g }|D ]Q  }|j                   t        t        t        d|j                                           j                                       S t        |t              j                  t        | |            }|j                  j                  t         d      sbd}t#        t$        t&           |j                  j                  t(        d            }|r|d|z   z  }|j                  t        t         |i|            }|S c c}w )	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.
        otelr   ,opentelemetry_resource_detector)groupnameNunknown_service:)unionr   getr   splitstripappendnextiterr   loadget_aggregated_resources_DEFAULT_RESOURCEmerger   r   SERVICE_NAMEr	   r   strPROCESS_EXECUTABLE_NAME)	r   r   #otel_experimental_resource_detector$otel_experimental_resource_detectorsresource_detectorsresource_detectorresourcedefault_service_nameprocess_executable_names	            r   createzResource.create   sv    J06x~~ <C;;8"<%*<<7 7	 499;<0
, 68 "F%%$"C!2!8!8!: $&	 "F , 1

%Z0
1 	 ""&&|T:#4 &*##''(?F'# '$.E(EE$~~,(<=zJH Ms   E!c                      t         S N)_EMPTY_RESOURCE r    r   	get_emptyzResource.get_empty   s    r    c                     | j                   S rA   )r   r   s    r   r   zResource.attributes       r    c                     | j                   S rA   )r   rF   s    r   r   zResource.schema_url   rG   r    otherc                    | j                   j                         }|j                  |j                          | j                  dk(  r|j                  }no|j                  dk(  r| j                  }nS| j                  |j                  k(  r|j                  }n-t        j                  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   rI   merged_attributesr   s       r   r4   zResource.merge   s    " !OO002  !1!12??b ))J#J__ 0 00))JLLW  
 K):66r    c                     t        |t              sy| j                  |j                  k(  xr | j                  |j                  k(  S )NF)
isinstancer   r   r   )r   rI   s     r   __eq__zResource.__eq__  sA    %* 1 11 6  E$5$55	
r    c                 |    t        t        | j                  j                         d       d| j                         S )NT)	sort_keys|)hashr   r   rK   r   rF   s    r   __hash__zResource.__hash__  s;    T%%**,=>a@P@P?QR
 	
r    indentc                 `    t        | j                        }t        || j                  d|      S )N)r   r   )rX   )dictr   r   r   )r   rX   r   s      r   to_jsonzResource.to_json"  s:    :>;

 (".. 
 	
r    rA   )NNr!   r   )rI   r   r!   r   )   )__name__
__module____qualname____doc__r   __annotations__r6   
Attributestypingr   r   staticmethodr?   rD   propertyr   r   r4   objectboolrR   intrW   r[   rC   r    r   r   r      s    d"" JN&$&28//#2F& 26+/8OOJ/8OOC(8 
8 8t    J      C    !7F
F 
t 

# 



c 

# 

r    r   pythonopentelemetryc                   D    e Zd ZddeddfdZej                  dd       Zy)ResourceDetectorraise_on_errorr!   Nc                     || _         y rA   )rn   )r   rn   s     r   r   zResourceDetector.__init__:  s
    ,r    c                     t               rA   )NotImplementedErrorrF   s    r   detectzResourceDetector.detect=  s    !##r    )Fr\   )r^   r_   r`   rh   r   abcabstractmethodrr   rC   r    r   rm   rm   9  s/    -t - - 	$ $r    rm   c                       e Zd ZddZy)OTELResourceDetectorc                    t        j                  t              }i }|rc|j                  d      D ]O  }	 |j                  dd      \  }}t        j                  |j                               }|||j                         <   Q t        j                  t              }|r	||t        <   t        |      S # t        $ r!}t
        j                  d||       Y d }~d }~ww xY w)Nr$   =   )maxsplitz0Invalid key value resource attribute pair %s: %s)r   r+   r   r,   
ValueErrorrM   warningr
   unquoter-   r   r5   r   )	r   env_resources_itemsenv_resource_mapitemkeyvalueexcvalue_url_decodedservice_names	            r   rr   zOTELResourceDetector.detectD  s    %kk*BC+11#6!%C!!<JC %*MM%++-$@!0A - 7 {{#45-9\*()) " NNJ
 s   B00	C9CCNr\   r^   r_   r`   rr   rC   r    r   rv   rv   B  s    *r    rv   c                       e Zd ZddZy)ProcessResourceDetectorc                    dj                  t        t        t        j                  j
                  dk(  r-t        j                  j                  st        j                  d d nt        j                              }t        j                         }t        j                  }t        j                  j                  |      }t        j                  d   }dj                  t        j                        }t        j                  }t        t        j                  t        t        j                   j"                  t$        |t&        |t(        |t*        |t,        |t.        |t0        |i	}t3        t        d      rt        j4                         |t6        <   t8        -t8        j;                         }	|	j=                         }
|
|t>        <   tA        |      S )N.final   r    getppid)!joinmapr6   sysversion_inforeleaselevelserialosgetpid
executablepathdirnameargvPROCESS_RUNTIME_DESCRIPTIONr   PROCESS_RUNTIME_NAMEimplementationr'   PROCESS_RUNTIME_VERSIONPROCESS_PIDr7   PROCESS_EXECUTABLE_PATHPROCESS_COMMANDPROCESS_COMMAND_LINEPROCESS_COMMAND_ARGShasattrr   PROCESS_PARENT_PIDr   ProcessusernamePROCESS_OWNERr   )r   _runtime_version_process_pid_process_executable_name_process_executable_path_process_command_process_command_line_process_command_argsresource_infoprocessr   s              r   rr   zProcessResourceDetector.detect^  sP   88 ''44?,,33 $$Ra( ))

 yy{#&>> #%77??3K#L 88A; # 2 #' #"4"4"9"9#%5#%=#%=- "7 "7

 2y!02

M,--3^^-=G'')H+3M-(&&r    Nr\   r   rC   r    r   r   r   \  s    &'r    r   c                       e Zd ZdZddZy)OsResourceDetectorzxDetect os resources based on `Operating System conventions <https://opentelemetry.io/docs/specs/semconv/resource/os/>`_.c                    t        j                         j                         }t        j                         }|dk(  rt        j                         }n|dk(  rd}t        j                         }t        t        |t        |i      S )a  Returns a resource with with ``os.type`` and ``os.version``.

        Python's platform library
        ~~~~~~~~~~~~~~~~~~~~~~~~~

        To grab this information, Python's ``platform`` does not return what a
        user might expect it to. Below is a breakdown of its return values in
        different operating systems.

        .. code-block:: python
            :caption: Linux

            >>> platform.system()
            'Linux'
            >>> platform.release()
            '6.5.0-35-generic'
            >>> platform.version()
            '#35~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Tue May  7 09:00:52 UTC 2'

        .. code-block:: python
            :caption: MacOS

            >>> platform.system()
            'Darwin'
            >>> platform.release()
            '23.0.0'
            >>> platform.version()
            'Darwin Kernel Version 23.0.0: Fri Sep 15 14:42:57 PDT 2023; root:xnu-10002.1.13~1/RELEASE_ARM64_T8112'

        .. code-block:: python
            :caption: Windows

            >>> platform.system()
            'Windows'
            >>> platform.release()
            '2022Server'
            >>> platform.version()
            '10.0.20348'

        .. code-block:: python
            :caption: FreeBSD

            >>> platform.system()
            'FreeBSD'
            >>> platform.release()
            '14.1-RELEASE'
            >>> platform.version()
            'FreeBSD 14.1-RELEASE releng/14.1-n267679-10e31f0946d8 GENERIC'

        .. code-block:: python
            :caption: Solaris

            >>> platform.system()
            'SunOS'
            >>> platform.release()
            '5.11'
            >>> platform.version()
            '11.4.0.15.0'

        windowssunossolaris)platformsystemlowerreleaser   r   OS_TYPE
OS_VERSION)r   os_type
os_versions      r   rr   zOsResourceDetector.detect  sw    | //#))+%%'
 i!))+JG!))+JJ
 	
r    Nr\   r^   r_   r`   ra   rr   rC   r    r   r   r     s     CN
r    r   c                       e Zd ZdZddZy)_HostResourceDetectorzT
    The HostResourceDetector detects the hostname and architecture attributes.
    c                 x    t        t        t        j                         t        t        j                         i      S rA   )r   	HOST_NAMEsocketgethostname	HOST_ARCHr   machinerF   s    r   rr   z_HostResourceDetector.detect  s0    6--/8++-
 	
r    Nr\   r   rC   r    r   r   r     s    
r    r   	detectorsinitial_resourcetimeoutr!   c                    |xs t         j                         }t        j                  j	                  d      5 }| D cg c]  }|j                  |j                         }}t        |      D ]4  \  }}| |   }t        }		 |j                  |      }	|j                  |	      }6 	 ddd       |S c c}w # t        j                  j                  $ r/}
|j                  r|
t        j                  d||       Y d}
~
jd}
~
wt        $ r/}
|j                  r|
t        j                  d|
|       Y d}
~
d}
~
ww xY w# |j                  |	      }w xY w# 1 sw Y   |S xY w)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:
    r]   )max_workers)r   z1Detector %s took longer than %s seconds, skippingNz%Exception %s in detector %s, ignoring)r   r?   
concurrentfuturesThreadPoolExecutorsubmitrr   	enumeraterB   resultTimeoutErrorrn   rM   r|   	Exceptionr4   )r   r   r   detectors_merged_resourceexecutordetectorr   detector_indfuturedetected_resourceexs              r   r2   r2     sQ    !1 EHOO4E				.	.1	.	=DMNI8??8??3IN$-g$6 L& .H*9$*MM'M$B!" -F,K,K%-)+ %7 
>6 %$5 O %%22 **HG   **H;R  -F,K,K%-)/ 
>6 %$sd   E"B/ E?B4E/E4D6%C;6D9;D6%D1,D91D66D99EEE)N   )ra   rs   concurrent.futuresr   loggingr   r   r   r   rd   jsonr   r   typesr   r   r   r   r	   urllibr
   opentelemetry.attributesr   'opentelemetry.sdk.environment_variablesr   r   r   opentelemetry.semconv.resourcer   &opentelemetry.util._importlib_metadatar   r   opentelemetry.util.typesr   r   rb   psutil_moduleImportError
LabelValueMappingr6   rc   	getLoggerr^   rM   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_INSTANCEr   r   	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_DESCRIPTIONr   r   r   r   r7   r   r   r   r   r   r   r   r   r5   SERVICE_NAMESPACESERVICE_INSTANCE_IDSERVICE_VERSIONTELEMETRY_SDK_NAMETELEMETRY_SDK_VERSIONTELEMETRY_AUTO_VERSIONTELEMETRY_SDK_LANGUAGEr   r   rB   r3   ABCrm   rv   r   r   r   ri   r2   rC   r    r   <module>r)     s#  )V    	   
     7 7  6 
 > H 3# #	"F 
^^CO,
			8	$#22%66 !..,DD #22!..)>> (<< +BB ((	

$
$!.."00((	((	((	$44"00':: ,== .AA '33 (55 .AA /BB 0DD .AA /CC 0DD 1FF .@@ /BB '33 (55 ,<< ->> #22

$
$**
 ,,':: ,DD ,DD $44)>> )>> "00)>> ,DD 0LL !..&88 (<< $44':: *@@ +BB +BB ")*="> C >O
 O
d 2,O9 $sww $*+ *4('. ('VQ
) Q
h
, 
  37)%{{-.)%ooh/)% )% 	)%c  		s   7O2 2O;:O;