hedis
Safe HaskellNone
LanguageHaskell2010

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:

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

Documentation

data Hooks Source #

A collection of hook functions used by a connection.

Instances

Instances details
Show Hooks Source # 
Instance details

Defined in Database.Redis.Hooks

Methods

showsPrec :: Int -> Hooks -> ShowS #

show :: Hooks -> String #

showList :: [Hooks] -> ShowS #

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.

type ReceiveHook = IO Reply -> IO Reply Source #

A hook for receiving replies from the server.

defaultHooks :: Hooks Source #

The default hooks.

Every hook is the identity function, so installing defaultHooks has no effect on behavior.