module Network.Metrics.Graphite (
open
, MetricSink(push, close)
, Group
, Bucket
, Value
, MetricType(..)
, Metric(..)
) 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 deriving (Show)
instance MetricSink Graphite where
push m (Graphite h) = encode m >>= flip hPush h
close (Graphite h) = hClose h
open :: String -> String -> IO Sink
open host port = liftM (Sink . 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]