Skip to content

Commit

Permalink
Add TraceMempoolSynced support
Browse files Browse the repository at this point in the history
Add support for tracing TraceMempoolSynced along with a txsSyncDuration
which tracks how long it took to sync the mempool after block adoption.
Unit is ms.
  • Loading branch information
karknu authored and crocodile-dentist committed Oct 15, 2024
1 parent 14a4c8b commit 82fac7d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 12 deletions.
20 changes: 20 additions & 0 deletions cardano-node/src/Cardano/Node/Tracing/Tracers/Consensus.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1205,6 +1205,12 @@ instance
, "mempoolSize" .= forMachine dtal mpSz
]

forMachine _dtal (TraceMempoolSynced et) =
mconcat
[ "kind" .= String "TraceMempoolSynced"
, "enclosingTime" .= et
]

asMetrics (TraceMempoolAddedTx _tx _mpSzBefore mpSz) =
[ IntM "txsInMempool" (fromIntegral $ msNumTxs mpSz)
, IntM "mempoolBytes" (fromIntegral . unByteSize32 . msNumBytes $ mpSz)
Expand All @@ -1227,6 +1233,13 @@ instance
, CounterM "txsProcessedNum" (Just (fromIntegral $ length txs))
]

asMetrics (TraceMempoolSynced (FallingEdgeWith duration)) =
[ IntM "txsSyncDuration" (round $ 1000 * duration)
]

asMetrics (TraceMempoolSynced RisingEdge) = []


instance LogFormatting MempoolSize where
forMachine _dtal MempoolSize{msNumTxs, msNumBytes} =
mconcat
Expand All @@ -1240,11 +1253,13 @@ instance MetaTrace (TraceEventMempool blk) where
namespaceFor TraceMempoolRejectedTx {} = Namespace [] ["RejectedTx"]
namespaceFor TraceMempoolRemoveTxs {} = Namespace [] ["RemoveTxs"]
namespaceFor TraceMempoolManuallyRemovedTxs {} = Namespace [] ["ManuallyRemovedTxs"]
namespaceFor TraceMempoolSynced {} = Namespace [] ["Synced"]

severityFor (Namespace _ ["AddedTx"]) _ = Just Info
severityFor (Namespace _ ["RejectedTx"]) _ = Just Info
severityFor (Namespace _ ["RemoveTxs"]) _ = Just Info
severityFor (Namespace _ ["ManuallyRemovedTxs"]) _ = Just Info
severityFor (Namespace _ ["Synced"]) _ = Just Info
severityFor _ _ = Nothing

metricsDocFor (Namespace _ ["AddedTx"]) =
Expand All @@ -1264,6 +1279,11 @@ instance MetaTrace (TraceEventMempool blk) where
, ("mempoolBytes", "Byte size of the mempool")
, ("txsProcessedNum", "")
]

metricsDocFor (Namespace _ ["Synced"]) =
[ ("txsSyncDuration", "Time to sync the mempool in ms after block adoption")
]

metricsDocFor _ = []

documentFor (Namespace _ ["AddedTx"]) = Just
Expand Down
5 changes: 5 additions & 0 deletions cardano-node/src/Cardano/Tracing/OrphanInstances/Consensus.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1510,6 +1510,11 @@ instance ( ToObject (ApplyTxErr blk), ToObject (GenTx blk),
, "txsInvalidated" .= map (toObject verb . txForgetValidated) txs1
, "mempoolSize" .= toObject verb mpSz
]
toObject _verb (TraceMempoolSynced et) =
mconcat
[ "kind" .= String "TraceMempoolSynced"
, "enclosingTime" .= et
]

instance ToObject MempoolSize where
toObject _verb MempoolSize{msNumTxs, msNumBytes} =
Expand Down
31 changes: 19 additions & 12 deletions cardano-node/src/Cardano/Tracing/Tracers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1249,25 +1249,32 @@ notifyTxsProcessed fStats tr = Tracer $ \case
-- so we can treat them as completely processed.
updatedTxProcessed <- mapForgingStatsTxsProcessed fStats (+ (length txs))
traceCounter "txsProcessedNum" tr (fromIntegral updatedTxProcessed)
TraceMempoolSynced (FallingEdgeWith duration) -> do
traceCounter "txsSyncDuration" tr (round $ 1000 * duration :: Int)

-- The rest of the constructors.
_ -> return ()


mempoolMetricsTraceTransformer :: Trace IO a -> Tracer IO (TraceEventMempool blk)
mempoolMetricsTraceTransformer tr = Tracer $ \mempoolEvent -> do
let tr' = appendName "metrics" tr
(_n, tot) = case mempoolEvent of
TraceMempoolAddedTx _tx0 _ tot0 -> (1, tot0)
TraceMempoolRejectedTx _tx0 _ tot0 -> (1, tot0)
TraceMempoolRemoveTxs txs0 tot0 -> (length txs0, tot0)
TraceMempoolManuallyRemovedTxs txs0 txs1 tot0 -> ( length txs0 + length txs1, tot0)
logValue1 :: LOContent a
logValue1 = LogValue "txsInMempool" $ PureI $ fromIntegral (msNumTxs tot)
logValue2 :: LOContent a
logValue2 = LogValue "mempoolBytes" . PureI . fromIntegral . unByteSize32 . msNumBytes $ tot
meta <- mkLOMeta Critical Confidential
traceNamedObject tr' (meta, logValue1)
traceNamedObject tr' (meta, logValue2)
(_n, tot_m) = case mempoolEvent of
TraceMempoolAddedTx _tx0 _ tot0 -> (1, Just tot0)
TraceMempoolRejectedTx _tx0 _ tot0 -> (1, Just tot0)
TraceMempoolRemoveTxs txs0 tot0 -> (length txs0, Just tot0)
TraceMempoolManuallyRemovedTxs txs0 txs1 tot0 -> ( length txs0 + length txs1, Just tot0)
TraceMempoolSynced _ -> (0, Nothing)
case tot_m of
Just tot -> do
let logValue1 :: LOContent a
logValue1 = LogValue "txsInMempool" $ PureI $ fromIntegral (msNumTxs tot)
logValue2 :: LOContent a
logValue2 = LogValue "mempoolBytes" . PureI . fromIntegral . unByteSize32 . msNumBytes $ tot
meta <- mkLOMeta Critical Confidential
traceNamedObject tr' (meta, logValue1)
traceNamedObject tr' (meta, logValue2)
Nothing -> return ()

mempoolTracer
:: ( ToJSON (GenTxId blk)
Expand Down

0 comments on commit 82fac7d

Please sign in to comment.