Ferricstore.SlowLog (ferricstore v0.3.1)

Copy Markdown View Source

ETS-backed ring buffer that records commands whose execution time exceeds a configurable threshold.

Mirrors the Redis SLOWLOG facility: each entry captures a monotonically increasing ID, a Unix timestamp (microseconds), the wall-clock duration (microseconds), and the command with its arguments.

Configuration (application env)

  • :slowlog_log_slower_than_us -- threshold in microseconds; commands taking longer than this are logged. Default: 10_000 (10 ms). Set to 0 to log every command, or -1 to disable.
  • :slowlog_max_len -- maximum number of entries kept in the ring buffer. Default: 128. When full, the oldest entry is evicted.

Ownership

This module is a GenServer that owns the ETS table :ferricstore_slowlog. It must be started in the application supervision tree before any command dispatch can call maybe_log/3.

Summary

Types

A single slow log entry.

Functions

Returns a specification to start this module under a supervisor.

Returns the last count slow log entries, newest first.

Returns the number of entries currently in the slow log.

Returns the configured maximum number of entries.

Records a command if its duration exceeds the configured threshold.

Clears all entries from the slow log and resets the ID counter.

Updates the slowlog max length in both Application env and persistent_term.

Updates the slowlog threshold in both Application env and persistent_term.

Starts the SlowLog GenServer and creates the backing ETS table.

Returns the configured threshold in microseconds.

Types

entry()

@type entry() ::
  {id :: non_neg_integer(), timestamp_us :: integer(),
   duration_us :: non_neg_integer(), command :: [binary()]}

A single slow log entry.

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

get(count \\ nil)

@spec get(non_neg_integer() | nil) :: [entry()]

Returns the last count slow log entries, newest first.

When count is nil or omitted, returns all entries up to max_len.

len()

@spec len() :: non_neg_integer()

Returns the number of entries currently in the slow log.

max_len()

@spec max_len() :: pos_integer()

Returns the configured maximum number of entries.

Reads from persistent_term (~5ns) rather than Application.get_env.

maybe_log(command, duration_us, metadata \\ nil)

@spec maybe_log([binary()], non_neg_integer(), term()) :: :ok

Records a command if its duration exceeds the configured threshold.

This function is designed to be called from the hot dispatch path. When the threshold is -1 (disabled), this is a no-op.

Parameters

  • command -- list of binaries, e.g. ["SET", "key", "value"]
  • duration_us -- execution time in microseconds
  • _metadata -- reserved for future use (client address, etc.)

reset()

@spec reset() :: :ok

Clears all entries from the slow log and resets the ID counter.

set_max_len(value)

@spec set_max_len(non_neg_integer()) :: :ok

Updates the slowlog max length in both Application env and persistent_term.

Called by CONFIG SET and may be called from tests.

set_threshold(value)

@spec set_threshold(integer()) :: :ok

Updates the slowlog threshold in both Application env and persistent_term.

Called by CONFIG SET and may be called from tests.

start_link(opts \\ [])

@spec start_link(keyword()) :: GenServer.on_start()

Starts the SlowLog GenServer and creates the backing ETS table.

threshold()

@spec threshold() :: integer()

Returns the configured threshold in microseconds.

A value of -1 means slow logging is disabled.

Reads from persistent_term (~5ns) rather than Application.get_env (~100-200ns ETS lookup). The persistent_term is initialized at GenServer startup and updated whenever the threshold changes via set_threshold/1 or CONFIG SET.