Skip to content

Commit

Permalink
With optimized configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
jutaro committed May 13, 2021
1 parent 9603e18 commit b822c07
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 20 deletions.
2 changes: 1 addition & 1 deletion trace-dispatcher/docs/trace-dispatcher.md
Expand Up @@ -393,7 +393,7 @@ We offer different traceOuts (backends):
* EKG (and Prometheus via EKG)
* Stdout

Since we want to get rid of any resource hungry computation in the node process, the most important backend in the actual node will be the Forwarding tracer, which forwards traces to a special logging process. The only other possibility in the node will be a simple Stdout Tracer.
Since we want to get rid of any resource hungry computation in the node process, the most important backend in the actual node will be the Forwarding tracer, which forwards traces to a special logging process. The only other possibility in the node will be a simple Stdout Tracer and an EKG store.

Katip is used for for writing human and machine readable log files. One basic choice is between a __human readable__ text representation, or a __machine readable__ object or JSON representation. This choice is made by sending the message either to a Katip tracer, which is configured for a human or machine readable configuration or to both.

Expand Down
68 changes: 49 additions & 19 deletions trace-dispatcher/src/Cardano/Logging/Configuration.hs
Expand Up @@ -12,10 +12,12 @@ module Cardano.Logging.Configuration
import Control.Monad.IO.Class (MonadIO, liftIO)
import qualified Control.Tracer as T
import Data.IORef (IORef, newIORef, readIORef, writeIORef)
import Data.List (maximumBy, nub)
import qualified Data.Map as Map
import Data.Maybe (catMaybes, fromMaybe, mapMaybe)
import Katip (Severity)


import Cardano.Logging.Trace (filterTraceByPrivacy,
filterTraceBySeverity)
import Cardano.Logging.Types
Expand All @@ -36,38 +38,48 @@ configureTracers config (Documented documented) tracers = do

-- | Take a selector function, and a function from trace to trace with
-- this selector to make a trace transformer with a config value
withNamespaceConfig :: (MonadIO m, Eq b) =>
withNamespaceConfig :: forall m a b. (MonadIO m, Eq b, Ord b) =>
(TraceConfig -> Namespace -> b)
-> (Maybe b -> Trace m a -> Trace m a)
-> Trace m a
-> m (Trace m a)
withNamespaceConfig extract needsConfigFunc tr = do
ref <- liftIO (newIORef (Left Map.empty))
ref <- liftIO (newIORef (Left (Map.empty, Nothing)))
pure $ Trace $ T.arrow $ T.emit $ mkTrace ref
where
mkTrace :: MonadIO m
=> IORef (Either (Map.Map Namespace b, Maybe b) b)
-> (LoggingContext, Maybe TraceControl, a)
-> m ()
mkTrace ref (lc, Nothing, a) = do
eitherConf <- liftIO $ readIORef ref
case eitherConf of
Right val ->
T.traceWith
(unpackTrace $ needsConfigFunc (Just val) tr) (lc, Nothing, a)
Left map -> case Map.lookup (lcNamespace lc) map of
Just val -> T.traceWith
(unpackTrace $ needsConfigFunc (Just val) tr)
(lc, Nothing, a)
Nothing -> error $ "Unconfigured trace with context "
++ show (lcNamespace lc)
Left (cmap, Just v) ->
case Map.lookup (lcNamespace lc) cmap of
Just val -> T.traceWith
(unpackTrace $ needsConfigFunc (Just val) tr)
(lc, Nothing, a)
Nothing -> T.traceWith
(unpackTrace $ needsConfigFunc (Just v) tr)
(lc, Nothing, a)

mkTrace ref (lc, Just Reset, a) = do
liftIO $ writeIORef ref (Left Map.empty)
liftIO $ writeIORef ref (Left (Map.empty, Nothing))
T.traceWith (unpackTrace $ needsConfigFunc Nothing tr) (lc, Just Reset, a)

mkTrace ref (lc, Just (Config c), m) = do
let ! val = extract c (lcNamespace lc)
eitherConf <- liftIO $ readIORef ref
case eitherConf of
Left map ->
case Map.lookup (lcNamespace lc) map of
Left (cmap, Nothing) ->
case Map.lookup (lcNamespace lc) cmap of
Nothing -> do
liftIO $ writeIORef ref $ Left (Map.insert (lcNamespace lc) val map)
liftIO
$ writeIORef ref
$ Left (Map.insert (lcNamespace lc) val cmap, Nothing)
T.traceWith
(unpackTrace $ needsConfigFunc (Just val) tr)
(lc, Just (Config c), m)
Expand All @@ -78,12 +90,15 @@ withNamespaceConfig extract needsConfigFunc tr = do
(lc, Just (Config c), m)
else error $ "Inconsistent trace configuration with context "
++ show (lcNamespace lc)
Right val -> error $ "Trace not reset before reconfiguration "
Right val -> error $ "Trace not reset before reconfiguration (1)"
++ show (lcNamespace lc)
Left (cmap, Just v) -> error $ "Trace not reset before reconfiguration (2)"
++ show (lcNamespace lc)

mkTrace ref (lc, Just Optimize, m) = do
eitherConf <- liftIO $ readIORef ref
case eitherConf of
Left cmap ->
Left (cmap, Nothing) ->
case Map.size cmap of
0 -> -- This will never be called!?
pure ()
Expand All @@ -95,11 +110,26 @@ withNamespaceConfig extract needsConfigFunc tr = do
(unpackTrace $ needsConfigFunc (Just val) tr)
(lc, Just Optimize, m)
_ -> error "Cardano.Logging.Configuration>>withConfig: Impossible"
_ -> T.traceWith
(unpackTrace $ needsConfigFunc Nothing tr)
(lc, Just Optimize, m)
Right val -> error $ "Trace not reset before reconfiguration "
++ show (lcNamespace lc)
n -> let eles = nub $ Map.elems cmap
decidingDict =
foldl
(\acc e -> Map.insertWith (\o _ -> o + 1) e 1 acc)
Map.empty eles
(mostCommon,_) = maximumBy (\(_, n) (_, m) -> compare n m)
(Map.assocs decidingDict)
newmap = Map.filter (/= mostCommon) cmap
in do
liftIO $ writeIORef ref (Left (newmap, Just mostCommon))
T.traceWith
(unpackTrace $ needsConfigFunc Nothing tr)
(lc, Just Optimize, m)
Right val -> T.traceWith
(unpackTrace $ needsConfigFunc Nothing tr)
(lc, Just Optimize, m)
Left (cmap, Just v) ->
T.traceWith
(unpackTrace $ needsConfigFunc Nothing tr)
(lc, Just Optimize, m)

-- | Filter a trace by severity and take the filter value from the config
filterSeverityFromConfig :: (MonadIO m) =>
Expand Down

0 comments on commit b822c07

Please sign in to comment.