module Network.Metrics.Graphite (
open
, Group
, Bucket
, Value
, MetricType(..)
, Metric(..)
, MetricSink(push)
, close
) where
import Control.Monad (liftM)
import Network.Socket
import Data.Time.Clock.POSIX
import Network.Metrics.Internal
import qualified Data.ByteString.Char8 as BS
import qualified Data.ByteString.Lazy.Char8 as BL
data Graphite = Graphite Handle
instance MetricSink Graphite where
push m (Graphite h) = encode m >>= flip hPush h
close (Graphite h) = hClose h
open :: String -> String -> IO Graphite
open host port = liftM Graphite (hOpen Stream host port)
encode :: Metric -> IO BL.ByteString
encode (Metric _ g b v) = liftM bstr getPOSIXTime
where
bucket = BS.concat [g, ".", b]
timestamp n = BS.pack $ show (truncate n :: Integer)
bstr n = BL.fromChunks [bucket, v, timestamp n]