Skip to content

Commit

Permalink
Add reforwarding option (via configuration) to cardano-tracer
Browse files Browse the repository at this point in the history
  • Loading branch information
mtullsen committed May 30, 2023
1 parent f170cb5 commit 1608e78
Show file tree
Hide file tree
Showing 10 changed files with 197 additions and 99 deletions.
4 changes: 2 additions & 2 deletions cardano-tracer/cardano-tracer.cabal
Expand Up @@ -40,8 +40,6 @@ library
Cardano.Tracer.Acceptors.Server
Cardano.Tracer.Acceptors.Utils

Cardano.Tracer.Handlers.Datapoints.Run

Cardano.Tracer.Handlers.Logs.File
Cardano.Tracer.Handlers.Logs.Journal
Cardano.Tracer.Handlers.Logs.Rotator
Expand All @@ -53,6 +51,8 @@ library
Cardano.Tracer.Handlers.Metrics.Servers
Cardano.Tracer.Handlers.Metrics.Utils

Cardano.Tracer.Handlers.ReForwarder

Cardano.Tracer.Handlers.RTView.Notifications.Check
Cardano.Tracer.Handlers.RTView.Notifications.Email
Cardano.Tracer.Handlers.RTView.Notifications.Send
Expand Down
9 changes: 5 additions & 4 deletions cardano-tracer/src/Cardano/Tracer/Configuration.hs
Expand Up @@ -90,11 +90,12 @@ data TracerConfig = TracerConfig
, loRequestNum :: !(Maybe Word16) -- ^ How many 'TraceObject's will be asked in each request.
, ekgRequestFreq :: !(Maybe Pico) -- ^ How often to request for EKG-metrics, in seconds.
, hasEKG :: !(Maybe (Endpoint, Endpoint)) -- ^ Endpoint for EKG web-page (list of nodes, monitoring).
, hasPrometheus :: !(Maybe Endpoint) -- ^ Endpoint for Promeheus web-page.
, hasPrometheus :: !(Maybe Endpoint) -- ^ Endpoint for Prometheus web-page.
, hasRTView :: !(Maybe Endpoint) -- ^ Endpoint for RTView web-page.
, hasForwarding :: !(Maybe (Network, Log.TraceOptionForwarder))
-- ^ Socket for tracer's own forwarding,
-- and the forwarder config.
, hasForwarding :: !(Maybe ( Network -- ^ Socket for tracer's to reforward on,
, Maybe [[Text]] -- ^ Reforward logs with these prefixes
, Log.TraceOptionForwarder -- ^ The forwarder config.
))
, logging :: !(NonEmpty LoggingParams) -- ^ Logging parameters.
, rotation :: !(Maybe RotationParams) -- ^ Rotation parameters.
, verbosity :: !(Maybe Verbosity) -- ^ Verbosity of the tracer itself.
Expand Down
34 changes: 18 additions & 16 deletions cardano-tracer/src/Cardano/Tracer/Environment.hs
Expand Up @@ -4,6 +4,7 @@ module Cardano.Tracer.Environment

import Control.Concurrent.Extra (Lock)

import Cardano.Logging.Types
import Cardano.Tracer.Configuration
import Cardano.Tracer.Handlers.RTView.Notifications.Types
import Cardano.Tracer.Handlers.RTView.State.Historical
Expand All @@ -14,20 +15,21 @@ import Cardano.Tracer.Types

-- | Environment for all functions.
data TracerEnv = TracerEnv
{ teConfig :: !TracerConfig
, teConnectedNodes :: !ConnectedNodes
, teConnectedNodesNames :: !ConnectedNodesNames
, teAcceptedMetrics :: !AcceptedMetrics
, teSavedTO :: !SavedTraceObjects
, teBlockchainHistory :: !BlockchainHistory
, teResourcesHistory :: !ResourcesHistory
, teTxHistory :: !TransactionsHistory
, teCurrentLogLock :: !Lock
, teCurrentDPLock :: !Lock
, teEventsQueues :: !EventsQueues
, teDPRequestors :: !DataPointRequestors
, teProtocolsBrake :: !ProtocolsBrake
, teRTViewPageOpened :: !WebPageStatus
, teRTViewStateDir :: !(Maybe FilePath)
, teTracer :: !(Trace IO TracerTrace)
{ teConfig :: !TracerConfig
, teConnectedNodes :: !ConnectedNodes
, teConnectedNodesNames :: !ConnectedNodesNames
, teAcceptedMetrics :: !AcceptedMetrics
, teSavedTO :: !SavedTraceObjects
, teBlockchainHistory :: !BlockchainHistory
, teResourcesHistory :: !ResourcesHistory
, teTxHistory :: !TransactionsHistory
, teCurrentLogLock :: !Lock
, teCurrentDPLock :: !Lock
, teEventsQueues :: !EventsQueues
, teDPRequestors :: !DataPointRequestors
, teProtocolsBrake :: !ProtocolsBrake
, teRTViewPageOpened :: !WebPageStatus
, teRTViewStateDir :: !(Maybe FilePath)
, teTracer :: !(Trace IO TracerTrace)
, teReforwardTraceObjects :: !([TraceObject] -> IO ())
}
36 changes: 0 additions & 36 deletions cardano-tracer/src/Cardano/Tracer/Handlers/Datapoints/Run.hs

This file was deleted.

Expand Up @@ -35,6 +35,9 @@ traceObjectsHandler tracerEnv nodeId traceObjects = do
JournalMode -> writeTraceObjectsToJournal nodeName traceObjects
whenJust hasRTView . const $
saveTraceObjects teSavedTO nodeId traceObjects
teReforwardTraceObjects traceObjects

where
TracerEnv{teConfig, teCurrentLogLock, teSavedTO} = tracerEnv
TracerEnv{teConfig, teCurrentLogLock, teSavedTO, teReforwardTraceObjects}
= tracerEnv
TracerConfig{logging, verbosity, hasRTView} = teConfig
119 changes: 119 additions & 0 deletions cardano-tracer/src/Cardano/Tracer/Handlers/ReForwarder.hs
@@ -0,0 +1,119 @@
{-# LANGUAGE ImportQualifiedPost #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveAnyClass #-}

-- | This module initializes a reforwarding service for use by
-- cardano-tracer. It could [re-] serve the three miniprotocols on
-- a new local socket. Currently,
-- - it creates a new Datapoint store: this has a single datapoint
-- empty but datapoints could be added here.
-- - it reforwards trace messages to the new socket, optionally
-- filtering trace messages.
-- - it does not (currently) reforward EKG to the new socket.

module Cardano.Tracer.Handlers.ReForwarder
( initReForwarder
) where

import Control.Monad(when)
import Data.Aeson
import Data.List (isPrefixOf)
import qualified Data.Text as Text
import GHC.Generics

import Ouroboros.Network.Magic (NetworkMagic (..))
import Ouroboros.Network.NodeToClient (withIOManager)

import Cardano.Logging.Forwarding
import Cardano.Logging.Trace
import Cardano.Logging.Tracer.DataPoint
import Cardano.Logging.Types qualified as Log
import Trace.Forward.Utils.DataPoint
import Trace.Forward.Utils.TraceObject (writeToSink,ForwardSink)

import Cardano.Tracer.Configuration
import Cardano.Tracer.MetaTrace

-- | Initialize the reforwarding service if configured to be active.
-- Returns
-- - the function by which logging sources report their log messages
-- - the DataPoint tracer (for the data point store associated with
-- the forwarding trace server).
initReForwarder :: TracerConfig
-> Log.Trace IO TracerTrace
-> IO ( [Log.TraceObject] -> IO ()
, Trace IO DataPoint
)
initReForwarder TracerConfig{networkMagic, hasForwarding}
teTracer = do
mForwarding <- case hasForwarding of
Nothing -> pure Nothing
Just x -> case x of
(ConnectTo{}, _, _) ->
error "initReForwarder: unsupported mode of operation: ConnectTo. Use AcceptAt."
(AcceptAt (LocalSocket socket), mFwdNames, forwConf) -> do
(fwdsink, dpStore :: DataPointStore) <- withIOManager $ \iomgr -> do
traceWith teTracer TracerStartedReforwarder
initForwarding iomgr forwConf
(NetworkMagic networkMagic)
Nothing
(Just (socket, Log.Responder))
pure $ Just ( filteredWriteToSink fwdsink mFwdNames
, dataPointTracer @IO dpStore
)

let traceDP = case mForwarding of
Just (_,tr) -> tr
Nothing -> mempty

modeDP :: Trace IO ReforwarderMode
<- mkDataPointTracer traceDP
traceWith modeDP $ RM "running"
-- Note: currently the only trace for this datapoint

let writesToSink' =
case mForwarding of
Just (writeToSink',_) ->
mapM_ writeToSink'
_ ->
const $ return ()

return (writesToSink', traceDP)

filteredWriteToSink :: ForwardSink Log.TraceObject
-> Maybe [[Text.Text]]
-> Log.TraceObject -> IO ()
filteredWriteToSink fwdsink mFwdNames =
case mFwdNames of
Nothing ->
writeToSink fwdsink

Just fwdNames ->
\logObj->
when (any (`isPrefixOf` Log.toNamespace logObj) fwdNames) $
writeToSink fwdsink logObj

------------------------------------------------------------------------------
-- ReforwarderMode datapoint: type and boilerplate
--

-- | Mode of the reforwarder
newtype ReforwarderMode = RM String
deriving (Eq,Ord,Read,Show,Generic)

deriving instance ToJSON ReforwarderMode

-- | give the 'ReforwarderMode' type a place in the Datapoint Namespace:
instance Log.MetaTrace ReforwarderMode
where
namespaceFor _ = Log.Namespace [] ["Reforwarder","Mode"]
severityFor _ _ = Just Info
documentFor _ = Just "the mode of the reforwarder"
allNamespaces = [ Log.namespaceFor (undefined :: ReforwarderMode) ]


8 changes: 4 additions & 4 deletions cardano-tracer/src/Cardano/Tracer/MetaTrace.hs
Expand Up @@ -50,7 +50,7 @@ data TracerTrace
| TracerStartedAcceptors
{ ttAcceptorsAddr :: !Network }
| TracerStartedRTView
| TracerStartedDataPoints
| TracerStartedReforwarder
| TracerSockListen
{ ttListenAt :: !FilePath }
| TracerSockIncoming
Expand Down Expand Up @@ -101,7 +101,7 @@ instance MetaTrace TracerTrace where
namespaceFor TracerStartedPrometheus = Namespace [] ["StartedPrometheus"]
namespaceFor TracerStartedAcceptors {} = Namespace [] ["StartedAcceptors"]
namespaceFor TracerStartedRTView = Namespace [] ["StartedRTView"]
namespaceFor TracerStartedDataPoints = Namespace [] ["StartedDataPoints"]
namespaceFor TracerStartedReforwarder = Namespace [] ["StartedReforwarder"]
namespaceFor TracerSockListen {} = Namespace [] ["SockListen"]
namespaceFor TracerSockIncoming {} = Namespace [] ["SockIncoming"]
namespaceFor TracerSockConnecting {} = Namespace [] ["SockConnecting"]
Expand All @@ -120,7 +120,7 @@ instance MetaTrace TracerTrace where
severityFor (Namespace _ ["StartedPrometheus"]) _ = Just Info
severityFor (Namespace _ ["StartedAcceptors"]) _ = Just Info
severityFor (Namespace _ ["StartedRTView"]) _ = Just Info
severityFor (Namespace _ ["StartedDataPoints"]) _ = Just Info
severityFor (Namespace _ ["StartedReforwarder"]) _ = Just Info
severityFor (Namespace _ ["SockListen"]) _ = Just Info
severityFor (Namespace _ ["SockIncoming"]) _ = Just Info
severityFor (Namespace _ ["SockConnecting"]) _ = Just Info
Expand All @@ -143,7 +143,7 @@ instance MetaTrace TracerTrace where
, Namespace [] ["StartedPrometheus"]
, Namespace [] ["StartedAcceptors"]
, Namespace [] ["StartedRTView"]
, Namespace [] ["StartedDataPoints"]
, Namespace [] ["StartedReforwarder"]
, Namespace [] ["SockListen"]
, Namespace [] ["SockIncoming"]
, Namespace [] ["SockConnecting"]
Expand Down
44 changes: 26 additions & 18 deletions cardano-tracer/src/Cardano/Tracer/Run.hs
@@ -1,4 +1,9 @@
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE PackageImports #-}

-- | This top-level module is used by 'cardano-tracer' app.
module Cardano.Tracer.Run
Expand All @@ -17,14 +22,15 @@ import Cardano.Tracer.Configuration
import Cardano.Tracer.Environment
import Cardano.Tracer.Handlers.Logs.Rotator
import Cardano.Tracer.Handlers.Metrics.Servers
import Cardano.Tracer.Handlers.Datapoints.Run
import Cardano.Tracer.Handlers.ReForwarder
import Cardano.Tracer.Handlers.RTView.Run
import Cardano.Tracer.Handlers.RTView.State.Historical
import Cardano.Tracer.Handlers.RTView.Update.Historical
import Cardano.Tracer.MetaTrace
import Cardano.Tracer.Types
import Cardano.Tracer.Utils


-- | Top-level run function, called by 'cardano-tracer' app.
runCardanoTracer :: TracerParams -> IO ()
runCardanoTracer TracerParams{tracerConfig, stateDir, logSeverity} = do
Expand Down Expand Up @@ -65,25 +71,28 @@ doRunCardanoTracer config rtViewStateDir tr protocolsBrake dpRequestors = do

rtViewPageOpened <- newTVarIO False

(reforwardTraceObject,_trDataPoint) <- initReForwarder config tr

-- Environment for all following functions.
let tracerEnv =
TracerEnv
{ teConfig = config
, teConnectedNodes = connectedNodes
, teConnectedNodesNames = connectedNodesNames
, teAcceptedMetrics = acceptedMetrics
, teSavedTO = savedTO
, teBlockchainHistory = chainHistory
, teResourcesHistory = resourcesHistory
, teTxHistory = txHistory
, teCurrentLogLock = currentLogLock
, teCurrentDPLock = currentDPLock
, teEventsQueues = eventsQueues
, teDPRequestors = dpRequestors
, teProtocolsBrake = protocolsBrake
, teRTViewPageOpened = rtViewPageOpened
, teRTViewStateDir = rtViewStateDir
, teTracer = tr
{ teConfig = config
, teConnectedNodes = connectedNodes
, teConnectedNodesNames = connectedNodesNames
, teAcceptedMetrics = acceptedMetrics
, teSavedTO = savedTO
, teBlockchainHistory = chainHistory
, teResourcesHistory = resourcesHistory
, teTxHistory = txHistory
, teCurrentLogLock = currentLogLock
, teCurrentDPLock = currentDPLock
, teEventsQueues = eventsQueues
, teDPRequestors = dpRequestors
, teProtocolsBrake = protocolsBrake
, teRTViewPageOpened = rtViewPageOpened
, teRTViewStateDir = rtViewStateDir
, teTracer = tr
, teReforwardTraceObjects = reforwardTraceObject
}

-- Specify what should be done before 'cardano-tracer' stops.
Expand All @@ -100,5 +109,4 @@ doRunCardanoTracer config rtViewStateDir tr protocolsBrake dpRequestors = do
, runMetricsServers tracerEnv
, runAcceptors tracerEnv
, runRTView tracerEnv
, runDatapoints config tr
]

0 comments on commit 1608e78

Please sign in to comment.