| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Database.Redis.Hooks
Description
Hooks for observing or wrapping Redis I/O.
Hooks are installed through connect defaultConnectInfo { connectHooks = ... }
and wrap the low-level actions used by hedis:
sendRequestHookwraps regular command execution.sendPubSubHookwraps pub/sub command sending.callbackHookwraps invocation of pub/sub callbacks.sendHookwraps raw bytes sent to the server.receiveHookwraps reply reception.
The common pattern is to start from defaultHooks and override only the
hook(s) you need. Each hook receives the original action and is expected to
call it after performing any extra work such as logging, tracing, metrics,
or timing.
Hooks can be used to alter existing behavior, or used to add metrics or telemetry to the redis application.
Example:
import Data.IORef
import Database.Redis
data Counts = Counts
{ sendRequestCount :: Word
, sendCount :: Word
, receiveCount :: Word
}
hooks :: IORef Counts -> Hooks
hooks ref =
defaultHooks
{ sendRequestHook = \run argv -> do
modifyIORef ref $ \c -> c { sendRequestCount = sendRequestCount c + 1 }
run argv
, sendHook = \sendBytes bytes -> do
modifyIORef ref $ \c -> c { sendCount = sendCount c + 1 }
sendBytes bytes
, receiveHook = \recvReply -> do
modifyIORef ref $ \c -> c { receiveCount = receiveCount c + 1 }
recvReply
}
main :: IO ()
main = do
ref <- newIORef (Counts 0 0 0)
conn <- connect defaultConnectInfo { connectHooks = hooks ref }
_ <- runRedis conn $ set "key" "value"
readIORef ref >>= print
Synopsis
- data Hooks = Hooks {}
- type SendRequestHook = ([ByteString] -> IO Reply) -> [ByteString] -> IO Reply
- type SendPubSubHook = ([ByteString] -> IO ()) -> [ByteString] -> IO ()
- type CallbackHook = (Message -> IO PubSub) -> Message -> IO PubSub
- type SendHook = (ByteString -> IO ()) -> ByteString -> IO ()
- type ReceiveHook = IO Reply -> IO Reply
- defaultHooks :: Hooks
Documentation
A collection of hook functions used by a connection.
Constructors
| Hooks | |
Fields | |
type SendRequestHook = ([ByteString] -> IO Reply) -> [ByteString] -> IO Reply Source #
A hook for sending commands to the server and receiving replies from the server.
This wraps the command-level request path used by most Redis commands.
type SendPubSubHook = ([ByteString] -> IO ()) -> [ByteString] -> IO () Source #
A hook for sending pub/sub messages to the server.
type CallbackHook = (Message -> IO PubSub) -> Message -> IO PubSub Source #
A hook for invoking callbacks with pub/sub messages.
type SendHook = (ByteString -> IO ()) -> ByteString -> IO () Source #
A hook for sending raw bytes to the server.
This sits below request rendering and can be used to observe the exact wire payload sent on the socket.
defaultHooks :: Hooks Source #
The default hooks.
Every hook is the identity function, so installing defaultHooks has no
effect on behavior.