From fe856ee80fe3e2e0fd1bc7ca8050c488b77a053c Mon Sep 17 00:00:00 2001 From: Julian Ospald Date: Mon, 16 Nov 2020 20:09:42 +0100 Subject: [PATCH] Improve statistics and send messages to EKG as well --- lib/cli/cardano-wallet-cli.cabal | 1 + lib/cli/src/Cardano/CLI.hs | 52 ++++++++++++++++++++++----- nix/.stack.nix/cardano-wallet-cli.nix | 1 + 3 files changed, 46 insertions(+), 8 deletions(-) diff --git a/lib/cli/cardano-wallet-cli.cabal b/lib/cli/cardano-wallet-cli.cabal index 2d1fad7137f..0385ce09896 100644 --- a/lib/cli/cardano-wallet-cli.cabal +++ b/lib/cli/cardano-wallet-cli.cabal @@ -32,6 +32,7 @@ library aeson , aeson-pretty , ansi-terminal + , async , base , bytestring , cardano-addresses diff --git a/lib/cli/src/Cardano/CLI.hs b/lib/cli/src/Cardano/CLI.hs index 2c8ea3d5f82..f0d71ff8e9f 100644 --- a/lib/cli/src/Cardano/CLI.hs +++ b/lib/cli/src/Cardano/CLI.hs @@ -11,6 +11,7 @@ {-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE LambdaCase #-} {-# LANGUAGE MultiParamTypeClasses #-} +{-# LANGUAGE NumericUnderscores #-} {-# LANGUAGE RankNTypes #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE TypeApplications #-} @@ -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 (..) @@ -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 @@ -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 @@ -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 @@ -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] diff --git a/nix/.stack.nix/cardano-wallet-cli.nix b/nix/.stack.nix/cardano-wallet-cli.nix index 3f82b055087..b6a5784cd69 100644 --- a/nix/.stack.nix/cardano-wallet-cli.nix +++ b/nix/.stack.nix/cardano-wallet-cli.nix @@ -29,6 +29,7 @@ (hsPkgs."aeson" or (errorHandler.buildDepError "aeson")) (hsPkgs."aeson-pretty" or (errorHandler.buildDepError "aeson-pretty")) (hsPkgs."ansi-terminal" or (errorHandler.buildDepError "ansi-terminal")) + (hsPkgs."async" or (errorHandler.buildDepError "async")) (hsPkgs."base" or (errorHandler.buildDepError "base")) (hsPkgs."bytestring" or (errorHandler.buildDepError "bytestring")) (hsPkgs."cardano-addresses" or (errorHandler.buildDepError "cardano-addresses"))