Skip to content

Commit 9887929

Browse files
committed
cardano-node: threads with labels
1 parent e3e74b0 commit 9887929

File tree

3 files changed

+46
-27
lines changed

3 files changed

+46
-27
lines changed

cardano-node/src/Cardano/Node/Configuration/Logging.hs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ createLoggingLayer ver nodeConfig' p = do
255255

256256
when (ncLogMetrics nodeConfig) $
257257
-- Record node metrics, if configured
258-
startCapturingMetrics (ncTraceConfig nodeConfig) trace
258+
startCapturingResources (ncTraceConfig nodeConfig) trace
259259

260260
mkLogLayer :: Configuration -> Switchboard Text -> Maybe EKGDirect -> Trace IO Text -> LoggingLayer
261261
mkLogLayer logConfig switchBoard mbEkgDirect trace =
@@ -278,14 +278,16 @@ createLoggingLayer ver nodeConfig' p = do
278278
, llEKGDirect = mbEkgDirect
279279
}
280280

281-
startCapturingMetrics :: TraceOptions
281+
startCapturingResources :: TraceOptions
282282
-> Trace IO Text
283283
-> IO ()
284-
startCapturingMetrics (TraceDispatcher _) _tr = do
284+
startCapturingResources (TraceDispatcher _) _tr = do
285285
pure ()
286286

287-
startCapturingMetrics _ tr = do
288-
void . Async.async . forever $ do
287+
startCapturingResources _ tr = do
288+
void . Async.async $ do
289+
myThreadId >>= flip labelThread "Metrics capturing (cardano-node)"
290+
forever $ do
289291
readResourceStats
290292
>>= maybe (pure ())
291293
(traceResourceStats

cardano-node/src/Cardano/Node/Tracing/Tracers/Peer.hs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import Cardano.Node.Orphans ()
1515
import Cardano.Node.Queries
1616
import Ouroboros.Consensus.Block (Header)
1717
import Ouroboros.Consensus.MiniProtocol.ChainSync.Client (ChainSyncClientHandle,
18-
csCandidate, cschcMap, viewChainSyncState, )
18+
csCandidate, cschcMap, viewChainSyncState)
1919
import Ouroboros.Consensus.Util.Orphans ()
2020
import qualified Ouroboros.Network.AnchoredFragment as Net
2121
import Ouroboros.Network.Block (unSlotNo)
@@ -39,25 +39,35 @@ import qualified Data.Map.Strict as Map
3939
import qualified Data.Set as Set
4040
import Data.Text (Text)
4141
import qualified Data.Text as Text
42+
import GHC.Conc (labelThread, myThreadId)
4243
import Text.Printf (printf)
4344

4445
{- HLINT ignore "Use =<<" -}
4546
{- HLINT ignore "Use <=<" -}
4647

48+
-- | Starts a background thread to periodically trace the current peer list.
49+
-- The thread is linked to the parent thread for proper error propagation
50+
-- and labeled for easier debugging and identification.
4751
startPeerTracer
48-
:: Tracer IO [PeerT blk]
49-
-> NodeKernelData blk
50-
-> Int
52+
:: Tracer IO [PeerT blk] -- ^ Tracer for the peer list
53+
-> NodeKernelData blk -- ^ Node kernel containing peer data
54+
-> Int -- ^ Delay in milliseconds between traces
5155
-> IO ()
52-
startPeerTracer tr nodeKern delayMilliseconds = do
53-
as <- async peersThread
54-
link as
56+
startPeerTracer tracer nodeKernel delayMilliseconds = do
57+
thread <- async peersThread
58+
-- Link the thread to the parent to propagate exceptions properly.
59+
link thread
5560
where
61+
-- | The background thread that periodically traces the peer list.
5662
peersThread :: IO ()
57-
peersThread = forever $ do
58-
peers <- getCurrentPeers nodeKern
59-
traceWith tr peers
60-
threadDelay (delayMilliseconds * 1000)
63+
peersThread = do
64+
-- Label the thread for easier debugging and identification.
65+
myThreadId >>= flip labelThread "Peer Tracer"
66+
forever $ do
67+
peers <- getCurrentPeers nodeKernel
68+
traceWith tracer peers
69+
threadDelay (delayMilliseconds * 1000)
70+
6171

6272
data PeerT blk = PeerT
6373
RemoteConnectionId

cardano-node/src/Cardano/Node/Tracing/Tracers/Resources.hs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,24 @@ import Control.Concurrent.Async (async)
1111
import Control.Monad (forM_, forever)
1212
import Control.Monad.Class.MonadAsync (link)
1313
import "contra-tracer" Control.Tracer
14+
import GHC.Conc (labelThread, myThreadId)
1415

15-
startResourceTracer
16-
:: Tracer IO ResourceStats
17-
-> Int
18-
-> IO ()
19-
startResourceTracer tr delayMilliseconds = do
20-
as <- async resourceThread
21-
link as
16+
-- | Starts a background thread to periodically trace resource statistics.
17+
-- The thread reads resource stats and traces them using the given tracer.
18+
-- It is linked to the parent thread to ensure proper error propagation.
19+
startResourceTracer :: Tracer IO ResourceStats -> Int -> IO ()
20+
startResourceTracer tracer delayMilliseconds = do
21+
thread <- async resourceThread
22+
-- Link the thread to the parent to propagate exceptions properly.
23+
link thread
2224
where
25+
-- | The background thread that periodically traces resource stats.
2326
resourceThread :: IO ()
24-
resourceThread = forever $ do
25-
mbrs <- readResourceStats
26-
forM_ mbrs $ \rs -> traceWith tr rs
27-
threadDelay (delayMilliseconds * 1000)
27+
resourceThread = do
28+
-- Label the thread for easier debugging and identification.
29+
myThreadId >>= flip labelThread "Resource Stats Tracer"
30+
forever $ do
31+
maybeStats <- readResourceStats
32+
-- If stats are available, trace them using the provided tracer.
33+
forM_ maybeStats $ traceWith tracer
34+
threadDelay (delayMilliseconds * 1000)

0 commit comments

Comments
 (0)