ó
    üÞ jÿ   ã                  ó"  • S r SSKJr  SSKrSSKrSSKJrJrJrJ	r	J
r
  SSKJr  SSKJr  SSKJrJr  \(       a  SS	KJrJr  SS
KJrJr  \R2                  " \5      r/ SQrSS jrSS jrSS jrSS.       SS jjr    S       SS jjr!g)z(OpenTelemetry integration for LangSmith.é    )ÚannotationsN)ÚTYPE_CHECKINGÚAnyÚOptionalÚUnionÚcast)Úutils)ÚClienté   )Úotel_safe_attribute_valueÚ set_langsmith_metadata_attribute)ÚSpanÚSpanContext)ÚOtelExporterÚOtelSpanProcessor)Ú	configurer   r   Ú"langsmith_run_id_from_otel_span_idÚget_langsmith_run_url_for_spanr   r   c                ó^   • U S;   a  SSK Jn  [        X5      $ [        S[        < SU < 35      e)zHLazily import processor exports so offline helpers stay dependency-free.)r   r   r   )Ú	processorzmodule z has no attribute )Ú r   ÚgetattrÚAttributeErrorÚ__name__)Únamer   s     Ú^/var/www/html/gaurav/venv/lib/python3.13/site-packages/langsmith/integrations/otel/__init__.pyÚ__getattr__r       s2   € àÐ4Ó4Ýä�yÓ'Ð'Ü
˜7¤8¡,Ð.@ÀÁÐIÓ
JÐJó    c                óì  • [        U [        5      (       aB  S[        U 5      s=::  a  S::  d  O  [        S[        U 5       35      eU R	                  SS5      nOy[        U [
        5      (       a  [        S5      e[        U [        5      (       a-  SU s=::  a  S:  d  O  [        S5      eU R                  SS5      nO[        S	[        U 5       35      eUS
:X  a  [        S5      e[        R                  " US9$ )a  Map an OpenTelemetry span ID to its LangSmith run ID.

The LangSmith backend derives a run's UUID from the OTel span ID during
ingest: the span-ID bytes are copied right-aligned into a 16-byte,
zero-padded buffer, and those bytes become the run UUID.

This mapping is deterministic and fully offline: it makes no network
request and does not require `opentelemetry` to be installed. The span ID
to run ID relationship is fixed by the server's ingest rule, so the run ID
is stable regardless of when (or whether) ingest has completed.

Args:
    span_id: The OTel span ID, either as an `int` (as returned by
        `span.get_span_context().span_id`) or as raw `bytes` (8 to 16
        bytes; left-padded with zeros to 16 bytes).

Returns:
    The LangSmith run ID as a `uuid.UUID`.
r   é   z&span_id bytes must be 1-16 bytes, got ó    z&span_id must be int or bytes, got booll            z+span_id int must be a non-zero 64-bit valueÚbigz"span_id must be int or bytes, got s                   z6span_id is all-zero, which is not a valid OTel span ID)Úbytes)Ú
isinstancer#   ÚlenÚ
ValueErrorÚrjustÚboolÚ	TypeErrorÚintÚto_bytesÚtypeÚuuidÚUUID)Úspan_idÚraws     r   r   r   )   sÔ   € ô( �'œ5×!Ñ!Ø”C˜“LÕ& BÕ&ÜÐEÄcÈ'ÃlÀ^ÐTÓUÐUØ�m‰m˜B Ó(‰Ü	�GœT×	"Ñ	"äÐ@ÓAÐAÜ	�GœS×	!Ñ	!à�GÕ'˜wÕ'ÜÐJÓKÐKØ×Ñ˜r 5Ó)‰äÐ<¼TÀ'»]¸OÐLÓMÐMØ
ˆlÓÜÐQÓRÐRÜ�9Š9˜3ÑÐr   c                ó‚   • SSK Jn  [        X5      (       a  U R                  5       nUR                  $ U nUR                  $ )z>Extract the integer span ID from an OTel span or span context.r   )r   )Úopentelemetry.tracer   r$   Úget_span_contextr/   )Úspanr   Úctxs      r   Ú_extract_span_idr6   P   s:   € å(ä%/°×%;Ñ%;ˆ$×
Ñ
Ó
!€CØ�;‰;Ðð BF€CØ�;‰;Ðr   )Úclientc               óœ   • U=(       d
    [        5       n[        [        U 5      5      nUR                   SUR	                  5        SU SU S3$ )a›  Build the LangSmith run URL for a native OpenTelemetry span.

Useful when your application creates and exports OTel spans to LangSmith
and wants to surface a run URL immediately, without polling or waiting for
asynchronous OTel ingest to complete.

The span ID to run ID mapping is deterministic and offline (see
`langsmith_run_id_from_otel_span_id`). The returned URL is "eventually
valid": it points at the run that will materialize once OTel ingest
finishes, which may not have happened yet at the time of the call.

Network usage:
    - The span to run ID mapping is fully offline.
    - Resolving `tenant_id` and `host_url` for the URL uses `client`
      (a default `Client` is constructed if none is given).

Args:
    span: An OTel span or span context.
    project_id: The LangSmith project (session) ID.
    client: Optional `Client` used to resolve tenant and host URL.

Returns:
    The full LangSmith run URL.
z/o/z/projects/p/z/r/z
?poll=true)r
   r   r6   Ú	_host_urlÚ_get_tenant_id)r4   Ú
project_idr7   Úrun_ids       r   r   r   X   sY   € ð< ×”v“x€FÜ/Ô0@ÀÓ0FÓG€Fà×ÑÐ
˜C × 5Ñ 5Ó 7Ð8¸ÀZÀLð QØˆH�Jð	 ðr   c                ó˜  •  SSK Jn  SSKJn  SSKJnJnJn  [        XCR                  5       5      nUR                  [        5      n	[        X‡5      (       aO  [        U	S5      (       a>  [        [        UU	5      R                  U5      (       a  U" 5       n
UR                  U
5        O[         R#                  S5        gU =(       d    [$        R&                  " S5      n U (       d  gU=(       d    [$        R(                  " 5       nS	S
KJn  U" XUS9nU
R/                  U5        g! [0         a   n[         R#                  SU5         SnAgSnAff = f)aÛ  Configure OpenTelemetry with LangSmith as the `TracerProvider`.

Initializes OpenTelemetry with LangSmith as the primary and only `TracerProvider`.

Usage:
    >>> from langsmith.integrations.otel import configure
    >>> configure(  # doctest: +SKIP
    ...     api_key="your-api-key", project_name="your-project"
    ... )

    Using environment variables:
    >>> # Set LANGSMITH_API_KEY and LANGSMITH_PROJECT
    >>> configure()  # Will use env vars  # doctest: +SKIP

!!! warning

    This function is only for when LangSmith is your ONLY OpenTelemetry source.

    It sets the global TracerProvider, which can only be done once per application.

This function will fail if OpenTelemetry is already initialized with another
`TracerProvider` (you cannot override an existing `TracerProvider`).

If you already have OpenTelemetry set up with other tools, use `OtelSpanProcessor`
directly to add LangSmith to your existing setup:

!!! example "Adding LangSmith to existing OTEL setup"
    ```python
    from opentelemetry import trace
    from langsmith.integrations.otel.processor import OtelSpanProcessor

    # Use your existing provider (already initialized)
    provider = trace.get_tracer_provider()

    # Add LangSmith processor to existing provider
    langsmith_processor = OtelSpanProcessor(
        api_key="your-api-key", project="your-project"
    )
    provider.add_span_processor(langsmith_processor)
    ```

Args:
    api_key: LangSmith API key. Defaults to `LANGSMITH_API_KEY` env var.
    project_name: Project name. Defaults to `LANGSMITH_PROJECT` env var.
    SpanProcessor: Span processor class to use. Defaults to `BatchSpanProcessor`.

Returns:
    `True` if configuration succeeded, `False` if `TracerProvider` already exists.
r   )Útrace)ÚTracerProvider)Ú
NoOpTracerÚProxyTracerÚProxyTracerProviderÚ_tracerz¨OpenTelemetry TracerProvider is already set. Cannot override existing TracerProvider. Use OtelSpanProcessor directly to add LangSmith to your existing provider instead.FNr   )r   )Úapi_keyÚprojectÚSpanProcessorTz(Failed to initialize Otel for LangSmith:)Úopentelemetryr>   Úopentelemetry.sdk.tracer?   r2   r@   rA   rB   r   Úget_tracer_providerÚ
get_tracerr   r$   ÚhasattrrC   Úset_tracer_providerÚloggerÚwarningÚls_utilsÚget_api_keyÚget_tracer_projectr   r   Úadd_span_processorÚ	Exception)rD   Úproject_namerF   r>   r?   r@   rA   rB   Úexisting_providerÚtracerÚproviderr   r   Úes                 r   r   r   ~   s  € ðl/Ý'Ý:ßTÑTä  ×1JÑ1JÓ1LÓMÐØ"×-Ñ-¬hÓ7ˆô
 Ð(×>Ñ>Ü˜ 	×*Ñ*ÜÜØØó÷ ‘'Ø÷ñ ñ &Ó'ˆHØ×%Ñ% hÕ/ä�N‰NðOôð
 à×7œX×1Ò1°$Ó7ˆÞØà#×D¤x×'BÒ'BÓ'Dˆå0á%ØÀñ
ˆ	ð 	×#Ñ# IÔ.ØøÜó Ü�‰ÐAÀ1ÔEÜûðús$   ‚B8D Â;&D Ã"<D Ä
E	Ä)EÅE	)r   ÚstrÚreturnr   )r/   zUnion[int, bytes]rZ   ú	uuid.UUID)r4   úUnion[Span, SpanContext]rZ   r*   )r4   r\   r;   r[   r7   zOptional[Client]rZ   rY   )NNN)rD   úOptional[str]rT   r]   rF   zOptional[type]rZ   r(   )"Ú__doc__Ú
__future__r   Úloggingr-   Útypingr   r   r   r   r   Ú	langsmithr	   rO   Úlangsmith.clientr
   Ú_utilsr   r   r2   r   r   r   r   r   Ú	getLoggerr   rM   Ú__all__r   r   r6   r   r   © r   r   Ú<module>rh      s¹   ðÙ .å "ã Û ß <Õ <å 'Ý #ç Oæß5ç:à	×	Ò	˜8Ó	$€ò€ôKô$ ôNð  $ñ	#Ø
"ð#àð#ð ð	#ð
 	õ#ðN "Ø"&Ø$(ðeØðeàðeð "ðeð 
ö	er   