Skip to content

Commit f3714be

Browse files
committed
Support journal format
Signed-off-by: Sasha Bogicevic <[email protected]>
1 parent 428aa10 commit f3714be

File tree

2 files changed

+46
-11
lines changed

2 files changed

+46
-11
lines changed

hydra-node/exe/visualize-logs/Main.hs

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
11
{-# LANGUAGE OverloadedRecordDot #-}
22

3+
-- | Parse hydra-node logs format more easy on the eyes. Parser works with regular json logs as well as journalctl format.
34
module Main where
45

56
import Hydra.Cardano.Api (Tx)
6-
import Hydra.Prelude hiding (encodeUtf8)
7-
import Hydra.Prelude qualified as P
7+
import Hydra.Prelude hiding (encodeUtf8, takeWhile)
88

99
import Conduit
1010
import Control.Lens ((^?))
1111
import Control.Monad (foldM)
1212
import Data.Aeson (eitherDecode')
1313
import Data.Aeson.Lens (key, _String)
14+
import Data.Attoparsec.ByteString
15+
import Data.Attoparsec.ByteString qualified as A
16+
import Data.Attoparsec.ByteString.Char8 (char8, endOfLine, isEndOfLine)
1417
import Data.Text.Encoding (encodeUtf8)
1518
import Hydra.Chain (ChainEvent (..))
1619
import Hydra.HeadLogic (Effect (..), Input (..), Outcome (..), StateChanged (..))
1720
import Hydra.Logging (Envelope (..))
1821
import Hydra.Logging.Messages (HydraLog (..))
1922
import Hydra.Node (HydraNodeLog (..))
20-
import Options.Applicative
23+
import Options.Applicative hiding (Parser)
24+
import Options.Applicative qualified as Options
2125

2226
data InfoLine = InfoLine {toplabel :: LogType, details :: Text} deriving (Eq, Show)
2327

@@ -79,7 +83,7 @@ newtype Options = Options
7983
}
8084
deriving (Show)
8185

82-
options :: Parser Options
86+
options :: Options.Parser Options
8387
options =
8488
Options
8589
<$> many
@@ -117,13 +121,43 @@ visualize paths = do
117121

118122
decodeAndProcess :: ByteString -> ResourceT IO (Decoded Tx)
119123
decodeAndProcess l =
120-
case l ^? key "message" . _String of
121-
Nothing -> P.error "Failed to find key 'message' which was expected"
122-
Just line ->
123-
let envelope = fromStrict $ encodeUtf8 line
124-
in case eitherDecode' envelope :: Either String (Envelope (HydraLog Tx)) of
125-
Left e -> P.error $ "Decoding failed" <> show e <> "for line: " <> line
126-
Right decoded -> lift $ processLogs decoded
124+
case inCurlyBraces l of
125+
Left _ -> lift $ pure DropLog
126+
Right incomingLine ->
127+
case incomingLine of
128+
Nothing -> lift $ pure DropLog
129+
Just jsonLine ->
130+
case jsonLine ^? key "message" . _String of
131+
Nothing -> process jsonLine
132+
Just line -> process $ encodeUtf8 line
133+
where
134+
process :: ByteString -> ResourceT IO (Decoded Tx)
135+
process line =
136+
case eitherDecode' (fromStrict line) of
137+
Left e -> do
138+
putTextLn $ red <> "Decoding failed for line: " <> decodeUtf8 l <> "\nError: " <> show e
139+
pure DropLog
140+
Right decoded -> lift $ processLogs decoded
141+
142+
charToWord8 :: Char -> Word8
143+
charToWord8 = fromIntegral . ord
144+
145+
textBetweenBraces :: A.Parser ByteString
146+
textBetweenBraces = do
147+
skipWhile (/= charToWord8 '{')
148+
_ <- char8 '{'
149+
jsonStr <- takeWhile (not . isEndOfLine)
150+
pure $ "{" <> jsonStr
151+
152+
lineParser :: A.Parser (Maybe ByteString)
153+
lineParser = do
154+
result <- optional textBetweenBraces
155+
skipWhile (not . isEndOfLine)
156+
endOfLine <|> endOfInput
157+
return result
158+
159+
inCurlyBraces :: ByteString -> Either String (Maybe ByteString)
160+
inCurlyBraces = parseOnly lineParser
127161

128162
-- | Ideally we would have Data instances for all types so we could get data type string representation
129163
-- instead of providing strings directly but that would add some compilation time overhead so not worth it.

hydra-node/hydra-node.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ executable visualize-logs
253253
main-is: Main.hs
254254
build-depends:
255255
, aeson
256+
, attoparsec
256257
, base
257258
, conduit
258259
, hydra-cardano-api

0 commit comments

Comments
 (0)