Skip to content

Commit

Permalink
Optimize Silence
Browse files Browse the repository at this point in the history
  • Loading branch information
jutaro committed Nov 29, 2022
1 parent 617ae5d commit 3232339
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
24 changes: 24 additions & 0 deletions trace-dispatcher/src/Cardano/Logging/Configuration.hs
Expand Up @@ -13,6 +13,8 @@ module Cardano.Logging.Configuration
, withBackendsFromConfig
, withLimitersFromConfig

, maybeSilent

, getSeverity
, getDetails
, getBackends
Expand Down Expand Up @@ -55,6 +57,28 @@ configureTracers config (Documented documented) tracers = do
documented


maybeSilent :: forall m a. (MonadIO m) =>
Namespace
-> Trace m a
-> m (Trace m a)
maybeSilent ns tr = do
ref <- liftIO (newIORef False)
pure $ Trace $ T.arrow $ T.emit $ mkTrace ref
where
mkTrace ref (lc, Right a) = do
silence <- liftIO $ readIORef ref
if silence
then pure ()
else T.traceWith (unpackTrace tr) (lc, Right a)
mkTrace ref (lc, Left (Config c)) = do
let val = isSilentTracer c ns
liftIO $ writeIORef ref val
T.traceWith (unpackTrace tr) (lc, Left (Config c))
mkTrace _ref (lc, Left other) =
T.traceWith (unpackTrace tr) (lc, Left other)

isSilentTracer :: TraceConfig -> Namespace -> Bool
isSilentTracer tc ns = getSeverity tc ns == SeverityF Nothing

-- | Take a selector function called 'extract'.
-- Take a function from trace to trace with this config dependent value.
Expand Down
4 changes: 2 additions & 2 deletions trace-dispatcher/src/Cardano/Logging/Tracer/Composed.hs
Expand Up @@ -88,8 +88,8 @@ mkCardanoTracer' trStdout trForward mbTrEkg tracerName namesFor severityFor priv
Nothing -> Trace NT.nullTracer
Just ekgTrace -> metricsFormatter "Cardano" ekgTrace
let metricsTrace' = filterTrace (\(_,v) -> asMetrics v /= []) metricsTrace
let hookedTrace = messageTrace'' <> metricsTrace'
hook hookedTrace
metricsTrace'' <- hook metricsTrace'
maybeSilent tracerName (messageTrace''' <> metricsTrace'')


where
Expand Down

0 comments on commit 3232339

Please sign in to comment.