Skip to content

Commit 9485a53

Browse files
committed
cardano-tracer: re-use runInLoop; simplify LogFormatting; proper error tracing
1 parent 10b900c commit 9485a53

File tree

8 files changed

+145
-220
lines changed

8 files changed

+145
-220
lines changed

cardano-tracer/src/Cardano/Tracer/Acceptors/Run.hs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ module Cardano.Tracer.Acceptors.Run
55
( runAcceptors
66
) where
77

8+
import Cardano.Logging.Types (TraceObject)
9+
import Cardano.Logging.Utils (runInLoop)
810
import Cardano.Tracer.Acceptors.Client
911
import Cardano.Tracer.Acceptors.Server
1012
import Cardano.Tracer.Configuration
1113
import Cardano.Tracer.Environment
1214
import Cardano.Tracer.MetaTrace
13-
import Cardano.Tracer.Utils
14-
import Cardano.Logging.Types (TraceObject)
15-
import qualified Cardano.Logging.Types as Net
1615

1716
import Control.Concurrent.Async (forConcurrently_)
17+
import Control.Exception (SomeException (..))
1818
import "contra-tracer" Control.Tracer (Tracer, contramap, nullTracer, stdoutTracer)
1919
import qualified Data.List.NonEmpty as NE
2020
import Data.Maybe (fromMaybe)
@@ -39,15 +39,19 @@ runAcceptors tracerEnv@TracerEnv{teTracer} tracerEnvRTView = do
3939
AcceptAt howToConnect ->
4040
-- Run one server that accepts connections from the nodes.
4141
runInLoop
42-
(runAcceptorsServer tracerEnv tracerEnvRTView howToConnect $ acceptorsConfigs (Net.howToConnectString howToConnect))
43-
verbosity howToConnect initialPauseInSec
42+
(runAcceptorsServer tracerEnv tracerEnvRTView howToConnect $ acceptorsConfigs (show howToConnect))
43+
(handleOnInterruption howToConnect) initialPauseInSec 10
4444
ConnectTo localSocks ->
4545
-- Run N clients that initiate connections to the nodes.
4646
forConcurrently_ (NE.nub localSocks) \howToConnect ->
4747
runInLoop
48-
(runAcceptorsClient tracerEnv tracerEnvRTView howToConnect $ acceptorsConfigs (Net.howToConnectString howToConnect))
49-
verbosity howToConnect initialPauseInSec
48+
(runAcceptorsClient tracerEnv tracerEnvRTView howToConnect $ acceptorsConfigs (show howToConnect))
49+
(handleOnInterruption howToConnect) initialPauseInSec 30
5050
where
51+
handleOnInterruption howToConnect (SomeException e)
52+
| verbosity == Just Minimum = pure ()
53+
| otherwise = traceWith teTracer $ TracerForwardingInterrupted howToConnect $ show e
54+
5155
TracerConfig{network, ekgRequestFreq, verbosity, ekgRequestFull} = teConfig tracerEnv
5256
ekgUseFullRequests = fromMaybe False ekgRequestFull
5357

cardano-tracer/src/Cardano/Tracer/Acceptors/Server.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ runAcceptorsServer
6464
-> IO ()
6565
runAcceptorsServer tracerEnv tracerEnvRTView howToConnect ( ekgConfig, tfConfig, dpfConfig) =
6666
withIOManager \iocp -> do
67-
traceWith (teTracer tracerEnv) $ TracerSockListen (Net.howToConnectString howToConnect)
67+
traceWith (teTracer tracerEnv) $ TracerSockListen (show howToConnect)
6868
case howToConnect of
6969
Net.LocalPipe p ->
7070
doListenToForwarderLocal

cardano-tracer/src/Cardano/Tracer/Configuration.hs

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
{-# LANGUAGE DeriveGeneric #-}
33
{-# LANGUAGE DuplicateRecordFields #-}
44
{-# LANGUAGE LambdaCase #-}
5-
{-# LANGUAGE MultiWayIf #-}
65
{-# LANGUAGE NamedFieldPuns #-}
76
{-# LANGUAGE OverloadedStrings #-}
87
{-# LANGUAGE RecordWildCards #-}
@@ -11,7 +10,7 @@
1110
{- HLINT ignore "Use any" -}
1211

1312
module Cardano.Tracer.Configuration
14-
( Address -- (..)
13+
( Address
1514
, Net.HowToConnect (..)
1615
, Endpoint (..)
1716
, setEndpoint
@@ -24,14 +23,14 @@ module Cardano.Tracer.Configuration
2423
, TracerConfig (..)
2524
, Verbosity (..)
2625
, readTracerConfig
27-
, parseHostPort
2826
) where
2927

28+
import Cardano.Logging.Types (HowToConnect)
3029
import qualified Cardano.Logging.Types as Log
30+
import qualified Cardano.Logging.Types as Net
3131

3232
import Control.Applicative ((<|>))
33-
import Data.Aeson (FromJSON (..), ToJSON (..), withText, withObject, (.:))
34-
import Data.Aeson.Types (Parser, Value)
33+
import Data.Aeson (FromJSON (..), ToJSON (..), withObject, (.:))
3534
import Data.Fixed (Pico)
3635
import Data.Function ((&))
3736
import Data.Functor ((<&>))
@@ -45,52 +44,15 @@ import Data.Maybe (catMaybes)
4544
import Data.String (fromString)
4645
import Data.Text (Text)
4746
import qualified Data.Text as Text
48-
import qualified Data.Text.Read as Text
4947
import Data.Word (Word16, Word32, Word64)
5048
import Data.Yaml (decodeFileEither)
5149
import GHC.Generics (Generic)
5250
import Network.Wai.Handler.Warp (HostPreference, Port, Settings, setHost, setPort)
5351
import System.Exit (die)
5452

55-
import Cardano.Logging.Types (HowToConnect)
56-
import qualified Cardano.Logging.Types as Net
57-
5853
type Address :: Type
5954
type Address = HowToConnect
6055

61-
-- first try to host:port, and if that fails revert to parsing any
62-
-- string literal and assume it is a localpipe.
63-
instance FromJSON HowToConnect where
64-
parseJSON :: Value -> Parser HowToConnect
65-
parseJSON = withText "HowToConnect" $ \t ->
66-
(uncurry Net.RemoteSocket <$> parseHostPort t)
67-
<|> ( Net.LocalPipe <$> parseLocalPipe t)
68-
69-
instance ToJSON HowToConnect where
70-
toJSON :: HowToConnect -> Value
71-
toJSON = toJSON . Net.howToConnectString
72-
73-
parseLocalPipe :: Text -> Parser FilePath
74-
parseLocalPipe t
75-
| Text.null t = fail "parseLocalPipe: empty Text"
76-
| otherwise = pure $ Text.unpack t
77-
78-
parseHostPort :: Text -> Parser (Text, Word16)
79-
parseHostPort t
80-
| Text.null t
81-
= fail "parseHostPort: empty Text"
82-
| otherwise
83-
= let
84-
(host_, portText) = Text.breakOnEnd ":" t
85-
host = maybe "" fst (Text.unsnoc host_)
86-
in if
87-
| Text.null host -> fail "parseHostPort: Empty host or no colon found."
88-
| Text.null portText -> fail "parseHostPort: Empty port."
89-
| Right (port, remainder) <- Text.decimal portText
90-
, Text.null remainder
91-
, 0 <= port, port <= 65535 -> pure (host, port)
92-
| otherwise -> fail "parseHostPort: Non-numeric port or value out of range."
93-
9456
-- | Endpoint for internal services.
9557
data Endpoint = Endpoint
9658
{ epHost :: !String

cardano-tracer/src/Cardano/Tracer/Handlers/Logs/Rotator.hs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ runLogsRotator TracerEnv
4141
} = do
4242
whenJust rotation \rotParams -> do
4343
traceWith teTracer TracerStartedLogRotator
44-
launchRotator loggingParamsForFiles rotParams verbosity teRegistry teCurrentLogLock
44+
launchRotator loggingParamsForFiles rotParams verbosity teTracer teRegistry teCurrentLogLock
4545
where
4646
loggingParamsForFiles :: [LoggingParams]
4747
loggingParamsForFiles = nub (NE.filter filesOnly logging)
@@ -53,14 +53,15 @@ launchRotator
5353
:: [LoggingParams]
5454
-> RotationParams
5555
-> Maybe Verbosity
56+
-> Trace IO TracerTrace
5657
-> HandleRegistry
5758
-> Lock
5859
-> IO ()
59-
launchRotator [] _ _ _ _ = return ()
60+
launchRotator [] _ _ _ _ _ = return ()
6061
launchRotator loggingParamsForFiles
61-
rotParams@RotationParams{rpFrequencySecs} verb registry currentLogLock =
62+
rotParams@RotationParams{rpFrequencySecs} verb tracer registry currentLogLock =
6263
forever do
63-
showProblemIfAny verb do
64+
showProblemIfAny verb tracer do
6465
forM_ loggingParamsForFiles \loggingParam -> do
6566
checkRootDir currentLogLock registry rotParams loggingParam
6667
sleep $ fromIntegral rpFrequencySecs

cardano-tracer/src/Cardano/Tracer/Handlers/Logs/TraceObjects.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ traceObjectsHandler _ _ _ [] = return ()
3838
traceObjectsHandler tracerEnv _tracerEnvRTView nodeId traceObjects = do
3939
nodeName <- askNodeName tracerEnv nodeId
4040
forConcurrently_ logging \loggingParams@LoggingParams{logMode, logFormat} -> do
41-
showProblemIfAny verbosity do
41+
showProblemIfAny verbosity teTracer do
4242
case logMode of
4343
FileMode ->
4444
writeTraceObjectsToFile teRegistry
@@ -56,6 +56,7 @@ traceObjectsHandler tracerEnv _tracerEnvRTView nodeId traceObjects = do
5656
, teCurrentLogLock
5757
, teReforwardTraceObjects
5858
, teRegistry
59+
, teTracer
5960
} = tracerEnv
6061

6162
deregisterNodeId :: TracerEnv -> NodeId -> IO ()

0 commit comments

Comments
 (0)