RedisCluster.Monitor (redis_cluster v0.8.0)
View SourceA Redis monitoring client for debugging and testing purposes.
This module provides functionality to monitor Redis commands being executed on Redis cluster nodes. It collects commands internally and allows querying them on demand.
Warning: MONITOR has a significant performance impact on Redis servers. Use only for debugging and testing, never in production under load.
Example Usage
# Start monitoring a specific node
{:ok, monitor} = RedisCluster.Monitor.start_link(
host: "localhost",
port: 6379,
max_commands: 100 # Keep only the 100 most recent commands
)
# Let some commands execute...
Process.sleep(5000)
# Get the collected commands
commands = RedisCluster.Monitor.get_commands(monitor)
for command <- commands do
IO.puts("Command: " <> command.message)
end
# Clear the command history
RedisCluster.Monitor.clear_commands(monitor)
# Stop monitoring
RedisCluster.Monitor.stop(monitor)
Summary
Types
A function that filters commands based on the message struct.
Also receives the current count of commands and the total count of commands.
This can be used for advanced filtering, such reservoir sampling.
If the function returns :quit, the monitor will stop.
If the function returns :drop, the message will be dropped.
If the function returns :keep, the message will be added to the queue, though an older message may be dropped.
A reference returned when starting monitoring
Functions
Returns a specification to start this module under a supervisor.
Clears all collected commands from the monitor. Also resets the command count and total count.
Gets the collected monitoring commands from the monitor.
Starts monitoring on multiple cluster nodes based on role.
Starts monitoring on a single node in the cluster.
Returns a filter function that implements reservoir sampling.
Starts a monitor process.
Stops the monitor process.
Types
@type filter_fun() :: (RedisCluster.Monitor.Message.t(), current_count :: non_neg_integer(), total_count :: non_neg_integer() -> :keep | :drop | :quit)
A function that filters commands based on the message struct.
Also receives the current count of commands and the total count of commands.
This can be used for advanced filtering, such reservoir sampling.
If the function returns :quit, the monitor will stop.
If the function returns :drop, the message will be dropped.
If the function returns :keep, the message will be added to the queue, though an older message may be dropped.
@type monitor_ref() :: reference()
A reference returned when starting monitoring
Functions
Returns a specification to start this module under a supervisor.
See Supervisor.
@spec clear_commands(GenServer.server()) :: :ok
Clears all collected commands from the monitor. Also resets the command count and total count.
@spec get_commands(GenServer.server()) :: [map()]
Gets the collected monitoring commands from the monitor.
Returns a list of command maps, with the most recent commands first. Each command map contains:
:host- The Redis host:port- The Redis port:message- The raw monitoring message from Redis:timestamp- The timestamp when the command was captured (milliseconds)
@spec monitor_cluster_nodes(RedisCluster.Configuration.t(), Keyword.t()) :: {:ok, [ %{ host: String.t(), port: non_neg_integer(), role: atom(), monitor_pid: pid() } ]} | {:error, term()}
Starts monitoring on multiple cluster nodes based on role.
Returns {:ok, monitors} where monitors is a list of maps:
%{host: String.t(), port: non_neg_integer(), role: atom(), monitor_pid: pid()}.
@spec monitor_node(String.t(), non_neg_integer(), Keyword.t()) :: {:ok, pid()} | {:error, term()}
Starts monitoring on a single node in the cluster.
Returns {:ok, monitor_pid} where monitor_pid can be used to query commands
with get_commands/1.
Options
:host- Redis host (required):port- Redis port (required):max_commands- Maximum number of commands to keep in memory (optional, unlimited if nil, default 100):ssl- Enable SSL/TLS connections (default: false):ssl_opts- SSL options passed to :ssl.connect (default: [])
@spec reservoir_sample_fun( keep_count :: non_neg_integer(), total_limit :: non_neg_integer() ) :: filter_fun()
Returns a filter function that implements reservoir sampling.
The gist of reservoir sampling is that it has an equal probability of keeping each item. This is useful for sampling a large stream of items without knowing the total number of items in advance.
Parameters
keep_count- The number of items to keep.total_limit- The total number of items to check before quitting.
@spec start_link(Keyword.t()) :: GenServer.on_start()
Starts a monitor process.
Options
:host- Redis host (required):port- Redis port (required):max_commands- Maximum number of commands to keep in memory (d if nil, default 100):filter- A function that filters commands based on the message struct (optional):ssl- Enable SSL/TLS connections (default: false):ssl_opts- SSL options passed to :ssl.connect (default: [])
@spec stop(GenServer.server()) :: :ok
Stops the monitor process.