Redis.Monitor (Redis v0.8.0)

Copy Markdown View Source

Streams commands observed by Redis' MONITOR command.

Redis.Monitor owns a dedicated connection because a connection in monitor mode cannot be used for ordinary commands. Each record is parsed into a Redis.Monitor.Entry and sent to subscribers:

{:ok, monitor} = Redis.Monitor.start_link(port: 6379)
:ok = Redis.Monitor.subscribe(monitor)

receive do
  {:redis_monitor, %Redis.Monitor.Entry{} = entry} ->
    IO.inspect(entry)
end

A Redis URI can be passed instead of connection options:

Redis.Monitor.start_link("redis://:secret@localhost:6379/2")

Subscriber filters

Filters are applied independently for each subscriber:

Redis.Monitor.subscribe(monitor,
  commands: ["SET", "DEL"],
  database: 0,
  client: ~r/^127.0.0.1:/
)

Supported filters are:

  • :commands - a command or list of commands, matched case-insensitively
  • :database - a database number or list of database numbers
  • :client - an exact client identifier or a regular expression

Connection options

The connection options mirror Redis.Connection where they apply: :host, :port, :password, :username, :database, :ssl, :ssl_opts, :socket, :client_name, :credential_provider, :timeout, :sync_connect, :backoff_initial, and :backoff_max.

MONITOR exposes live application traffic and has a measurable performance cost. It should be protected and used deliberately in production.

Summary

Functions

Returns a child specification for a monitor process.

Starts a dedicated Redis MONITOR connection.

Stops the monitor connection.

Subscribes a process to parsed monitor entries.

Subscribes subscriber with optional per-subscriber filters.

Returns the currently subscribed process identifiers.

Unsubscribes a process from monitor entries.

Types

filter()

@type filter() ::
  {:commands, String.t() | [String.t()]}
  | {:database, non_neg_integer() | [non_neg_integer()]}
  | {:client, String.t() | Regex.t()}

Functions

child_spec(init_arg)

@spec child_spec(keyword() | String.t()) :: Supervisor.child_spec()

Returns a child specification for a monitor process.

start_link()

start_link(uri)

@spec start_link(keyword() | String.t()) :: GenServer.on_start()

Starts a dedicated Redis MONITOR connection.

stop(monitor)

@spec stop(GenServer.server()) :: :ok

Stops the monitor connection.

subscribe(monitor, subscriber_or_filters \\ self())

@spec subscribe(GenServer.server(), pid() | [filter()]) :: :ok | {:error, term()}

Subscribes a process to parsed monitor entries.

The second argument may be a subscriber pid or a filter keyword list.

subscribe(monitor, subscriber, filters)

@spec subscribe(GenServer.server(), pid(), [filter()]) :: :ok | {:error, term()}

Subscribes subscriber with optional per-subscriber filters.

subscribers(monitor)

@spec subscribers(GenServer.server()) :: [pid()]

Returns the currently subscribed process identifiers.

unsubscribe(monitor, subscriber \\ self())

@spec unsubscribe(GenServer.server(), pid()) :: :ok

Unsubscribes a process from monitor entries.