Skip to main content

Snapshotter

Monitors and logs system resource usage at predefined intervals for performance optimization.

The class monitors and records the state of various system resources (CPU, memory, event loop, and client API) at predefined intervals. This continuous monitoring helps in identifying resource overloads and ensuring optimal performance of the application. It is utilized in the AutoscaledPool module to adjust task allocation dynamically based on the current demand and system load.

Index

Methods

__aenter__

  • Starts capturing snapshots at configured intervals.


    Returns Snapshotter

__aexit__

  • async __aexit__(*, exc_type, exc_value, exc_traceback): None
  • Stops all resource capturing.

    This method stops capturing snapshots of system resources (CPU, memory, event loop, and client information). It should be called to terminate resource capturing when it is no longer needed.


    Parameters

    • optionalkeyword-onlyexc_type: type[BaseException] | None
    • optionalkeyword-onlyexc_value: BaseException | None
    • optionalkeyword-onlyexc_traceback: TracebackType | None

    Returns None

__init__

  • __init__(*, event_manager, event_loop_snapshot_interval, client_snapshot_interval, max_used_cpu_ratio, max_memory_size, max_used_memory_ratio, max_event_loop_delay, max_client_errors, snapshot_history, available_memory_ratio, reserve_memory_ratio, memory_warning_cooldown_period, client_rate_limit_error_retry_count): None
  • A default constructor.


    Parameters

    • optionalkeyword-onlyevent_manager: EventManager

      The event manager used to emit system info events. From data provided by this event the CPU and memory usage are read.

    • optionalkeyword-onlyevent_loop_snapshot_interval: timedelta = timedelta(milliseconds=500)

      The interval at which the event loop is sampled.

    • optionalkeyword-onlyclient_snapshot_interval: timedelta = timedelta(milliseconds=1000)

      The interval at which the client is sampled.

    • optionalkeyword-onlymax_used_cpu_ratio: float = 0.95

      Sets the ratio, defining the maximum CPU usage. When the CPU usage is higher than the provided ratio, the CPU is considered overloaded.

    • optionalkeyword-onlymax_memory_size: ByteSize | None = None

      Sets the maximum amount of system memory to be used by the AutoscaledPool. If None is provided, the max amount of memory to be used is set to one quarter of total system memory. I.e. on a system with 8192 MB, the AutoscaledPool will only use up to 2048 MB of memory.

    • optionalkeyword-onlymax_used_memory_ratio: float = 0.9

      Sets the ratio, defining the maximum ratio of memory usage. When the memory usage is higher than the provided ratio of max_memory_size, the memory is considered overloaded.

    • optionalkeyword-onlymax_event_loop_delay: timedelta = timedelta(milliseconds=50)

      Sets the maximum delay of the event loop. When the delay is higher than the provided value, the event loop is considered overloaded.

    • optionalkeyword-onlymax_client_errors: int = 1

      Sets the maximum number of client errors (HTTP 429). When the number of client errors is higher than the provided number, the client is considered overloaded.

    • optionalkeyword-onlysnapshot_history: timedelta = timedelta(seconds=30)

      Sets the time interval for which the snapshots are kept.

    • optionalkeyword-onlyavailable_memory_ratio: float | None = None

      How big part of the system memory should be used if max_memory_size is not given.

    • optionalkeyword-onlyreserve_memory_ratio: float = 0.5

      Fraction of memory kept in reserve. Used to calculate critical memory overload threshold.

    • optionalkeyword-onlymemory_warning_cooldown_period: timedelta = timedelta(milliseconds=10000)

      Minimum time interval between logging successive critical memory overload warnings.

    • optionalkeyword-onlyclient_rate_limit_error_retry_count: int = 2

      Number of retries for a client request before considering it a failure due to rate limiting.

    Returns None

get_client_sample

  • get_client_sample(*, duration): list[Snapshot]
  • Returns a sample of the latest client snapshots.


    Parameters

    • optionalkeyword-onlyduration: timedelta | None = None

      The duration of the sample from the latest snapshot. If omitted, it returns a full history.

    Returns list[Snapshot]

get_cpu_sample

  • get_cpu_sample(*, duration): list[Snapshot]
  • Returns a sample of the latest CPU snapshots.


    Parameters

    • optionalkeyword-onlyduration: timedelta | None = None

      The duration of the sample from the latest snapshot. If omitted, it returns a full history.

    Returns list[Snapshot]

get_event_loop_sample

  • get_event_loop_sample(*, duration): list[Snapshot]
  • Returns a sample of the latest event loop snapshots.


    Parameters

    • optionalkeyword-onlyduration: timedelta | None = None

      The duration of the sample from the latest snapshot. If omitted, it returns a full history.

    Returns list[Snapshot]

get_memory_sample

  • get_memory_sample(*, duration): list[Snapshot]
  • Returns a sample of the latest memory snapshots.


    Parameters

    • optionalkeyword-onlyduration: timedelta | None = None

      The duration of the sample from the latest snapshot. If omitted, it returns a full history.

    Returns list[Snapshot]