o
    wi.                     @   s   d 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
mZmZ ddlmZmZmZ ddlmZ ddlmZ dd	lmZ dd
lmZ eeZG dd deZG dd deZG dd deZdS )Configuration for OneLogger.    N)Enum)Path)CallableDictLiteralOptionalUnion)	BaseModel
ConfigDictmodel_validator)Self)TelemetryConfig)AttributeValue)evaluate_valuec                   @   s   e Zd ZdZ	 dZ	 dZdS )OneLoggerErrorHandlingStrategya  Enum for the error handling strategy for OneLogger.

    This enum determines what happens when OneLogger encounters a fatal error (e.g., an exception in the instrumentation code or
    a problem with the OneLogger state). This does NOT affect handling of errors occuring when communicating with the telemetry backends
    (i.e., exporter failures, which are handled by the Recorder). Rather, this is about handling user errors when configuring OneLogger
    or bugs in telemetry code (e.g., assertion/invariant violations or hitting an inconsistent state).

    Our recommendation is to use PROPAGATE_EXCEPTIONS for development scenarios where you want maximum visibility into errors,
    or DISABLE_QUIETLY_AND_REPORT_METRIC_ERROR for production scenarios where training should continue even if logging fails.

    Note: If you don't explicitly specify a strategy, OneLogger will automatically apply smart defaults:
    - Enabled ranks (enable_for_current_rank=True): PROPAGATE_EXCEPTIONS (for error visibility)
    - Disabled ranks (enable_for_current_rank=False): DISABLE_QUIETLY_AND_REPORT_METRIC_ERROR (for safety)

    Read the docstrings for DefaultRecorder for more details on how errors from exporters (e.g., communication errors with telemetry
    backends) are handled when using that recorder.
    propagate_exceptions'disable_quietly_and_report_metric_errorN)__name__
__module____qualname____doc__PROPAGATE_EXCEPTIONS'DISABLE_QUIETLY_AND_REPORT_METRIC_ERROR r   r   U/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/nv_one_logger/api/config.pyr   !   s    r   c                   @   sn   e Zd ZU dZdZed ed< dZeed< dZ	e
eef ed< d	Ze
eef ed
< edddefddZdS )LoggerConfigz=Configuration for how OneLogger logs its messages and errors.INFODEBUGr   WARNINGERRORCRITICAL	log_levelz&%(name)s - %(levelname)s - %(message)s
log_formatzonelogger.loglog_file_path_for_infozonelogger.errlog_file_path_for_erraftermodereturnc                 C   s$   | j dvrtdd d| j  | S )zValidate the logger config.>   r   r   r!   r    r"   zlog_level must be one of r   z, got )r#   
ValueErrorselfr   r   r   validate_configS   s   
zLoggerConfig.validate_configN)r   r   r   r   r#   r   __annotations__r$   strr%   r	   r   r&   r   r   r.   r   r   r   r   r   F   s   
 r   c                   @   s8  e Zd ZU dZeddZeed< ee	e
g e	f f ed< ede	fddZeee
g ef f ed	< edefd
dZdZeee
g ef f ed< edefddZdZeeeef  ed< ejZeed< dZeed< e Zeed< dZeeef ed< dZee ed< eddedd Z eddde!fddZ"dS )OneLoggerConfigr   T)arbitrary_types_allowedapplication_nameworld_size_or_fnr*   c                 C   
   t | jS )z>Number of processes participating in the telemetry collection.)r   r4   r,   r   r   r   
world_sizeo   s   
zOneLoggerConfig.world_sizesession_tag_or_fnc                 C   r5   )zaGet the session tag.

        Returns:
            str: The evaluated session tag value.
        )r   r7   r,   r   r   r   session_tagy      
zOneLoggerConfig.session_tagFis_baseline_run_or_fnc                 C   r5   )znGet the baseline run flag.

        Returns:
            bool: The evaluated baseline run flag value.
        )r   r:   r,   r   r   r   is_baseline_run   r9   zOneLoggerConfig.is_baseline_runNcustom_metadataerror_handling_strategyenable_for_current_ranklogger_configz1.0.0summary_data_schema_versiontelemetry_configbeforer(   c                 C   s   t |trCd|v }|sC|ddsCttjdd}td| d tj	|d< d|vr0i |d< t |d trCd	|d vrCd
|d d	< |S )a	  Apply defaults for error handling strategy based on rank enablement.

        This applies production-safe defaults by automatically using quiet error handling
        for disabled ranks when the user hasn't explicitly specified an error handling strategy.
        r=   r>   TRANKr   zeOneLogger: Setting error_handling_strategy to DISABLE_QUIETLY_AND_REPORT_METRIC_ERROR for rank (rank=zY) with OneLogger disabled. To override: explicitly set error_handling_strategy parameter.r?   r#   r"   )

isinstancedictgetintosenvironloggerwarningr   r   )clsdatauser_set_error_handlingrankr   r   r   apply_error_handling_defaults   s   

z-OneLoggerConfig.apply_error_handling_defaultsr'   c                 C   sX   | j r| j  std| jdkrtd| jdur*| j D ]
}| s)tdq| S )a  Validate the OneLogger configuration.

        This validator ensures that:
        - application_name is not empty
        - world_size is set to a positive value
        - custom_metadata keys are valid strings (if provided)
        - telemetry_config implements the TelemetryConfig protocol (if provided)

        Returns:
            OneLoggerConfig: The validated configuration.

        Raises:
            ValueError: If the configuration is invalid.
        z3application_name cannot be empty or whitespace-onlyr   z*world_size must be set to a positive valueNz.custom_metadata keys must be non-empty strings)r3   stripr+   r6   r<   keys)r-   keyr   r   r   r.      s   

zOneLoggerConfig.validate_config)#r   r   r   r   r   model_configr0   r/   r	   rG   r   propertyr6   r8   r:   boolr;   r<   r   r   r   r   r   r=   r>   r   r?   r@   rA   r   r   classmethodrP   r   r.   r   r   r   r   r1   [   s0   
 

	r1   )r   loggingrH   enumr   pathlibr   typingr   r   r   r   r	   pydanticr
   r   r   typing_extensionsr   "nv_one_logger.api.telemetry_configr   nv_one_logger.core.attributesr   !nv_one_logger.core.internal.utilsr   	getLoggerr   rJ   r   r   r1   r   r   r   r   <module>   s   
%