Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ByteString instead of String for stats callback #159

Merged
merged 1 commit into from
Dec 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Kafka/Callbacks.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Kafka.Callbacks
)
where

import Data.ByteString (ByteString)
import Kafka.Internal.RdKafka (rdKafkaConfSetErrorCb, rdKafkaConfSetLogCb, rdKafkaConfSetStatsCb)
import Kafka.Internal.Setup (HasKafkaConf(..), getRdKafkaConf)
import Kafka.Types (KafkaError(..), KafkaLogLevel(..))
Expand Down Expand Up @@ -39,7 +40,7 @@ logCallback callback k =
let realCb _ = callback . toEnum
in rdKafkaConfSetLogCb (getRdKafkaConf k) realCb

-- | Add a callback for stats.
-- | Add a callback for stats. The passed ByteString contains an UTF-8 encoded JSON document and can e.g. be parsed using Data.Aeson.decodeStrict. For more information about the content of the JSON document see <https://github.com/edenhill/librdkafka/blob/master/STATISTICS.md>.
--
-- ==== __Examples__
--
Expand All @@ -49,7 +50,7 @@ logCallback callback k =
-- >
-- > myStatsCallback :: String -> IO ()
-- > myStatsCallback stats = print $ show stats
statsCallback :: HasKafkaConf k => (String -> IO ()) -> k -> IO ()
statsCallback :: HasKafkaConf k => (ByteString -> IO ()) -> k -> IO ()
statsCallback callback k =
let realCb _ = callback
in rdKafkaConfSetStatsCb (getRdKafkaConf k) realCb
8 changes: 5 additions & 3 deletions src/Kafka/Internal/RdKafka.chs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

module Kafka.Internal.RdKafka where

import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
import Data.Text (Text)
import qualified Data.Text as Text
import Control.Monad (liftM)
Expand All @@ -15,7 +17,7 @@ import Foreign.Storable (Storable(..))
import Foreign.Ptr (Ptr, FunPtr, castPtr, nullPtr)
import Foreign.ForeignPtr (FinalizerPtr, addForeignPtrFinalizer, newForeignPtr_, withForeignPtr)
import Foreign.C.Error (Errno(..), getErrno)
import Foreign.C.String (CString, newCString, withCAString, peekCAString, peekCAStringLen, peekCString)
import Foreign.C.String (CString, newCString, withCAString, peekCAString, peekCString)
import Foreign.C.Types (CFile, CInt(..), CSize, CChar)
import System.IO (Handle, stdin, stdout, stderr)
import System.Posix.IO (handleToFd)
Expand Down Expand Up @@ -414,7 +416,7 @@ rdKafkaConfSetLogCb conf cb = do

---- Stats Callback
type StatsCallback' = Ptr RdKafkaT -> CString -> CSize -> Word8Ptr -> IO ()
type StatsCallback = Ptr RdKafkaT -> String -> IO ()
type StatsCallback = Ptr RdKafkaT -> ByteString -> IO ()

foreign import ccall safe "wrapper"
mkStatsCallback :: StatsCallback' -> IO (FunPtr StatsCallback')
Expand All @@ -424,7 +426,7 @@ foreign import ccall safe "rd_kafka.h rd_kafka_conf_set_stats_cb"

rdKafkaConfSetStatsCb :: RdKafkaConfTPtr -> StatsCallback -> IO ()
rdKafkaConfSetStatsCb conf cb = do
cb' <- mkStatsCallback $ \k j jl _ -> peekCAStringLen (j, cIntConv jl) >>= cb k
cb' <- mkStatsCallback $ \k j jl _ -> BS.packCStringLen (j, cIntConv jl) >>= cb k
withForeignPtr conf $ \c -> rdKafkaConfSetStatsCb' c cb'
return ()

Expand Down