Skip to content

Commit 77efb66

Browse files
authored
Merge pull request #6093 from IntersectMBO/jutaro/thread-labels
Label Yet Unlabeled Tracing Threads
2 parents c4d675b + 2ed706d commit 77efb66

File tree

3 files changed

+76
-58
lines changed

3 files changed

+76
-58
lines changed

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

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -28,28 +28,6 @@ module Cardano.Node.Configuration.Logging
2828
import Cardano.Api (textShow)
2929
import qualified Cardano.Api as Api
3030

31-
import qualified Control.Concurrent as Conc
32-
import qualified Control.Concurrent.Async as Async
33-
import Control.Concurrent.MVar (MVar, newMVar)
34-
import Control.Concurrent.STM (STM)
35-
import Control.Exception (IOException)
36-
import Control.Exception.Safe (MonadCatch)
37-
import Control.Monad (forM_, forever, void, when)
38-
import Control.Monad.Except (ExceptT)
39-
import Control.Monad.IO.Class (MonadIO (..))
40-
import Control.Monad.Trans.Except.Extra (catchIOExceptT)
41-
import "contra-tracer" Control.Tracer
42-
import Data.List (nub)
43-
import qualified Data.Map.Strict as Map
44-
import Data.Maybe (isJust)
45-
import Data.Text (Text, pack)
46-
import Data.Time.Clock (UTCTime, getCurrentTime)
47-
import Data.Version (showVersion)
48-
import System.Metrics.Counter (Counter)
49-
import System.Metrics.Gauge (Gauge)
50-
import System.Metrics.Label (Label)
51-
import qualified System.Remote.Monitoring.Wai as EKG
52-
5331
import Cardano.BM.Backend.Aggregation (plugin)
5432
import Cardano.BM.Backend.EKGView (plugin)
5533
import Cardano.BM.Backend.Monitoring (plugin)
@@ -65,17 +43,23 @@ import Cardano.BM.Data.LogItem (LOContent (..), LOMeta (..), LoggerNam
6543
import qualified Cardano.BM.Observer.Monadic as Monadic
6644
import qualified Cardano.BM.Observer.STM as Stm
6745
import Cardano.BM.Plugin (loadPlugin)
68-
#if defined(SYSTEMD)
46+
#ifdef SYSTEMD
6947
import Cardano.BM.Scribe.Systemd (plugin)
7048
#endif
7149
import Cardano.BM.Setup (setupTrace_, shutdown)
7250
import Cardano.BM.Stats
7351
import Cardano.BM.Stats.Resources
7452
import qualified Cardano.BM.Trace as Trace
7553
import Cardano.BM.Tracing
76-
7754
import qualified Cardano.Chain.Genesis as Gen
55+
import Cardano.Git.Rev (gitRev)
7856
import qualified Cardano.Ledger.Shelley.API as SL
57+
import Cardano.Node.Configuration.POM (NodeConfiguration (..), ncProtocol)
58+
import Cardano.Node.Protocol.Types (SomeConsensusProtocol (..))
59+
import Cardano.Node.Types
60+
import Cardano.Slotting.Slot (EpochSize (..))
61+
import Cardano.Tracing.Config (TraceOptions (..))
62+
import Cardano.Tracing.OrphanInstances.Common ()
7963
import qualified Ouroboros.Consensus.BlockchainTime.WallClock.Types as WCT
8064
import Ouroboros.Consensus.Byron.Ledger.Conversions
8165
import Ouroboros.Consensus.Cardano.Block
@@ -86,13 +70,28 @@ import Ouroboros.Consensus.HardFork.Combinator.Degenerate
8670
import Ouroboros.Consensus.Node.ProtocolInfo
8771
import Ouroboros.Consensus.Shelley.Ledger.Ledger
8872

89-
import Cardano.Git.Rev (gitRev)
90-
import Cardano.Node.Configuration.POM (NodeConfiguration (..), ncProtocol)
91-
import Cardano.Node.Protocol.Types (SomeConsensusProtocol (..))
92-
import Cardano.Node.Types
93-
import Cardano.Slotting.Slot (EpochSize (..))
94-
import Cardano.Tracing.Config (TraceOptions (..))
95-
import Cardano.Tracing.OrphanInstances.Common ()
73+
import qualified Control.Concurrent as Conc
74+
import qualified Control.Concurrent.Async as Async
75+
import Control.Concurrent.MVar (MVar, newMVar)
76+
import Control.Concurrent.STM (STM)
77+
import Control.Exception (IOException)
78+
import Control.Exception.Safe (MonadCatch)
79+
import Control.Monad (forM_, forever, void, when)
80+
import Control.Monad.Except (ExceptT)
81+
import Control.Monad.IO.Class (MonadIO (..))
82+
import Control.Monad.Trans.Except.Extra (catchIOExceptT)
83+
import Data.List (nub)
84+
import qualified Data.Map.Strict as Map
85+
import Data.Maybe (isJust)
86+
import Data.Text (Text, pack)
87+
import Data.Time.Clock (UTCTime, getCurrentTime)
88+
import Data.Version (showVersion)
89+
import GHC.Conc (labelThread, myThreadId)
90+
import System.Metrics.Counter (Counter)
91+
import System.Metrics.Gauge (Gauge)
92+
import System.Metrics.Label (Label)
93+
import qualified System.Remote.Monitoring.Wai as EKG
94+
9695
import Paths_cardano_node (version)
9796

9897
--------------------------------
@@ -255,7 +254,7 @@ createLoggingLayer ver nodeConfig' p = do
255254

256255
when (ncLogMetrics nodeConfig) $
257256
-- Record node metrics, if configured
258-
startCapturingMetrics (ncTraceConfig nodeConfig) trace
257+
startCapturingResources (ncTraceConfig nodeConfig) trace
259258

260259
mkLogLayer :: Configuration -> Switchboard Text -> Maybe EKGDirect -> Trace IO Text -> LoggingLayer
261260
mkLogLayer logConfig switchBoard mbEkgDirect trace =
@@ -278,14 +277,16 @@ createLoggingLayer ver nodeConfig' p = do
278277
, llEKGDirect = mbEkgDirect
279278
}
280279

281-
startCapturingMetrics :: TraceOptions
280+
startCapturingResources :: TraceOptions
282281
-> Trace IO Text
283282
-> IO ()
284-
startCapturingMetrics (TraceDispatcher _) _tr = do
283+
startCapturingResources (TraceDispatcher _) _tr = do
285284
pure ()
286285

287-
startCapturingMetrics _ tr = do
288-
void . Async.async . forever $ do
286+
startCapturingResources _ tr = do
287+
void . Async.async $ do
288+
myThreadId >>= flip labelThread "Resource capturing (old tracing)"
289+
forever $ do
289290
readResourceStats
290291
>>= maybe (pure ())
291292
(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)