
    AinN              	          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 except by `ResourceDetector`
instances which can't use `Resource.create` to avoid infinite loops. 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OptionalSet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	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      l/home/jang/Projects/ai-researcher/.venv/lib/python3.12/site-packages/opentelemetry/sdk/resources/__init__.py__init__zResource.__init__   s$     -
CJ%    returnc                 N   | si } dhj                  t        j                  t        d      j	                  d      D ch c]  }|r|j                          c}      }g }d|v rt        d      j                  }|D ]R  }	 |j                   t        t        t        d|j                                           j                                       T t        |t               j#                  t%        | |            }|j&                  j                  t(        d	      sbd
}t+        t,        t.           |j&                  j                  t0        d	            }|r|d|z   z  }|j#                  t%        t(        |i|            }|S c c}w # t        $ r t        j                  d|       Y )w xY w)a9  Creates a new `Resource` from attributes.

        `ResourceDetector` instances should not call this method.

        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)r'   namez/Failed to load resource detector '%s', skippingNunknown_service:)unionr   getr   splitstripr   namesappendnextiterload	Exceptionlogger	exception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   s   " J:@ <C;;8"<%*	7 7	 499;:
, 68663?74e 1 "F 	"))D(&G%6%<%<%> df		$ , 1

%Z0
1 	 ""&&|T:#4 &*##''(?F'# '$.E(EE$~~,(<=zJH c8    E% s   E<>AFF$#F$c                      t         S N)_EMPTY_RESOURCE r    r   	get_emptyzResource.get_empty   s    r    c                     | j                   S rF   )r   r   s    r   r   zResource.attributes       r    c                     | j                   S rF   )r   rK   s    r   r   zResource.schema_url  rL   r    otherc                    t        | 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)dictr   copyupdater   r5   errorr   )r   rN   merged_attributesr   s       r   r9   zResource.merge  s    " !1668  !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   rN   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   rQ   r   rK   s    r   __hash__zResource.__hash__1  s;    T%%**,=>a@P@P?QR
 	
r    indentc                 \    t        t        | j                        | j                  d|      S )N)r   r   )r]   )r   rP   r   r   )r   r]   s     r   to_jsonzResource.to_json6  s.    "4??3".. 
 	
r    rF   )NNr!   r   )rN   r   r!   r   )   )__name__
__module____qualname____doc__r   __annotations__r;   
Attributestypingr   r   staticmethodrD   rI   propertyr   r   r9   objectboolrW   intr\   r_   rH   r    r   r   r      s   d"" JN&$&28//#2F& 26+/EOOJ/EOOC(E 
E EN    J      C    !7F
F 
t 

# 


hsm 
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 rF   )rr   )r   rr   s     r   r   zResourceDetector.__init__K  s
    ,r    c                     t               )zdDon't call `Resource.create` here to avoid an infinite loop, instead instantiate `Resource` directly)NotImplementedErrorrK   s    r   detectzResourceDetector.detectN  s     "##r    )Fr`   )rb   rc   rd   rl   r   abcabstractmethodrv   rH   r    r   rq   rq   J  s/    -t - - 	$ $r    rq   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-   
ValueErrorr5   warningr
   unquoter.   r   r:   r   )	r   env_resources_itemsenv_resource_mapitemkeyvalueexcvalue_url_decodedservice_names	            r   rv   zOTELResourceDetector.detectV  s    %kk*BC68+11#6 B!%C!!<JC %*MM%++-$@!0A -B {{#45-9\*()) " NNJ
 s   B00	C9CCNr`   rb   rc   rd   rv   rH   r    r   rz   rz   T  s    *r    rz   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mapr;   sysversion_inforeleaselevelserialosgetpid
executablepathdirnameargvPROCESS_RUNTIME_DESCRIPTIONr   PROCESS_RUNTIME_NAMEimplementationr(   PROCESS_RUNTIME_VERSIONPROCESS_PIDr<   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   rv   zProcessResourceDetector.detectp  sP   88 ''44?,,33 $$Ra( ))

 yy{#&>> #%77??3K#L 88A; # 2 #' #"4"4"9"9#%5#%=#%=- "7 "7

 2y!02

M,-nn&G'')H+3M-(&&r    Nr`   r   rH   r    r   r   r   n  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   rv   zOsResourceDetector.detect  sw    | //#))+%%'
 i!))+JG!))+JJ
 	
r    Nr`   rb   rc   rd   re   rv   rH   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 rF   )r   	HOST_NAMEsocketgethostname	HOST_ARCHr   machinerK   s    r   rv   z_HostResourceDetector.detect  s0    6--/8++-
 	
r    Nr`   r   rH   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:
    ra   )max_workers)r   z1Detector %s took longer than %s seconds, skippingNz%Exception %s in detector %s, ignoring)r   rD   
concurrentfuturesThreadPoolExecutorsubmitrv   	enumeraterG   resultTimeoutErrorrr   r5   r   r4   r9   )r   r   r   detectors_merged_resourceexecutordetectorr   detector_indfuturedetected_resourceexs              r   r7   r7     sZ    !1 EHOO4E				.	.1	.	= DMN8??8??3NN$-g$6 	 L& .H*9$*MM'M$B!" -F,K,K%-)+	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   )re   rw   concurrent.futuresr   loggingr   r   r   r   rh   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   rf   psutil_moduleImportError
LabelValueMappingr;   rg   	getLoggerrb   r5   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   r<   r   r   r   r   r   r   r   r   r:   SERVICE_NAMESPACESERVICE_INSTANCE_IDSERVICE_VERSIONTELEMETRY_SDK_NAMETELEMETRY_SDK_VERSIONTELEMETRY_AUTO_VERSIONTELEMETRY_SDK_LANGUAGEr   r   rG   r8   ABCrq   rz   r   r   r   rm   r7   rH   r    r   <module>r,     s'  *^    	   
     , ,  6 
 > 4# #	"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 >Y
 Y
x 2,O9 $sww $*+ *4('. ('VQ
) Q
h
, 
  37)%{{-.)%ooh/)% )% 	)%y  		s   7O2 2O;:O;