
    $jY                     >   d 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mZ ddlmZmZ deeef         fd	Zdeeef         fd
Zdedz  fdZe	 ddededededededz  dee
ddf         fd            Zededededee
ddf         fd            Zg dZdS )zServer-side telemetry helpers.    )	Generator)contextmanager)request_ctx)Context)SpanSpanKindStatus
StatusCode)extract_trace_context
get_tracerreturnc                      ddl m}  i }	  |             }|r5|j        r
|j        |d<   |j        rd                    |j                  |d<   n# t
          $ r Y nw xY w|S )z>Get auth attributes for the current request, if authenticated.r   )get_access_tokenz
enduser.id zenduser.scope)fastmcp.server.dependenciesr   	client_idscopesjoinRuntimeError)r   attrstokens      `/home/rurouni/.hermes/hermes-agent/venv/lib/python3.11/site-packages/fastmcp/server/telemetry.pyget_auth_span_attributesr      s    <<<<<<E  "" 	@ 6&+ol#| @),%,)?)?o&   Ls   AA 
AAc                  ~    ddl m}  i }	  |             }|j        |j        
|j        |d<   n# t          $ r Y nw xY w|S )z/Get session attributes for the current request.r   )get_contextNzmcp.session.id)r   r   request_context
session_idr   )r   r   ctxs      r   get_session_span_attributesr      sl    777777Ekmm*s~/I&)nE"#   Ls   "- 
::Nc                      	 t          j                    } | r8t          | d          r(| j        r!t	          t          | j                            S n# t          $ r Y nw xY wdS )zCGet parent trace context from request meta for distributed tracing.metaN)r   gethasattrr!   r   dictLookupError)req_ctxs    r   _get_parent_trace_contextr'   ,   st    /## 	=ww// 	=GL 	=(gl););<<<   4s   AA 
AAnamemethodserver_namecomponent_typecomponent_keyresource_uric           	   #     K   t                      }|                    | t                      t          j                  5 }d||||||dt                      t                      }|||d<   |                    |           	 |V  nT# t          $ rG}	|	                    |	           |
                    t          t          j                              d}	~	ww xY w	 ddd           dS # 1 swxY w Y   dS )zCreate a SERVER span with standard MCP attributes and auth context.

    Automatically records any exception on the span and sets error status.
    )contextkindmcp)z
rpc.systemzrpc.servicez
rpc.methodzmcp.method.namezfastmcp.server.namezfastmcp.component.typefastmcp.component.keyNzmcp.resource.uri)r   start_as_current_spanr'   r   SERVERr   r   set_attributes	Exceptionrecord_exception
set_statusr	   r
   ERROR)
r(   r)   r*   r+   r,   r-   tracerspanr   es
             r   server_spanr=   7   ss      \\F		%	%)++_ 
& 
 
  
  & %#.&4%2!
 !
 '((!
 *++!
 #(4E$%E"""	JJJJ 	 	 	!!!$$$OOF:#344555	 /                 s7   AC*BC*
CACCC**C.1C.provider_typec              #   n  K   t                      }|                    d|            5 }|                    ||d           	 |V  nT# t          $ rG}|                    |           |                    t          t          j                              d}~ww xY w	 ddd           dS # 1 swxY w Y   dS )zCreate an INTERNAL span for provider delegation.

    Used by FastMCPProvider when delegating to mounted servers.
    Automatically records any exception on the span and sets error status.
    z	delegate )zfastmcp.provider.typer2   N)	r   r3   r5   r6   r7   r8   r	   r
   r9   )r(   r>   r,   r:   r;   r<   s         r   delegate_spanr@   c   s      \\F		%	%&8$&8&8	9	9 T)6)6 	
 	
 	
	JJJJ 	 	 	!!!$$$OOF:#344555	                  s6   B*AB*
BABBB**B.1B.)r@   r   r   r=   )N)__doc__collections.abcr   
contextlibr   mcp.server.lowlevel.serverr   opentelemetry.contextr   opentelemetry.tracer   r   r	   r
   fastmcp.telemetryr   r   r$   strr   r   r'   r=   r@   __all__     r   <module>rL      s   $ $ % % % % % % % % % % % % 2 2 2 2 2 2 ) ) ) ) ) ) B B B B B B B B B B B B ? ? ? ? ? ? ? ?$sCx.    "T#s(^    7T>       $( (
(( ( 	(
 ( *( tT4 ( ( ( (V 
  tT4 	   4  rK   