
    g                        d dl m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
mZmZ d dlmZ d dlmZmZ d dlmZmZ d d	lmZ d d
lmZ ddlmZmZ  G d de      Zy)    )annotationsN)Sequence)Path)AnyMappingcast)trace)EventReadableSpan)SpanExporterSpanExportResult)ResourceAttributes)SpanAttributes   )ATTRIBUTES_SPAN_TYPE_KEY$RESOURCE_ATTRIBUTES_PACKAGE_VERSIONSc                  b    e Zd ZdZdZddZd	dZddZ	 	 	 	 	 	 	 d
	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZy)TestExporterzKA SpanExporter that stores exported spans in a list for asserting in tests.Fc                    g | _         y Nexported_spansselfs    U/var/www/openai/venv/lib/python3.12/site-packages/logfire/_internal/exporters/test.py__init__zTestExporter.__init__   s
    24    c                X    | j                   j                  |       t        j                  S )z"Exports a batch of telemetry data.)r   extendr   SUCCESS)r   spanss     r   exportzTestExporter.export   s"    ""5)'''r   c                    g | _         y)zClears the collected spans.Nr   r   s    r   clearzTestExporter.clear!   s
     r   c                D   dfddfddddfddddfddfd	}| j                   D 	cg c]
  }	 ||	       }
}	|
D 	cg c]1  }	|d
u s)|	j                  di       j                  t        d      dk7  r|	3 c}	S c c}	w c c}	w )a\  The exported spans as a list of dicts.

        Args:
            fixed_line_number: The line number to use for all spans.
            strip_filepaths: Whether to strip the filepaths from the exported spans.
            include_resources: Whether to include the resource attributes in the exported spans.
            include_package_versions: Whether to include the package versions in the exported spans.
            include_instrumentation_scope: Whether to include the instrumentation scope in the exported spans.

        Returns:
            A list of dicts representing the exported spans.
        c                   | dk(  rr	 t        |      j                  S | dk(  rS | dk(  r)t        j                  dk\  rr|j                  d      d   S | t        j                  k(  r|t        j                         k(  sJ y| t        j                  k(  rt        j                  d|      ry	|S # t        $ r |cY S w xY w)
Nzcode.filepathzcode.linenozcode.function)      .i  z^[0-9a-f]{32}$ 00000000000000000000000000000000)r   name
ValueErrorsysversion_infosplitr   PROCESS_PIDosgetpidSERVICE_INSTANCE_IDrematch)r,   value_strip_function_qualnamefixed_line_numberstrip_filepathss     r   process_attributez>TestExporter.exported_spans_as_dict.<locals>.process_attribute<   s    &?!;+++ }$):)F((&##w.3K ;;s+B//)555		+++)===88-u5#L " ! L!s   B/ /B=<B=
attributesc           	         | y | j                         D ci c]  \  }}|t        k7  sr
| ||       } }}d| v rd| d<   | S c c}}w )Nztelemetry.sdk.versionz0.0.0)itemsr   )r<   kvinclude_package_versionsr;   s      r   build_attributesz=TestExporter.exported_spans_as_dict.<locals>.build_attributesO   st    ! ',,..DAq<<@X $Q**.  
 '*46=
23s   Ac                J    | j                   | j                  | j                  dS )Ntrace_idspan_id	is_remoterD   )contexts    r   build_contextz:TestExporter.exported_spans_as_dict.<locals>.build_context[   s      ' 0 0W__[b[l[lmmr   c                v    | j                   xs t        j                  } |       | j                        dS )N)rH   r<   )rH   r	   INVALID_SPAN_CONTEXTr<   )linkrH   rB   rI   s     r   
build_linkz7TestExporter.exported_spans_as_dict.<locals>.build_link^   s4    ll@e&@&@G,W5EUVZVeVeEfggr   c           
     t   | j                   | j                  d}| j                  rt        | j                        x|d<   }t        j
                  |v rft        d t        t        t        | j                  t        j
                           j                  d            D              }||t        j
                  <   |S )N)r,   	timestampr<   c              3  \   K   | ]$  }|j                         r|j                          & y wr   )strip).0lines     r   	<genexpr>zKTestExporter.exported_spans_as_dict.<locals>.build_event.<locals>.<genexpr>g   s-      %%D  ::<	 

%s   *,
)r,   rO   r<   dictr   EXCEPTION_STACKTRACEnextreversedr   strr0   )eventresr<   	last_lines       r   build_eventz8TestExporter.exported_spans_as_dict.<locals>.build_eventb   s    +0::EOO"TC15e6F6F1GGL!J!66*D $ %$, e&6&6~7Z7Z&[\bbcgh%% !I GPJ~BBCJr   spanc                X    r&d| j                   xr | j                   j                  iS i S )Ninstrumentation_scope)ra   r,   )r_   include_instrumentation_scopes    r   build_instrumentation_scopezHTestExporter.exported_spans_as_dict.<locals>.build_instrumentation_scopeq   s.    ,/1K1K1oPTPjPjPoPopp	r   c                *   | j                   xs t        j                  }| j                   |      | j                  r | j                        nd | j
                  | j                  d 	|       d | j                        i}| j                  r#| j                  D cg c]
  } 
|       c}|d<   | j                  r#| j                  D cg c]
  } |       c}|d<   r# | j                  j                        }d|i|d<   |S c c}w c c}w )N)r,   rH   parent
start_timeend_timer<   linkseventsresource)rH   r	   rK   r,   re   rf   rg   r<   rh   ri   rj   )r_   rH   r\   rL   r[   resource_attributesrB   rI   r^   rc   rM   include_resourcess         r   
build_spanz7TestExporter.exported_spans_as_dict.<locals>.build_spanw   s    ll@e&@&@G		(18<-4"oo MM# .d3# .t?#C zz=AZZHZT
4 0ZHG{{AE MU!3 MH &6t}}7O7O&P# "5#J J  I Ms    DDTpending_span)r,   rZ   r7   r   returnr   )r<   zMapping[str, Any] | Nonero   zdict[str, Any] | None)rH   ztrace.SpanContextro   dict[str, Any])rL   z
trace.Linkro   rp   )r[   r
   ro   rp   )r_   r   ro   rp   )r   getr   )r   r9   r:   rl   rA   rb   _include_pending_spansr8   rm   r_   r!   rB   rI   r^   rc   rM   r;   s    ````` `   @@@@@@r   exported_spans_as_dictz#TestExporter.exported_spans_as_dict%   s    .	&
		n	h			 	, /3.A.AB.AdD!.AB 
%-r*../GPTbb 
 	
 C
s   
B6BN)ro   None)r!   zSequence[ReadableSpan]ro   r   ){   TFFFFT)r9   z
int | Noner:   boolrl   rv   rA   rv   rb   rv   rr   rv   r8   rv   ro   zlist[dict[str, Any]])	__name__
__module____qualname____doc____test__r   r"   r$   rs    r   r   r   r      s    U H5(
! ), $"')..3',)-n
%n
 n
  	n

 #'n
 (,n
 !%n
 #'n
 
n
r   r   )
__future__r   r2   r5   r.   collections.abcr   pathlibr   typingr   r   r   opentelemetryr	   opentelemetry.sdk.tracer
   r   opentelemetry.sdk.trace.exportr   r   opentelemetry.semconv.resourcer   opentelemetry.semconv.tracer   	constantsr   r   r   r|   r   r   <module>r      s=    " 	 	 
 $  % %  7 I = 6 V@
< @
r   