
    g                         d Z ddlZddlmZmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZmZ dZdad	 Zd
 Z G d de      Z G d de      Z G d de      Z e       Z G d de      Z G d de      Zy)a  
This module implements experimental propagators to inject trace context
into response carriers. This is useful for server side frameworks that start traces
when server requests and want to share the trace context with the client so the
client can add its spans to the same trace.

This is part of an upcoming W3C spec and will eventually make it to the Otel spec.

https://w3c.github.io/trace-context/#trace-context-http-response-headers-format
    N)ABCabstractmethod)trace)Context)textmap)format_span_idformat_trace_idzAccess-Control-Expose-Headersc                      t         S N_RESPONSE_PROPAGATOR     ^/var/www/openai/venv/lib/python3.12/site-packages/opentelemetry/instrumentation/propagators.pyget_global_response_propagatorr   &   s    r   c                     | a y r   r   )
propagators    r   set_global_response_propagatorr   *   s    %r   c                       e Zd Zed        Zy)Setterc                      y)z.Inject the provided key value pair in carrier.Nr   selfcarrierkeyvalues       r   setz
Setter.set0       r   N)__name__
__module____qualname__r   r   r   r   r   r   r   /   s    = =r   r   c                       e Zd Zd Zy)DictHeaderSetterc                 D    |j                  |d      }|r| d| }|||<   y )N z, )get)r   r   r   r   	old_values        r   r   zDictHeaderSetter.set6   s.    KKR(	 kE7+Er   N)r   r    r!   r   r   r   r   r#   r#   5   s    r   r#   c                       e Zd ZdZd Zd Zy)
FuncSetteraz  FuncSetter converts a function into a valid Setter. Any function that
    can set values in a carrier can be converted into a Setter by using
    FuncSetter. This is useful when injecting trace context into non-dict
    objects such HTTP Response objects for different framework.

    For example, it can be used to create a setter for Falcon response object
    as:

        setter = FuncSetter(falcon.api.Response.append_header)

    and then used with the propagator as:

        propagator.inject(falcon_response, setter=setter)

    This would essentially make the propagator call `falcon_response.append_header(key, value)`
    c                     || _         y r   _func)r   funcs     r   __init__zFuncSetter.__init__O   s	    
r   c                 *    | j                  |||       y r   r+   r   s       r   r   zFuncSetter.setR   s    

7C'r   N)r   r    r!   __doc__r.   r   r   r   r   r)   r)   =   s    "(r   r)   c            
       v    e Zd Zedefdej                  dej                  e	   dej                  ddfd       Zy)ResponsePropagatorNr   contextsetterreturnc                      y)3Injects SpanContext into the HTTP response carrier.Nr   )r   r   r3   r4   s       r   injectzResponsePropagator.injectZ   r   r   )r   r    r!   r   default_setterr   CarrierTtypingOptionalr   r   r8   r   r   r   r2   r2   Y   s[     -1!/	B!!B )B 	B
 
B Br   r2   c            	       p    e Zd ZdZdefdej                  dej                  e	   dej                  ddfdZy)TraceResponsePropagatorzFExperimental propagator that injects tracecontext into HTTP responses.Nr   r3   r4   r5   c                 B   t        j                  |      }|j                         }|t         j                  k(  ryd}|j	                  ||dt        |j                         dt        |j                         d|j                  d       |j	                  |t        |       y)r7   Ntraceresponsez00--02x)r   get_current_spanget_span_contextINVALID_SPAN_CONTEXTr   r	   trace_idr   span_idtrace_flags*_HTTP_HEADER_ACCESS_CONTROL_EXPOSE_HEADERS)r   r   r3   r4   spanspan_contextheader_names          r   r8   zTraceResponsePropagator.injectg   s     %%g.,,.5555%

/,"7"789>,J^J^;_:``abnbzbz{~a  A	

 	

6	
r   )r   r    r!   r0   r9   r   r:   r;   r<   r   r   r8   r   r   r   r>   r>   d   sK    P
 -1!/	
!!
 )
 	

 

r   r>   )r0   r;   abcr   r   opentelemetryr   opentelemetry.context.contextr   opentelemetry.propagatorsr   opentelemetry.tracer   r	   rI   r   r   r   r   r#   r)   r9   r2   r>   r   r   r   <module>rR      s}   	  #  1 - ?-L *  &
=S =v ( (2 "#B B
0 
r   