Ë
    tPêjÚ  ã                   ó  — U d Z ddlmZmZ ddlZddlmZ ddlmZm	Z	 ddl
mZ ddlmZmZmZ ddlmZ d	Zeed
<   dZeed<   dZeed<    G d„ de«      Zddœdedz  defd„Zdedefd„Zdedeeef   fd„Z eee«       y)uy  AgentOps preset â€” OTLP/HTTP to AgentOps' endpoint with a lazily-fetched JWT.

AgentOps authenticates with a short-lived JWT minted from the API key. Fetching
it is blocking network I/O, so it must never run on the event loop: callback
construction (where presets are built) can run inside the proxy's async startup
or, in the SDK, on the first request. Instead of fetching at config-build time,
this preset registers a custom exporter (``kind="agentops"``) that mints the JWT
**on its first export** â€” which the ``BatchSpanProcessor`` runs in its own
worker thread, off any event loop â€” and caches it for the process lifetime.
é    )ÚAnyÚFinalN)ÚField)ÚBaseSettingsÚSettingsConfigDict)Úverbose_logger)ÚExporterOwnerÚExporterSpecÚOpenTelemetryV2Config)Úregister_exporter_factoryz"https://otlp.agentops.ai/v1/tracesÚ_AGENTOPS_ENDPOINTz%https://api.agentops.ai/v3/auth/tokenÚ_AGENTOPS_AUTH_ENDPOINTÚagentopsÚ_AGENTOPS_EXPORTER_KINDc                   óˆ   — e Zd ZU  edd¬«      Z edd¬«      Zedz  ed<    edd	¬«      Z	eed
<    edd¬«      Z
edz  ed<   y)Ú_AgentOpsSettingsFÚignore)Úcase_sensitiveÚextraNÚAGENTOPS_API_KEY)ÚdefaultÚvalidation_aliasÚapi_keyr   ÚAGENTOPS_SERVICE_NAMEÚservice_nameÚAGENTOPS_ENVIRONMENTÚenvironment)Ú__name__Ú
__module__Ú__qualname__r   Úmodel_configr   r   ÚstrÚ__annotations__r   r   © ó    úo/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/litellm/integrations/otel/presets/agentops.pyr   r      sL   … Ù%°UÀ(ÔK€Lá¨Ð?QÔR€GˆS�4‰ZÓRÙ jÐCZÔ[€L�#Ó[Ù#¨DÐCYÔZ€K��t‘ÔZr%   r   )Úconfig_overridesr'   Úreturnc           	      ó`  — t        «       }| xs
 t        «       }|j                  g |j                  ¢t	        t
        t        |j                  rd|j                  indt        j                  ¬«      ‘i |j                  ¥|j                  ddœ¥|j                  rd|j                  ini ¥dœ¬«      S )	uE  Build the AgentOps config without any network I/O.

    The ``agentops`` exporter mints (and caches) the JWT lazily on its first
    export, so this stays non-blocking. ``project.id`` is therefore not a
    resource attribute â€” it is encoded in the JWT, which AgentOps uses to route
    the trace to the right project.
    r   N)ÚkindÚendpointÚoptionsÚownerr   )zservice.nameztelemetry.sdk.namezdeployment.environment)Ú	exportersÚresource_attributes)Úupdate)r   r   Ú
model_copyr.   r
   r   r   r   r	   ÚAGENTOPSr/   r   r   )r'   ÚsettingsÚbases      r&   Úagentops_presetr5   '   sÂ   € ô (Ó)€HØ"Ò=Ô&;Ó&=€DØ�?‰?ðØ—‘ðäÜ0Ü/Ø>F×>NÒ>N˜i¨×)9Ñ)9Ñ:ÐTXÜ'×0Ñ0ô	ðð$Ø×*Ñ*ð$à (× 5Ñ 5Ø&0ò$ð HP×G[ÒG[Ð,¨h×.BÑ.BÑCÐacð	$ñ
ð ó ð r%   Úspecc                 óŠ   — ddl m}  G d„ d|«      }| j                  xs i } || j                  |j	                  d«      ¬«      S )zKFactory for the ``agentops`` exporter kind: a lazy-auth OTLP/HTTP exporter.r   )ÚOTLPSpanExporterc                   óX   ‡ — e Zd ZdZdedz  dedz  ddfˆ fd„Zd
d„Zdedefˆ fd	„Zˆ xZ	S )ú;_build_agentops_exporter.<locals>._LazyAuthAgentOpsExportera6  OTLP/HTTP exporter that mints the AgentOps JWT on its first export.

        ``export`` runs in the ``BatchSpanProcessor`` worker thread, so the
        blocking token fetch never touches an event loop. The result is cached
        after the first attempt (success or failure) so it runs at most once.
        r+   Nr   r(   c                óB   •— t         ‰| �  |¬«       || _        d| _        y )N)r+   F)ÚsuperÚ__init__Ú_agentops_api_keyÚ_auth_resolved)Úselfr+   r   Ú	__class__s      €r&   r=   zD_build_agentops_exporter.<locals>._LazyAuthAgentOpsExporter.__init__W   s#   ø€ Ü‰GÑ hÐÔ/Ø%,ˆDÔ"Ø"'ˆDÕr%   c                 ó&  — | j                   ry d| _         | j                  sy 	 t        | j                  «      j                  d«      }|rd|› �| j                  j
                  d<   y y # t        $ r }t        j                  d|«       Y d }~y d }~ww xY w)NTÚtokenzBearer ÚAuthorizationzAgentOps JWT fetch failed: %s)	r?   r>   Ú_fetch_agentops_jwtÚgetÚ_sessionÚheadersÚ	Exceptionr   Údebug)r@   rC   Úes      r&   Ú_ensure_authenticatedzQ_build_agentops_exporter.<locals>._LazyAuthAgentOpsExporter._ensure_authenticated\   s’   € Ø×"Ò"ØØ"&ˆDÔØ×)Ò)ØðIÜ2°4×3IÑ3IÓJ×NÑNÈwÓW�Ùð @GÀuÀgÐ=N�D—M‘M×)Ñ)¨/Ò:ð	 øô
 ò IÜ×$Ñ$Ð%DÀa×HÑHûðIús   £AA' Á'	BÁ0BÂBÚspansc                 óB   •— | j                  «        t        ‰| �	  |«      S )N)rL   r<   Úexport)r@   rM   rA   s     €r&   rO   zB_build_agentops_exporter.<locals>._LazyAuthAgentOpsExporter.exportl   s   ø€ Ø×&Ñ&Ô(Ü‘7‘> %Ó(Ð(r%   )r(   N)
r   r   r    Ú__doc__r"   r=   rL   r   rO   Ú__classcell__)rA   s   @r&   Ú_LazyAuthAgentOpsExporterr:   O   sH   ø„ ñ	ð	(¨¨d©
ð 	(¸SÀ4¹Zð 	(ÈDõ 	(ó
	Ið 	) ð 	)¨÷ 	)ñ 	)r%   rR   r   )r+   r   )Ú5opentelemetry.exporter.otlp.proto.http.trace_exporterr8   r,   r+   rF   )r6   r8   rR   r,   s       r&   Ú_build_agentops_exporterrT   I   s@   € õô)Ð$4ô )ðB —\‘\Ò' R€GÙ$¨d¯m©mÀWÇ[Á[ÐQZÓE[Ô\Ð\r%   r   c                 ó  — t        j                  d¬«      5 }|j                  t        dddœd| i¬«      }|j                  dk7  rt        d	|j                  › �«      ‚|j                  «       cd d d «       S # 1 sw Y   y xY w)
Né
   )Útimeoutzapplication/jsonz
keep-alive)zContent-TypeÚ
Connectionr   )ÚurlrH   ÚjsonéÈ   z Failed to fetch AgentOps token: )ÚhttpxÚClientÚpostr   Ústatus_codeÚRuntimeErrorÚtextrZ   )r   ÚclientÚresponses      r&   rE   rE   t   s}   € ô
 
�‰˜bÔ	!ð  VØ Ÿ+™+Ü'Ø%7À|ÑTØ˜WÐ%ð &ó 
ˆð
 ×Ñ 3Ò&ÜÐ!AÀ(Ç-Á-ÀÐQÓRÐRØ�}‰}‹÷÷ ò ús   —AA5Á5A>)rP   Útypingr   r   r\   Úpydanticr   Úpydantic_settingsr   r   Úlitellm._loggingr   Ú&litellm.integrations.otel.model.configr	   r
   r   Ú,litellm.integrations.otel.plumbing.providersr   r   r#   r   r   r   r5   rT   r"   ÚdictrE   r$   r%   r&   ú<module>rk      s¼   ðò	÷ ã Ý ß >å +÷ñ õ
 Sà@Ð �EÓ @Ø!HÐ ˜Ó HØ!+Ð ˜Ó +ô[˜ô [ð 6:òà+¨dÑ2ðð óðD(] <ð (]°Có (]ðV ð ¨¨c°3¨h©ó ñ  Ð1Ð3KÕ Lr%   