Skip to content

Commit d337e15

Browse files
authored
Merge pull request #44 from IntersectMBO/coot/fmaste-cardano-logging
trace-dispatcher integration
2 parents 102d1ad + 19c4d9f commit d337e15

28 files changed

Lines changed: 1506 additions & 861 deletions

cabal.project

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ index-state:
1818
, hackage.haskell.org 2026-02-17T10:15:41Z
1919

2020
-- Bump this if you need newer packages from CHaP
21-
, cardano-haskell-packages 2026-03-17T12:33:13Z
21+
, cardano-haskell-packages 2026-03-25T05:20:03Z
2222

2323
packages:
2424
./dmq-node

dmq-node/app/Main.hs

Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
{-# LANGUAGE DataKinds #-}
22
{-# LANGUAGE DisambiguateRecordFields #-}
33
{-# LANGUAGE MultiWayIf #-}
4+
{-# LANGUAGE NamedFieldPuns #-}
45
{-# LANGUAGE OverloadedRecordDot #-}
56
{-# LANGUAGE OverloadedStrings #-}
7+
{-# LANGUAGE PackageImports #-}
68
{-# LANGUAGE ScopedTypeVariables #-}
79
{-# LANGUAGE TemplateHaskell #-}
810
{-# LANGUAGE TypeApplications #-}
911
{-# LANGUAGE TypeOperators #-}
1012

1113
module Main where
1214

13-
import Control.Concurrent.Class.MonadMVar
1415
import Control.Concurrent.Class.MonadSTM.Strict
1516
import Control.Monad (unless, void, when)
17+
import Control.Monad.Class.MonadAsync
1618
import Control.Monad.Class.MonadThrow
17-
import Control.Tracer (Tracer (..), nullTracer, traceWith)
19+
import "contra-tracer" Control.Tracer (nullTracer, traceWith)
1820

1921
import Data.Act
20-
import Data.Aeson (ToJSON)
2122
import Data.ByteString.Lazy qualified as BSL
2223
import Data.Foldable (traverse_)
2324
import Data.Functor.Contravariant ((>$<))
@@ -31,28 +32,29 @@ import Options.Applicative
3132
import System.Directory qualified as Dir
3233
import System.Exit (die, exitSuccess)
3334
import System.IOManager (withIOManager)
35+
import System.Metrics qualified as EKG
3436
import System.Random qualified as Random
3537

3638
import Cardano.Git.Rev (gitRev)
3739
import Cardano.KESAgent.Protocols.StandardCrypto (StandardCrypto)
40+
import Cardano.Logging.Prometheus.TCPServer qualified as Prometheus
3841

3942
import DMQ.Configuration
4043
import DMQ.Configuration.CLIOptions (parseCLIOptions)
4144
import DMQ.Configuration.Topology (readTopologyFileOrError)
4245
import DMQ.Diffusion.Applications (diffusionApplications)
4346
import DMQ.Diffusion.Arguments
4447
import DMQ.Diffusion.NodeKernel
48+
import DMQ.Diffusion.PeerSelection (policy)
4549
import DMQ.Handlers.TopLevel (toplevelExceptionHandler)
4650
import DMQ.NodeToClient qualified as NtC
51+
import DMQ.NodeToClient.LocalStateQueryClient
4752
import DMQ.NodeToNode (NodeToNodeVersion, dmqCodecs, dmqLimitsAndTimeouts,
4853
ntnApps)
4954
import DMQ.Policy qualified as Policy
5055
import DMQ.Protocol.SigSubmission.Type (Sig (..))
51-
import DMQ.Tracer
52-
53-
import DMQ.Diffusion.PeerSelection (policy)
54-
import DMQ.NodeToClient.LocalStateQueryClient
5556
import DMQ.Protocol.SigSubmission.Validate
57+
import DMQ.Tracer (DMQStartupTrace (..), DMQTracers (..), mkDMQTracers)
5658
import Ouroboros.Network.Diffusion qualified as Diffusion
5759
import Ouroboros.Network.PeerSelection.LedgerPeers.Type
5860
import Ouroboros.Network.PeerSelection.PeerSharing.Codec (decodeRemoteAddress,
@@ -78,30 +80,43 @@ runDMQ commandLineConfig = do
7880
$ dmqcConfigFile commandLineConfig
7981
`act` dmqcConfigFile defaultConfiguration
8082

83+
ekgStore <- EKG.newStore
84+
EKG.registerGcMetrics ekgStore
85+
8186
-- read & parse configuration file
8287
config' <- readConfigurationFileOrError configFilePath
8388
-- combine default configuration, configuration file and command line
8489
-- options
85-
let dmqConfig@Configuration {
86-
dmqcPrettyLog = I prettyLog,
90+
let dmqConfig :: Configuration
91+
dmqConfig@Configuration {
8792
dmqcTopologyFile = I topologyFile,
88-
dmqcHandshakeTracer = I handshakeTracer,
89-
dmqcValidationTracer = I validationTracer,
90-
dmqcLocalHandshakeTracer = I localHandshakeTracer,
9193
dmqcCardanoNodeSocket = I socketPath,
9294
dmqcVersion = I version,
93-
dmqcLocalStateQueryTracer = I localStateQueryTracer,
9495
dmqcLedgerPeers = I ledgerPeers
9596
} = config' <> commandLineConfig
9697
`act`
9798
defaultConfiguration
9899

99-
lock <- newMVar ()
100-
let tracer', tracer :: ToJSON ev => Tracer IO (WithEventType ev)
101-
tracer' = dmqTracer prettyLog
102-
-- use a lock to prevent writing two lines at the same time
103-
-- TODO: this won't be needed with `cardano-tracer` integration
104-
tracer = Tracer $ \a -> withMVar lock $ \_ -> traceWith tracer' a
100+
( dmqTracers@DMQTracers {
101+
dmqStartupTracer,
102+
localStateQueryClientTracer,
103+
sigValidationTracer,
104+
localSigValidationTracer,
105+
cardanoNodeHandshakeTracer
106+
}
107+
, dmqDiffusionTracers
108+
, prometheusConfig
109+
)
110+
<- mkDMQTracers ekgStore configFilePath
111+
112+
case prometheusConfig of
113+
Nothing -> return ()
114+
Just ps ->
115+
-- morally it belongs to `NodeKernel`, but it runs in `IO`, not `m`.
116+
Prometheus.runPrometheusSimple
117+
(DMQPrometheus >$< dmqStartupTracer)
118+
ekgStore ps
119+
>>= link
105120

106121
when version $ do
107122
let gitrev = $(gitRev)
@@ -120,12 +135,11 @@ runDMQ commandLineConfig = do
120135
]
121136
exitSuccess
122137

123-
traceWith tracer (WithEventType "Configuration" dmqConfig)
138+
traceWith dmqStartupTracer (DMQConfiguration dmqConfig)
124139
Dir.doesFileExist socketPath >>= \a ->
125140
unless a (die $ "CardanoNodeSocket " ++ show socketPath ++": file does not exist")
126141
nt <- readTopologyFileOrError topologyFile
127-
traceWith tracer (WithEventType "NetworkTopology" nt)
128-
142+
traceWith dmqStartupTracer (DMQTopology nt)
129143

130144
stdGen <- Random.newStdGen
131145
let (psRng, policyRng) = Random.splitGen stdGen
@@ -135,15 +149,13 @@ runDMQ commandLineConfig = do
135149
withIOManager \iocp -> do
136150
let localSnocket' = localSnocket iocp
137151
mkStakePoolMonitor = connectToCardanoNode
138-
(if localStateQueryTracer
139-
then WithEventType "LocalStateQuery" >$< tracer
140-
else nullTracer)
152+
localStateQueryClientTracer
141153
ledgerPeers
142154
localSnocket'
143155
socketPath
144156

145157
withNodeKernel @StandardCrypto
146-
tracer
158+
dmqTracers
147159
dmqConfig
148160
psRng
149161
mkStakePoolMonitor $ \nodeKernel -> do
@@ -153,9 +165,6 @@ runDMQ commandLineConfig = do
153165
let sigSize :: Sig StandardCrypto -> SizeInBytes
154166
sigSize = fromIntegral . BSL.length . sigRawBytes
155167
mempoolReader = Mempool.getReader sigId sigSize (mempool nodeKernel)
156-
ntnValidationTracer = if validationTracer
157-
then WithEventType "NtN Validation" >$< tracer
158-
else nullTracer
159168
dmqNtNApps =
160169
let ntnMempoolWriter =
161170
Mempool.getWriter SigDuplicate
@@ -164,7 +173,7 @@ runDMQ commandLineConfig = do
164173
withPoolValidationCtx (stakePools nodeKernel) (validateSig now sigs)
165174
)
166175
(traverse_ $ \(sigid, reason) -> do
167-
traceWith ntnValidationTracer $ InvalidSignature sigid reason
176+
traceWith sigValidationTracer $ InvalidSignature sigid reason
168177
case reason of
169178
SigDuplicate -> return ()
170179
SigExpired -> return ()
@@ -174,7 +183,7 @@ runDMQ commandLineConfig = do
174183
err -> throwIO (SigValidationException sigid err)
175184
)
176185
(mempool nodeKernel)
177-
in ntnApps tracer
186+
in ntnApps dmqTracers
178187
dmqConfig
179188
mempoolReader
180189
ntnMempoolWriter
@@ -185,9 +194,6 @@ runDMQ commandLineConfig = do
185194
(decodeRemoteAddress (maxBound @NodeToNodeVersion)))
186195
dmqLimitsAndTimeouts
187196
Policy.sigDecisionPolicy
188-
ntcValidationTracer = if validationTracer
189-
then WithEventType "NtC Validation" >$< tracer
190-
else nullTracer
191197
dmqNtCApps =
192198
let ntcMempoolWriter =
193199
Mempool.getWriter SigDuplicate
@@ -196,19 +202,15 @@ runDMQ commandLineConfig = do
196202
withPoolValidationCtx (stakePools nodeKernel) (validateSig now sigs)
197203
)
198204
(traverse_ $ \(sigid, reason) ->
199-
traceWith ntcValidationTracer $ InvalidSignature sigid reason
205+
traceWith localSigValidationTracer $ InvalidSignature sigid reason
200206
)
201207
(mempool nodeKernel)
202-
in NtC.ntcApps tracer dmqConfig
208+
in NtC.ntcApps dmqTracers dmqConfig
203209
mempoolReader ntcMempoolWriter
204210
NtC.dmqCodecs
205211
dmqDiffusionArguments =
206-
diffusionArguments (if handshakeTracer
207-
then WithEventType "Handshake" >$< tracer
208-
else nullTracer)
209-
(if localHandshakeTracer
210-
then WithEventType "Handshake" >$< tracer
211-
else nullTracer)
212+
diffusionArguments nullTracer
213+
cardanoNodeHandshakeTracer
212214
$ maybe [] out <$> tryReadTMVar nodeKernel.stakePools.ledgerPeersVar
213215
where
214216
out :: LedgerPeerSnapshot AllLedgerPeers
@@ -225,6 +227,6 @@ runDMQ commandLineConfig = do
225227
(policy policyRngVar)
226228

227229
Diffusion.run dmqDiffusionArguments
228-
(dmqDiffusionTracers dmqConfig tracer)
230+
dmqDiffusionTracers
229231
dmqDiffusionConfiguration
230232
dmqDiffusionApplications
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!--
2+
A new scriv changelog fragment.
3+
4+
Uncomment the section that is right (remove the HTML comment wrapper).
5+
For top level release notes, leave all the headers commented out.
6+
-->
7+
8+
### Breaking
9+
10+
- Integration with `trace-dispatcher`. Removed tracing configuration options
11+
from `Configuration`, `trace-dispatcher` configuration is used instead.
12+
- Added EKG counters and a prometheus server.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
### Non-Breaking
2+
3+
- Replaced `NoExtraPeers`, `NoExtraState`, `NoExtraDebugState`, `NoExtraFlags` with types from `ouroboros-network`.
4+
- Removed unused types `NoExtraTracer`, `NoExtraCounters`.

dmq-node/changelog.d/scriv.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ format = md
33
insert_marker = Changelog entries
44
md_header_level = 2
55
version = literal: dmq-node.cabal: version
6-
categories = Breaking, Non-Breaking
6+
categories = Breaking, Non-Breaking, Patch
77
start_marker = scriv-insert-here
88
end_marker = scriv-end-here
99
fragment_directory = changelog.d

dmq-node/config.json

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,46 @@
11
{ "NetworkMagic": 12
2+
, "TraceOptions": {
3+
"": {
4+
"backends": [
5+
"Stdout MachineFormat"
6+
],
7+
"severity": "Info"
8+
}
9+
, "Handshake": {
10+
"severity": "Debug"
11+
}
12+
, "LocalMux": {
13+
"severity": "Debug"
14+
}
15+
, "LocalHandshake": {
16+
"severity": "Debug"
17+
}
18+
, "Diffusion": {
19+
"severity": "Debug"
20+
}
21+
, "PeerSelection": {
22+
"severity": "Debug"
23+
}
24+
, "PeerSelectionCounters": {
25+
"severity": "Debug"
26+
}
27+
, "ConnectionManager": {
28+
"severity": "Debug"
29+
}
30+
, "Server": {
31+
"severity": "Debug"
32+
}
33+
, "InboundGovernor": {
34+
"severity": "Debug"
35+
}
36+
, "LocalMsgSubmission.Protocol.Server": {
37+
"severity": "Debug"
38+
}
39+
, "LocalMsgNotification.Protocol.Server": {
40+
"severity": "Debug"
41+
}
42+
, "SigSubmission.Inbound": {
43+
"severity": "Debug"
44+
}
45+
}
246
}

dmq-node/dmq-node.cabal

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,14 @@ library
6363
DMQ.Diffusion.Applications
6464
DMQ.Diffusion.Arguments
6565
DMQ.Diffusion.NodeKernel
66+
DMQ.Diffusion.NodeKernel.Types
6667
DMQ.Diffusion.PeerSelection
6768
DMQ.Handlers.TopLevel
6869
DMQ.NodeToClient
6970
DMQ.NodeToClient.LocalMsgNotification
7071
DMQ.NodeToClient.LocalMsgSubmission
7172
DMQ.NodeToClient.LocalStateQueryClient
73+
DMQ.NodeToClient.LocalStateQueryClient.Types
7274
DMQ.NodeToClient.Version
7375
DMQ.NodeToNode
7476
DMQ.NodeToNode.Version
@@ -97,7 +99,6 @@ library
9799
acts,
98100
acts-generic,
99101
aeson >=2.1.1.0 && <3,
100-
aeson-pretty,
101102
base >=4.14 && <4.23,
102103
base16-bytestring,
103104
bytestring >=0.10 && <0.13,
@@ -116,6 +117,7 @@ library
116117
contra-tracer >=0.1 && <0.3,
117118
deepseq >=1.0 && <1.6,
118119
dns >=1.0 && <4.3,
120+
ekg-core,
119121
generic-data,
120122
hashable >=1.0 && <1.6,
121123
io-classes:{io-classes, si-timers, strict-mvar, strict-stm} ^>=1.8.0.1,
@@ -127,12 +129,13 @@ library
127129
nothunks,
128130
optparse-applicative >=0.18 && <0.20,
129131
ouroboros-consensus:{ouroboros-consensus, cardano, diffusion},
130-
ouroboros-network:{ouroboros-network, api, framework, orphan-instances, protocols} ^>=1.1.0.0,
132+
ouroboros-network:{ouroboros-network, api, framework, framework-tracing, orphan-instances, protocols, tracing} ^>=1.1.0.0,
131133
quiet,
132134
random ^>=1.3,
133135
singletons,
134136
text >=1.2.4 && <2.2,
135137
time >=1.12 && <1.15,
138+
trace-dispatcher ^>=2.12.0,
136139
transformers,
137140
typed-protocols:{typed-protocols, cborg} ^>=1.2,
138141

@@ -154,19 +157,20 @@ executable dmq-node
154157
build-depends:
155158
Win32-network,
156159
acts,
157-
aeson,
158160
base,
159161
bytestring,
160162
cardano-git-rev,
161163
contra-tracer >=0.1 && <0.3,
162164
directory,
163165
dmq-node,
166+
ekg-core,
164167
io-classes:{io-classes, strict-stm},
165168
kes-agent-crypto,
166169
optparse-applicative,
167170
ouroboros-network:{ouroboros-network, api, framework},
168171
random,
169172
text,
173+
trace-dispatcher,
170174

171175
hs-source-dirs: app
172176
default-language: Haskell2010

0 commit comments

Comments
 (0)