o
    Á¿ik  ã                   @   sn   d Z ddlZddlmZ ddlmZ ddlmZ ddlm	Z	 G dd„ deƒZ
d	d
„ ZejG dd„ deƒƒZdS )a<  
Span Links
==========

Description
-----------

``ddtrace.trace.SpanLink`` introduces a new causal relationship between spans.
This new behavior is analogous to OpenTelemetry Span Links:
https://opentelemetry.io/docs/concepts/signals/traces/#span-links


Usage
-----

SpanLinks can be set using :meth:`ddtrace.Span.link_span(...)` Ex::

    from ddtrace.trace import tracer

    s1 = tracer.trace("s1")
    s2 = tracer.trace("s2")

    link_attributes = {"link.name": "s1_to_s2", "link.kind": "scheduled_by", "key1": "val1"}
    s1.link_span(s2.context, link_attributes)
é    N)ÚEnum)ÚOptional)ÚSpanLinkData)Úflatten_key_valuec                   @   s   e Zd ZdZdZdS )ÚSpanLinkKindzw
    A collection of standard SpanLink kinds. It's possible to use others, but these should be used when possible.
    zspan-pointerN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚSPAN_POINTER© r   r   úM/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/_trace/_span_link.pyr   #   s    r   c                 C   s   |dkst |› d|› ƒ‚d S )Nr   z must be > 0. Value is )Ú
ValueError)ÚselfÚattribute_nameÚvaluer   r   r   Ú_id_not_zero+   s   ÿr   c                   @   sà   e Zd ZU dZeed< eed< dZee ed< dZ	ee ed< e
jedZeed< d	Zeed
< dd„ Zedd„ ƒZejdd„ ƒZedee fdd„ƒZejdeddfdd„ƒZdd„ Zdd„ Zdd„ Zdd„ Zdefdd„ZdS )ÚSpanLinkaB  
    TraceId [required]: The span's 128-bit Trace ID
    SpanId [required]: The span's 64-bit Span ID

    Flags [optional]: The span's trace-flags field, as defined in the W3C standard. If only sampling
    information is provided, the flags value must be 1 if the decision is keep, otherwise 0.

    TraceState [optional]: The span's tracestate field, as defined in the W3C standard.

    Attributes [optional]: Zero or more key-value pairs, where the key must be a non-empty string and the
    value is either a string, bool, number or an array of primitive type values.
    Útrace_idÚspan_idNÚ
tracestateÚflags)Údefault_factoryÚ
attributesr   Ú_dropped_attributesc                 C   s    t | d| jƒ t | d| jƒ d S )Nr   r   )r   r   r   ©r   r   r   r   Ú__post_init__F   s   zSpanLink.__post_init__c                 C   s
   | j d S ©Nz	link.name©r   r   r   r   r   ÚnameJ   s   
zSpanLink.namec                 C   ó   || j d< d S r   r   ©r   r   r   r   r   r   N   ó   Úreturnc                 C   s   | j  d¡S ©Nz	link.kind)r   Úgetr   r   r   r   ÚkindR   s   zSpanLink.kindr   c                 C   r    r$   r   r!   r   r   r   r&   V   r"   c                 C   s2   || j vrtd|› ƒ‚| j |= |  jd7  _d S )NzInvalid key: é   )r   ÚKeyErrorr   )r   Úkeyr   r   r   Ú_drop_attributeZ   s   
zSpanLink._drop_attributec                 C   sÔ   | j d›| jd›dœ}| jrLi |d< | j ¡ D ]4\}}t||ƒ ¡ D ](\}}t|tƒr2||d |< q"t|tƒrBt|ƒ ¡ |d |< q"t|ƒ|d |< q"q| j	dkrV| j	|d< | j
r^| j
|d< | jd urh| j|d< |S )	NÚ032xÚ016x)r   r   r   r   Údropped_attributes_countr   r   )r   r   r   Úitemsr   Ú
isinstanceÚstrÚboolÚlowerr   r   r   )r   ÚdÚkÚvÚk1Úv1r   r   r   Úto_dict`   s(   þ

ø





zSpanLink.to_dictc                 C   s
   t  | ¡S )N)ÚdataclassesÚasdictr   r   r   r   Ú__getstate__|   s   
zSpanLink.__getstate__c                 C   s   | j di |¤Ž d S )Nr   )Ú__init__)r   Ústater   r   r   Ú__setstate__   s   zSpanLink.__setstate__c                 C   s6   d| j › d| j› d| j› d| j› d| j› d| j› dS )NzSpanLink(trace_id=z
, span_id=z, attributes=z, tracestate=z, flags=z, dropped_attributes=ú))r   r   r   r   r   r   r   r   r   r   Ú__repr__‚   s   ÿÿÿÿzSpanLink.__repr__)r   r   r	   r
   ÚintÚ__annotations__r   r   r0   r   r9   ÚfieldÚdictr   r   r   Úpropertyr   Úsetterr&   r*   r8   r;   r>   r@   r   r   r   r   r   0   s,   
 

r   )r
   r9   Úenumr   Útypingr   Úddtrace.internal.native._nativer   Úddtrace.internal.utils.formatsr   r   r   Ú	dataclassr   r   r   r   r   Ú<module>   s    