{-# LANGUAGE GeneralizedNewtypeDeriving #-}

module System.Metrics.Prometheus.RegistryT where

import           Control.Applicative                        ((<$>))
import           Control.Monad.IO.Class                     (MonadIO, liftIO)
import           Control.Monad.Trans.Class                  (MonadTrans)
import           Control.Monad.Trans.State.Strict           (StateT (..),
                                                             evalStateT,
                                                             execStateT, get)

import           System.Metrics.Prometheus.Metric.Counter   (Counter)
import           System.Metrics.Prometheus.Metric.Gauge     (Gauge)
import           System.Metrics.Prometheus.Metric.Histogram (Histogram)
import qualified System.Metrics.Prometheus.Metric.Histogram as Histogram
import           System.Metrics.Prometheus.MetricId         (Labels, Name)
import           System.Metrics.Prometheus.Registry         (Registry,
                                                             RegistrySample,
                                                             new)
import qualified System.Metrics.Prometheus.Registry         as R


newtype RegistryT m a =
    RegistryT { RegistryT m a -> StateT Registry m a
unRegistryT :: StateT Registry m a }
    deriving (Applicative (RegistryT m)
a -> RegistryT m a
Applicative (RegistryT m)
-> (forall a b.
    RegistryT m a -> (a -> RegistryT m b) -> RegistryT m b)
-> (forall a b. RegistryT m a -> RegistryT m b -> RegistryT m b)
-> (forall a. a -> RegistryT m a)
-> Monad (RegistryT m)
RegistryT m a -> (a -> RegistryT m b) -> RegistryT m b
RegistryT m a -> RegistryT m b -> RegistryT m b
forall a. a -> RegistryT m a
forall a b. RegistryT m a -> RegistryT m b -> RegistryT m b
forall a b. RegistryT m a -> (a -> RegistryT m b) -> RegistryT m b
forall (m :: * -> *). Monad m => Applicative (RegistryT m)
forall (m :: * -> *) a. Monad m => a -> RegistryT m a
forall (m :: * -> *) a b.
Monad m =>
RegistryT m a -> RegistryT m b -> RegistryT m b
forall (m :: * -> *) a b.
Monad m =>
RegistryT m a -> (a -> RegistryT m b) -> RegistryT m b
forall (m :: * -> *).
Applicative m
-> (forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
return :: a -> RegistryT m a
$creturn :: forall (m :: * -> *) a. Monad m => a -> RegistryT m a
>> :: RegistryT m a -> RegistryT m b -> RegistryT m b
$c>> :: forall (m :: * -> *) a b.
Monad m =>
RegistryT m a -> RegistryT m b -> RegistryT m b
>>= :: RegistryT m a -> (a -> RegistryT m b) -> RegistryT m b
$c>>= :: forall (m :: * -> *) a b.
Monad m =>
RegistryT m a -> (a -> RegistryT m b) -> RegistryT m b
$cp1Monad :: forall (m :: * -> *). Monad m => Applicative (RegistryT m)
Monad, m a -> RegistryT m a
(forall (m :: * -> *) a. Monad m => m a -> RegistryT m a)
-> MonadTrans RegistryT
forall (m :: * -> *) a. Monad m => m a -> RegistryT m a
forall (t :: (* -> *) -> * -> *).
(forall (m :: * -> *) a. Monad m => m a -> t m a) -> MonadTrans t
lift :: m a -> RegistryT m a
$clift :: forall (m :: * -> *) a. Monad m => m a -> RegistryT m a
MonadTrans, Functor (RegistryT m)
a -> RegistryT m a
Functor (RegistryT m)
-> (forall a. a -> RegistryT m a)
-> (forall a b.
    RegistryT m (a -> b) -> RegistryT m a -> RegistryT m b)
-> (forall a b c.
    (a -> b -> c) -> RegistryT m a -> RegistryT m b -> RegistryT m c)
-> (forall a b. RegistryT m a -> RegistryT m b -> RegistryT m b)
-> (forall a b. RegistryT m a -> RegistryT m b -> RegistryT m a)
-> Applicative (RegistryT m)
RegistryT m a -> RegistryT m b -> RegistryT m b
RegistryT m a -> RegistryT m b -> RegistryT m a
RegistryT m (a -> b) -> RegistryT m a -> RegistryT m b
(a -> b -> c) -> RegistryT m a -> RegistryT m b -> RegistryT m c
forall a. a -> RegistryT m a
forall a b. RegistryT m a -> RegistryT m b -> RegistryT m a
forall a b. RegistryT m a -> RegistryT m b -> RegistryT m b
forall a b. RegistryT m (a -> b) -> RegistryT m a -> RegistryT m b
forall a b c.
(a -> b -> c) -> RegistryT m a -> RegistryT m b -> RegistryT m c
forall (m :: * -> *). Monad m => Functor (RegistryT m)
forall (m :: * -> *) a. Monad m => a -> RegistryT m a
forall (m :: * -> *) a b.
Monad m =>
RegistryT m a -> RegistryT m b -> RegistryT m a
forall (m :: * -> *) a b.
Monad m =>
RegistryT m a -> RegistryT m b -> RegistryT m b
forall (m :: * -> *) a b.
Monad m =>
RegistryT m (a -> b) -> RegistryT m a -> RegistryT m b
forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> c) -> RegistryT m a -> RegistryT m b -> RegistryT m c
forall (f :: * -> *).
Functor f
-> (forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
<* :: RegistryT m a -> RegistryT m b -> RegistryT m a
$c<* :: forall (m :: * -> *) a b.
Monad m =>
RegistryT m a -> RegistryT m b -> RegistryT m a
*> :: RegistryT m a -> RegistryT m b -> RegistryT m b
$c*> :: forall (m :: * -> *) a b.
Monad m =>
RegistryT m a -> RegistryT m b -> RegistryT m b
liftA2 :: (a -> b -> c) -> RegistryT m a -> RegistryT m b -> RegistryT m c
$cliftA2 :: forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> c) -> RegistryT m a -> RegistryT m b -> RegistryT m c
<*> :: RegistryT m (a -> b) -> RegistryT m a -> RegistryT m b
$c<*> :: forall (m :: * -> *) a b.
Monad m =>
RegistryT m (a -> b) -> RegistryT m a -> RegistryT m b
pure :: a -> RegistryT m a
$cpure :: forall (m :: * -> *) a. Monad m => a -> RegistryT m a
$cp1Applicative :: forall (m :: * -> *). Monad m => Functor (RegistryT m)
Applicative, a -> RegistryT m b -> RegistryT m a
(a -> b) -> RegistryT m a -> RegistryT m b
(forall a b. (a -> b) -> RegistryT m a -> RegistryT m b)
-> (forall a b. a -> RegistryT m b -> RegistryT m a)
-> Functor (RegistryT m)
forall a b. a -> RegistryT m b -> RegistryT m a
forall a b. (a -> b) -> RegistryT m a -> RegistryT m b
forall (m :: * -> *) a b.
Functor m =>
a -> RegistryT m b -> RegistryT m a
forall (m :: * -> *) a b.
Functor m =>
(a -> b) -> RegistryT m a -> RegistryT m b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> RegistryT m b -> RegistryT m a
$c<$ :: forall (m :: * -> *) a b.
Functor m =>
a -> RegistryT m b -> RegistryT m a
fmap :: (a -> b) -> RegistryT m a -> RegistryT m b
$cfmap :: forall (m :: * -> *) a b.
Functor m =>
(a -> b) -> RegistryT m a -> RegistryT m b
Functor, Monad (RegistryT m)
Monad (RegistryT m)
-> (forall a. IO a -> RegistryT m a) -> MonadIO (RegistryT m)
IO a -> RegistryT m a
forall a. IO a -> RegistryT m a
forall (m :: * -> *).
Monad m -> (forall a. IO a -> m a) -> MonadIO m
forall (m :: * -> *). MonadIO m => Monad (RegistryT m)
forall (m :: * -> *) a. MonadIO m => IO a -> RegistryT m a
liftIO :: IO a -> RegistryT m a
$cliftIO :: forall (m :: * -> *) a. MonadIO m => IO a -> RegistryT m a
$cp1MonadIO :: forall (m :: * -> *). MonadIO m => Monad (RegistryT m)
MonadIO)


evalRegistryT :: Monad m => RegistryT m a -> m a
evalRegistryT :: RegistryT m a -> m a
evalRegistryT = (StateT Registry m a -> Registry -> m a)
-> Registry -> StateT Registry m a -> m a
forall a b c. (a -> b -> c) -> b -> a -> c
flip StateT Registry m a -> Registry -> m a
forall (m :: * -> *) s a. Monad m => StateT s m a -> s -> m a
evalStateT Registry
new (StateT Registry m a -> m a)
-> (RegistryT m a -> StateT Registry m a) -> RegistryT m a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RegistryT m a -> StateT Registry m a
forall (m :: * -> *) a. RegistryT m a -> StateT Registry m a
unRegistryT


execRegistryT :: Monad m => RegistryT m a -> m Registry
execRegistryT :: RegistryT m a -> m Registry
execRegistryT = (StateT Registry m a -> Registry -> m Registry)
-> Registry -> StateT Registry m a -> m Registry
forall a b c. (a -> b -> c) -> b -> a -> c
flip StateT Registry m a -> Registry -> m Registry
forall (m :: * -> *) s a. Monad m => StateT s m a -> s -> m s
execStateT Registry
new (StateT Registry m a -> m Registry)
-> (RegistryT m a -> StateT Registry m a)
-> RegistryT m a
-> m Registry
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RegistryT m a -> StateT Registry m a
forall (m :: * -> *) a. RegistryT m a -> StateT Registry m a
unRegistryT


runRegistryT :: Monad m => RegistryT m a -> m (a, Registry)
runRegistryT :: RegistryT m a -> m (a, Registry)
runRegistryT = (StateT Registry m a -> Registry -> m (a, Registry))
-> Registry -> StateT Registry m a -> m (a, Registry)
forall a b c. (a -> b -> c) -> b -> a -> c
flip StateT Registry m a -> Registry -> m (a, Registry)
forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
runStateT Registry
new (StateT Registry m a -> m (a, Registry))
-> (RegistryT m a -> StateT Registry m a)
-> RegistryT m a
-> m (a, Registry)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RegistryT m a -> StateT Registry m a
forall (m :: * -> *) a. RegistryT m a -> StateT Registry m a
unRegistryT


withRegistry :: MonadIO m => (Registry -> m (a, Registry)) -> RegistryT m a
withRegistry :: (Registry -> m (a, Registry)) -> RegistryT m a
withRegistry = StateT Registry m a -> RegistryT m a
forall (m :: * -> *) a. StateT Registry m a -> RegistryT m a
RegistryT (StateT Registry m a -> RegistryT m a)
-> ((Registry -> m (a, Registry)) -> StateT Registry m a)
-> (Registry -> m (a, Registry))
-> RegistryT m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Registry -> m (a, Registry)) -> StateT Registry m a
forall s (m :: * -> *) a. (s -> m (a, s)) -> StateT s m a
StateT


registerCounter :: MonadIO m => Name -> Labels -> RegistryT m Counter
registerCounter :: Name -> Labels -> RegistryT m Counter
registerCounter Name
n Labels
l = (Registry -> m (Counter, Registry)) -> RegistryT m Counter
forall (m :: * -> *) a.
MonadIO m =>
(Registry -> m (a, Registry)) -> RegistryT m a
withRegistry (IO (Counter, Registry) -> m (Counter, Registry)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Counter, Registry) -> m (Counter, Registry))
-> (Registry -> IO (Counter, Registry))
-> Registry
-> m (Counter, Registry)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Name -> Labels -> Registry -> IO (Counter, Registry)
R.registerCounter Name
n Labels
l)


registerGauge :: MonadIO m => Name -> Labels -> RegistryT m Gauge
registerGauge :: Name -> Labels -> RegistryT m Gauge
registerGauge Name
n Labels
l = (Registry -> m (Gauge, Registry)) -> RegistryT m Gauge
forall (m :: * -> *) a.
MonadIO m =>
(Registry -> m (a, Registry)) -> RegistryT m a
withRegistry (IO (Gauge, Registry) -> m (Gauge, Registry)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Gauge, Registry) -> m (Gauge, Registry))
-> (Registry -> IO (Gauge, Registry))
-> Registry
-> m (Gauge, Registry)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Name -> Labels -> Registry -> IO (Gauge, Registry)
R.registerGauge Name
n Labels
l)


registerHistogram :: MonadIO m => Name -> Labels -> [Histogram.UpperBound] -> RegistryT m Histogram
registerHistogram :: Name -> Labels -> [UpperBound] -> RegistryT m Histogram
registerHistogram Name
n Labels
l [UpperBound]
u = (Registry -> m (Histogram, Registry)) -> RegistryT m Histogram
forall (m :: * -> *) a.
MonadIO m =>
(Registry -> m (a, Registry)) -> RegistryT m a
withRegistry (IO (Histogram, Registry) -> m (Histogram, Registry)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Histogram, Registry) -> m (Histogram, Registry))
-> (Registry -> IO (Histogram, Registry))
-> Registry
-> m (Histogram, Registry)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Name
-> Labels -> [UpperBound] -> Registry -> IO (Histogram, Registry)
R.registerHistogram Name
n Labels
l [UpperBound]
u)


sample :: Monad m => RegistryT m (IO RegistrySample)
sample :: RegistryT m (IO RegistrySample)
sample = Registry -> IO RegistrySample
R.sample (Registry -> IO RegistrySample)
-> RegistryT m Registry -> RegistryT m (IO RegistrySample)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> StateT Registry m Registry -> RegistryT m Registry
forall (m :: * -> *) a. StateT Registry m a -> RegistryT m a
RegistryT StateT Registry m Registry
forall (m :: * -> *) s. Monad m => StateT s m s
get