Skip to content

Commit

Permalink
Improve statistics and send messages to EKG as well
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian Ospald committed Nov 16, 2020
1 parent 5ab7146 commit fe856ee
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
1 change: 1 addition & 0 deletions lib/cli/cardano-wallet-cli.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ library
aeson
, aeson-pretty
, ansi-terminal
, async
, base
, bytestring
, cardano-addresses
Expand Down
52 changes: 44 additions & 8 deletions lib/cli/src/Cardano/CLI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
Expand Down Expand Up @@ -98,14 +99,18 @@ module Cardano.CLI
import Prelude hiding
( getLine )

import Cardano.BM.Data.Configuration
( Endpoint(..) )
import Cardano.BM.Backend.Switchboard
( Switchboard )
import Cardano.BM.Configuration.Static
( defaultConfigStdout )
import Cardano.BM.Counters
( readCounters )
import Cardano.BM.Data.Configuration
( Endpoint (..) )
import Cardano.BM.Data.Counter
( Counter (..), nameCounter )
import Cardano.BM.Data.LogItem
( LoggerName )
( LOContent (..), LoggerName, PrivacyAnnotation (..), mkLOMeta )
import Cardano.BM.Data.Output
( ScribeDefinition (..)
, ScribeFormat (..)
Expand All @@ -115,12 +120,16 @@ import Cardano.BM.Data.Output
)
import Cardano.BM.Data.Severity
( Severity (..) )
import Cardano.BM.Data.SubTrace
( SubTrace (..) )
import Cardano.BM.Data.Tracer
( HasPrivacyAnnotation (..), HasSeverityAnnotation (..) )
import Cardano.BM.Plugin
( loadPlugin )
import Cardano.BM.Setup
( setupTrace_, shutdown )
import Cardano.BM.Trace
( Trace, appendName, logDebug )
( Trace, appendName, logDebug, traceNamedObject )
import Cardano.Mnemonic
( MkSomeMnemonic (..), SomeMnemonic (..) )
import Cardano.Wallet.Api.Client
Expand Down Expand Up @@ -184,10 +193,14 @@ import Control.Applicative
( optional, some, (<|>) )
import Control.Arrow
( first, left )
import Control.Concurrent
( threadDelay )
import Control.Exception
( bracket, catch )
import Control.Monad
( join, unless, void, when )
( forever, join, unless, void, when )
import Control.Monad.IO.Class
( MonadIO )
import Control.Tracer
( Tracer, traceWith )
import Data.Aeson
Expand Down Expand Up @@ -301,13 +314,14 @@ import System.IO
, stdin
, stdout
)
import Cardano.BM.Plugin (loadPlugin)

import qualified Cardano.BM.Configuration.Model as CM
import qualified Cardano.BM.Backend.EKGView as EKG
import qualified Cardano.BM.Configuration.Model as CM
import qualified Cardano.BM.Data.BackendKind as CM
import qualified Cardano.BM.Data.Observable as Obs
import qualified Command.Key as Key
import qualified Command.RecoveryPhrase as RecoveryPhrase
import qualified Control.Concurrent.Async as Async
import qualified Data.Aeson as Aeson
import qualified Data.Aeson.Encode.Pretty as Aeson
import qualified Data.Aeson.Types as Aeson
Expand Down Expand Up @@ -1542,16 +1556,38 @@ initTracer
initTracer loggerName outputs = do
cfg <- do
c <- defaultConfigStdout
CM.setSetupBackends c [CM.KatipBK, CM.AggregationBK, CM.EKGViewBK]
CM.setSetupBackends c [CM.KatipBK, CM.AggregationBK, CM.EKGViewBK, CM.EditorBK]
CM.setDefaultBackends c [CM.KatipBK, CM.EKGViewBK]
CM.setSetupScribes c $ map mkScribe outputs
CM.setDefaultScribes c $ map mkScribeId outputs
CM.setEKGBindAddr c $ Just (Endpoint ("127.0.0.1", 13788))
CM.setPrometheusBindAddr c $ Just ("127.0.0.1", 13798)
CM.setBackends c "cardano-wallet.metrics" (Just [CM.EKGViewBK])
pure c
(tr, sb) <- setupTrace_ cfg loggerName
EKG.plugin cfg tr sb >>= loadPlugin sb
startCapturingMetrics tr
pure (sb, (cfg, tr))

where
startCapturingMetrics :: Trace IO Text -> IO ()
startCapturingMetrics trace0 = do
let trace = appendName "metrics" trace0
counters = [Obs.MemoryStats, Obs.ProcessStats
, Obs.NetStats, Obs.IOStats, Obs.GhcRtsStats, Obs.SysStats]
_ <- Async.async $ forever $ do
cts <- readCounters (ObservableTraceSelf counters)
traceCounters trace cts
threadDelay 30_000_000 -- 30 seconds
pure ()
where
traceCounters :: forall m a. MonadIO m => Trace m a -> [Counter] -> m ()
traceCounters _tr [] = return ()
traceCounters tr (c@(Counter _ct cn cv) : cs) = do
mle <- mkLOMeta Notice Confidential
traceNamedObject tr (mle, LogValue (nameCounter c <> "." <> cn) cv)
traceCounters tr cs

-- | See 'withLoggingNamed'
withLogging
:: [LogOutput]
Expand Down
1 change: 1 addition & 0 deletions nix/.stack.nix/cardano-wallet-cli.nix

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fe856ee

Please sign in to comment.