o
    wig                     @   s   d dl m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 d dlmZ d dlmZmZ eeZd	efd
dZe		ddedee
 dee
 d	eeddf fddZdS )    )contextmanager)	GeneratorOptional)OneLoggerProvider)Recorder)
Attributes)assert_that)
get_logger)SpanSpanNamereturnc                   C   s
   t  jS )zReturn the singleton recorder.

    This is a simply a shortcut for getting the recorder used by the timed_span context manager.
    )r   instancerecorder r   r   Y/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/nv_one_logger/api/timed_span.pyget_recorder   s   
r   Nnamespan_attributesstart_event_attributesc              
   c   s~    t  }t|d |j| ||d}z'z|V  W n ty0 } z||d|  d| |d}~ww W || dS || w )a  
        Context manager for recording a span.

        Note that OneLoggerProvider.instance().configure() must be called once per process (e.g., at app start up) before using the timed_span context manager.
        See the "How to use One Logger" section of the README.md file for more information.

        Args:
            name: The name of the span.
            span_attributes: Optional attributes to add to the span.
            start_event_attributes: Optional attributes to add to the start event.

        Yields:
            the span


        Usage:
        On app start up:
        ```python
            from nv_one_logger.api.timed_span import get_recorder, timed_span

            def main():
                # One-time initialization of the library
                recorder = ... # You can use a factory that takes some config parameters and builds a recorder.
                            # Or in simple use cases, just use recorder = DefaultRecorder(exporters=[...])
                OneLoggerProvider.instance().configure(recorder)
        ```

        In other parts of the application:
        ```python
            from nv_one_logger.api.timed_span import get_recorder, timed_span

            def foo():
                with timed_span(name="foo", start_event_attributes=Attributes({...})):
                    # business logic for operation foo()
        ```

        or for more advanced use cases, you can access the recorder directly for recording events and errors:
        ```python

            def bar():
                with timed_span(name="application", span_attributes=Attributes({"app_tag": "some tag"})) as span:
                    # some business logic

                    # You can record events of interests within a timed span.
                    get_recorder().event(span, Event.create(...))

                    # Or record errors
                    if(...):
                        get_recorder().error(span, "Error in bar", ...)

                    # some more business logic

    ```

    zcOne Logger is not configured! Use OneLoggerProvider.instance().configure() to configure One Logger.)	span_namer   r   z	Error in z: N)r   r   start	Exceptionerrorstop)r   r   r   r   spaner   r   r   
timed_span   s   =

r   )NN)
contextlibr   typingr   r   %nv_one_logger.api.one_logger_providerr   nv_one_logger.api.recorderr   nv_one_logger.core.attributesr   nv_one_logger.core.exceptionsr   #nv_one_logger.core.internal.loggingr	   nv_one_logger.core.spanr
   r   __name___loggerr   r   r   r   r   r   <module>   s,   