o
    i                     @   s:   d dl Z d dlZd dlZd dlmZ G dd dejZdS )    N)Optionalc                   @   sB   e Zd ZdZdejdefddZd
dejdee defdd	Z	dS )StructuredJSONFormatterz
    A structured JSON formatter for flare logs using the native orjson encoder.
    Converts log records to structured JSON format for better parsing and analysis.
    recordreturnc                 C   st  |  || j|j|j|j|j|j|j| |j	|j
|j|jd}|jr+| |j|d< |jr6| |j|d< h d}|j D ]E\}}||vr|ds|durt|tttfszt|||< W q? ty   zt|||< W n ty|   d||< Y nw Y q?w |||< q?zt|W S  ty } z"|  || j|j|jd| d	|  d
d}t|W  Y d}~S d}~ww )z
        Format a log record as structured JSON.

        Args:
            record: The log record to format

        Returns:
            JSON formatted log string
        )	timestamplevelloggerfilenamemodulefuncNamelinenomessagethread
threadNameprocessprocessName	exception
stack_info>   msgargsnamemsecsr   r
   r   createdlevelnor   r   exc_infoexc_textr	   r   pathname	levelname
getMessager   r   r   relativeCreated_Nunserializable_valuezJSON formatting failed: z. Original message: json_formatting_failed)r   r   r   r   error)
formatTimedatefmtr   r   r	   r
   r   r   r   r   r   r   r   r   formatExceptionr   formatStack__dict__items
startswith
isinstancestrintbool	Exceptionreprjsondumps)selfr   log_dataskip_fieldskeyvalueefallback_data r:   Y/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/internal/flare/json_formatter.pyformat   sZ   zStructuredJSONFormatter.formatNr%   c                 C   s,   t |j}t d|dt|jdd S )z
        Format the time for the log record in ISO format.

        Args:
            record: The log record
            datefmt: Date format string (ignored, always uses ISO format)

        Returns:
            ISO formatted timestamp string
        z%Y-%m-%dT%H:%M:%S.03dZ)timegmtimer   strftimer-   r   )r3   r   r%   ctr:   r:   r;   r$   d   s    z"StructuredJSONFormatter.formatTime)N)
__name__
__module____qualname____doc__logging	LogRecordr,   r<   r   r$   r:   r:   r:   r;   r      s    "Wr   )r1   rH   r@   typingr   	Formatterr   r:   r:   r:   r;   <module>   s
    