Skip to content

Commit 6ac41d4

Browse files
committed
leiosdemo202511: hacky special case for mux_tm
1 parent 9e457f9 commit 6ac41d4

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,23 @@ import qualified Ouroboros.Network.Protocol.LocalStateQuery.Type as LSQ
1919
import qualified Ouroboros.Network.Protocol.LocalTxMonitor.Type as LTM
2020
import qualified Ouroboros.Network.Protocol.LocalTxSubmission.Type as LTS
2121

22+
import Control.Monad.Class.MonadTime.SI (Time (..))
2223
import Data.Aeson (Value (String), (.=), (.?=))
2324
import Data.Text (Text, pack)
2425
import qualified Network.TypedProtocol.Codec as Simple
2526
import qualified Network.TypedProtocol.Stateful.Codec as Stateful
2627

2728
{-# ANN module ("HLint: ignore Redundant bracket" :: Text) #-}
2829

30+
jsonTime :: Time -> Double
31+
jsonTime (Time x) = realToFrac x
32+
2933
instance LogFormatting (Simple.AnyMessage ps)
3034
=> LogFormatting (Simple.TraceSendRecv ps) where
3135
forMachine dtal (Simple.TraceSendMsg tm m) = mconcat
32-
[ "kind" .= String "Send" , "msg" .= forMachine dtal m, "tm" .= String (pack $ show tm) ]
36+
[ "kind" .= String "Send" , "msg" .= forMachine dtal m, "mux_at" .= jsonTime tm ]
3337
forMachine dtal (Simple.TraceRecvMsg mbTm m) = mconcat
34-
[ "kind" .= String "Recv" , "msg" .= forMachine dtal m, "tm" .?= fmap (String . pack . show) mbTm ]
38+
[ "kind" .= String "Recv" , "msg" .= forMachine dtal m, "mux_at" .?= fmap jsonTime mbTm ]
3539

3640
forHuman (Simple.TraceSendMsg _tm m) = "Send: " <> forHumanOrMachine m
3741
forHuman (Simple.TraceRecvMsg _mbTm m) = "Receive: " <> forHumanOrMachine m
@@ -42,9 +46,9 @@ instance LogFormatting (Simple.AnyMessage ps)
4246
instance LogFormatting (Stateful.AnyMessage ps f)
4347
=> LogFormatting (Stateful.TraceSendRecv ps f) where
4448
forMachine dtal (Stateful.TraceSendMsg tm m) = mconcat
45-
[ "kind" .= String "Send" , "msg" .= forMachine dtal m, "tm" .= String (pack $ show tm) ]
49+
[ "kind" .= String "Send" , "msg" .= forMachine dtal m, "mux_at" .= jsonTime tm ]
4650
forMachine dtal (Stateful.TraceRecvMsg mbTm m) = mconcat
47-
[ "kind" .= String "Recv" , "msg" .= forMachine dtal m, "tm" .?= fmap (String . pack . show) mbTm ]
51+
[ "kind" .= String "Recv" , "msg" .= forMachine dtal m, "mux_at" .?= fmap jsonTime mbTm ]
4852

4953
forHuman (Stateful.TraceSendMsg _tm m) = "Send: " <> forHumanOrMachine m
5054
forHuman (Stateful.TraceRecvMsg _mbTm m) = "Receive: " <> forHumanOrMachine m

cardano-node/src/Cardano/Tracing/OrphanInstances/Network.hs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,18 +1445,20 @@ instance (ToJSON peer, ConvertRawHash header)
14451445
instance ToObject (AnyMessage ps)
14461446
=> ToObject (TraceSendRecv ps) where
14471447
toObject verb (TraceSendMsg tm m) = mconcat
1448-
[ "kind" .= String "Send" , "msg" .= toObject verb m, "tm" .= String (pack $ show tm) ]
1448+
[ "kind" .= String "Send" , "msg" .= toObject verb m, "mux_at" .= jsonTime tm ]
14491449
toObject verb (TraceRecvMsg mbTm m) = mconcat
1450-
[ "kind" .= String "Recv" , "msg" .= toObject verb m, "tm" Aeson..?= fmap (String . pack . show) mbTm ]
1450+
[ "kind" .= String "Recv" , "msg" .= toObject verb m, "mux_at" Aeson..?= fmap jsonTime mbTm ]
14511451

14521452

14531453
instance ToObject (Stateful.AnyMessage ps f)
14541454
=> ToObject (Stateful.TraceSendRecv ps f) where
14551455
toObject verb (Stateful.TraceSendMsg tm m) = mconcat
1456-
[ "kind" .= String "Send" , "msg" .= toObject verb m, "tm" .= String (pack $ show tm) ]
1456+
[ "kind" .= String "Send" , "msg" .= toObject verb m, "mux_at" .= jsonTime tm ]
14571457
toObject verb (Stateful.TraceRecvMsg mbTm m) = mconcat
1458-
[ "kind" .= String "Recv" , "msg" .= toObject verb m, "tm" Aeson..?= fmap (String . pack . show) mbTm ]
1458+
[ "kind" .= String "Recv" , "msg" .= toObject verb m, "mux_at" Aeson..?= fmap jsonTime mbTm ]
14591459

1460+
jsonTime :: Time -> Double
1461+
jsonTime (Time x) = realToFrac x
14601462

14611463
instance ToObject (TraceTxSubmissionInbound txid tx) where
14621464
toObject _verb (TraceTxSubmissionCollected count) =

trace-dispatcher/src/Cardano/Logging/Formatter.hs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,22 @@ import qualified Control.Tracer as T
2626
import Data.Aeson ((.=))
2727
import qualified Data.Aeson as AE
2828
import qualified Data.Aeson.Encoding as AE
29+
import qualified Data.Aeson.KeyMap as AE
2930
import Data.Functor.Contravariant
3031
import Data.Maybe (fromMaybe)
3132
import Data.Text as T (Text, intercalate, null, pack)
3233
import Data.Text.Lazy (toStrict)
3334
import Data.Text.Lazy.Builder as TB
3435
import Data.Text.Lazy.Encoding (decodeUtf8)
35-
import Data.Time (UTCTime, defaultTimeLocale, formatTime, getCurrentTime)
36+
import Data.Time (UTCTime, addUTCTime, defaultTimeLocale, formatTime, getCurrentTime)
37+
import GHC.Clock (getMonotonicTime)
3638
import Network.HostName
3739
import System.IO.Unsafe (unsafePerformIO)
3840

3941

42+
data I a = I a
43+
instance Functor I where fmap f (I x) = I (f x)
44+
4045
encodingToText :: AE.Encoding -> Text
4146
{-# INLINE encodingToText #-}
4247
encodingToText = toStrict . decodeUtf8 . AE.encodingToLazyByteString
@@ -45,6 +50,10 @@ timeFormatted :: UTCTime -> Text
4550
{-# INLINE timeFormatted #-}
4651
timeFormatted = pack . formatTime defaultTimeLocale "%F %H:%M:%S%4QZ"
4752

53+
timeFormattedT :: UTCTime -> Text
54+
{-# INLINE timeFormattedT #-}
55+
timeFormattedT = pack . formatTime defaultTimeLocale "%FT%H:%M:%S%8QZ"
56+
4857
-- If the hostname in the logs should be anything different from the system reported hostname,
4958
-- a new field would need to be added to PreFormatted to carry a new hostname argument to preFormatted.
5059
hostname :: Text
@@ -84,13 +93,21 @@ preFormatted withForHuman =
8493
flip contramapM
8594
(\case
8695
(lc, Right msg) -> do
96+
tm <- liftIO getMonotonicTime
8797
time <- liftIO getCurrentTime
98+
let tmf tm' = flip addUTCTime time $ fromRational $ tm' - toRational tm
8899
threadId <- liftIO myThreadId
89100
let ns' = lcNSPrefix lc ++ lcNSInner lc
90101
threadTextShortened = T.pack $ drop 9 $ show threadId -- drop "ThreadId " prefix
91102
details = fromMaybe DNormal (lcDetails lc)
92103
condForHuman = let txt = forHuman msg in if T.null txt then Nothing else Just txt
93-
machineFormatted = AE.toEncoding $ forMachine details msg
104+
obj = forMachine details msg
105+
-- nasty special case for a numeric "mux_tm" field
106+
I obj' = (\f -> AE.alterF f "mux_at" obj) $ \case
107+
Nothing -> I Nothing
108+
Just (AE.Number tm') -> I $ Just $ AE.String $ timeFormattedT $ tmf $ toRational tm'
109+
Just x -> I $ Just x
110+
machineFormatted = AE.toEncoding $ obj'
94111

95112
pure (lc, Right (PreFormatted
96113
{ pfForHuman = if withForHuman then condForHuman else Nothing

0 commit comments

Comments
 (0)