{-# LANGUAGE ScopedTypeVariables #-}

module Cardano.RTView.Acceptor
    ( launchMetricsAcceptor
    ) where

import           Cardano.Prelude

import           Cardano.BM.Backend.Switchboard (Switchboard)
import qualified Cardano.BM.Backend.TraceAcceptor as TraceAcceptor
import           Cardano.BM.Configuration (Configuration)
import           Cardano.BM.IOManager
import           Cardano.BM.Plugin (loadPlugin)
import           Cardano.BM.Trace (Trace)

-- | It is assumed that there's at least one cardano-node process
--   that sends its metrics as |LogObject|s via |TraceForwarder|.
--   These |LogObject|s will be accepted by |TraceAcceptor|s and
--   redirected to the corresponding tracers and finally stored
--   in the |LogBuffer|.
launchMetricsAcceptor
  :: Configuration
  -> Trace IO Text
  -> Switchboard Text
  -> IO ()
launchMetricsAcceptor :: Configuration -> Trace IO Text -> Switchboard Text -> IO ()
launchMetricsAcceptor Configuration
config Trace IO Text
accTr Switchboard Text
switchBoard =
  (IOManager -> IO ()) -> IO ()
WithIOManager
withIOManager ((IOManager -> IO ()) -> IO ()) -> (IOManager -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \IOManager
iomgr -> do
    IOManager
-> Configuration
-> Trace IO Text
-> Switchboard Text
-> IO (Plugin Text)
forall (s :: * -> *) a.
(IsEffectuator s a, ToJSON a, FromJSON a) =>
IOManager -> Configuration -> Trace IO a -> s a -> IO (Plugin a)
TraceAcceptor.plugin IOManager
iomgr Configuration
config Trace IO Text
accTr Switchboard Text
switchBoard IO (Plugin Text) -> (Plugin Text -> IO ()) -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Switchboard Text -> Plugin Text -> IO ()
forall a. Switchboard a -> Plugin a -> IO ()
loadPlugin Switchboard Text
switchBoard
    IO () -> IO ()
forall (f :: * -> *) a b. Applicative f => f a -> f b
forever (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ Int -> IO ()
threadDelay Int
1000000