-- |-- Module : Network.Metric.Sink.Statsd-- Copyright : (c) 2012 Brendan Hay <brendan@soundcloud.com>-- License : This Source Code Form is subject to the terms of-- the Mozilla Public License, v. 2.0.-- A copy of the MPL can be found in the LICENSE file or-- you can obtain it at http://mozilla.org/MPL/2.0/.-- Maintainer : Brendan Hay <brendan@soundcloud.com>-- Stability : experimental-- Portability : non-portable (GHC extensions)--moduleNetwork.Metric.Sink.Statsd(-- * Sink Functionsopen,Sink(push,close)-- * Re-exports,Group,Bucket,MetricSink(..))whereimportControl.Monad(liftM)importNetwork.Socket(SocketType(..))importSystem.Random(randomRIO)importNetwork.Metric.InternalimportqualifiedData.ByteString.Char8asBSimportqualifiedData.ByteString.Lazy.Char8asBL-- | The sample status of a metricdataSampled=Sampled|Exact|Ignore-- | A handle to a Statsd sinkdataStatsd=StatsdHandlederiving(Show)instanceSinkStatsdwherepush(Statsdh)m=encm>>=hPushhwhereenc(Countergbv)=putgbv"c"1.0enc(Gaugegbv)=putgbv"g"1.0enc(Timergbv)=putgbv"ms"1.0close(Statsdh)=hCloseh---- API---- | Open a new Statsd sinkopen::String->String->IOMetricSinkopen=fOpenStatsdDatagram---- Private---- | Encode a metric into the Statsd format-- *TODO:* Currently statsd sampling is not exposed via the global metric typeput::Encodablea=>Group->Bucket->a->BS.ByteString->Double->IOBL.ByteStringputgroupbucketvaluetyprate=liftMbstr(randomRIO(0.0,1.0))wherebase=[keygroupbucket,":",encodevalue,"|",typ]bstrn=BL.fromChunks$casesampleratenofSampled->base++["@",BS.pack$showrate]Exact->baseIgnore->[]sample::Double->Double->Sampledsampleraterand|rate<1.0&&rand<=rate=Sampled|rate==1.0=Exact|otherwise=Ignore