Skip to content

Commit 24938d2

Browse files
committed
trace-dispatcher: Review fixes1
1 parent e056894 commit 24938d2

File tree

6 files changed

+69
-79
lines changed

6 files changed

+69
-79
lines changed

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,10 @@ module Cardano.Logging.DocuGenerator (
2525
) where
2626

2727
import Cardano.Logging.ConfigurationParser ()
28-
import Cardano.Logging.DocuGenerator.RoseTree
28+
import Cardano.Logging.DocuGenerator.Tree
2929
import Cardano.Logging.DocuResult (DocuResult (..))
3030
import qualified Cardano.Logging.DocuResult as DocuResult
3131
import Cardano.Logging.Types
32-
import Cardano.Logging.Utils (indent)
3332

3433
import Prelude hiding (lines, unlines)
3534

@@ -487,9 +486,9 @@ generateTOC DocTracer {..} traces metrics datapoints =
487486
<> generateTOCDatapoints
488487
<> generateTOCRest
489488
where
490-
tracesTree = mapMaybe (trim []) (toTree traces)
491-
metricsTree = toTree (fmap splitToNS metrics)
492-
datapointsTree = toTree datapoints
489+
tracesTree = mapMaybe (trim []) (toForest traces)
490+
metricsTree = toForest (fmap splitToNS metrics)
491+
datapointsTree = toForest datapoints
493492

494493
generateTOCTraces =
495494
fromText "### [Trace Messages](#trace-messages)\n\n"
@@ -516,25 +515,27 @@ generateTOC DocTracer {..} traces metrics datapoints =
516515

517516
-- Modify the given tracer tree so that the result is a tree where entries which
518517
-- are not tracers are removed. In case the whole tree doesn't contain a tracer, return Nothing.
519-
trim :: [Text] {- accumulated namespace in reverse -} -> RoseTree -> Maybe RoseTree
520-
trim ns (RoseTree x nested) =
518+
trim :: [Text] {- accumulated namespace in reverse -} -> Tree Text -> Maybe (Tree Text)
519+
trim ns (Node x nested) =
521520
let that = reverse (x : ns)
522521
-- List of all nested tracers that we shall render
523522
nestedTrimmed = mapMaybe (trim (x : ns)) nested in
524-
mfilter (\_ -> not (null nestedTrimmed) || isTracerSymbol that) (Just (RoseTree x nestedTrimmed))
523+
mfilter (\_ -> not (null nestedTrimmed) || isTracerSymbol that) (Just (Node x nestedTrimmed))
525524

526525
namespaceToToc ::
527526
[[Text]]
528527
-> Bool
529528
-> [Text] {- Accumulated namespace in reverse -}
530-
-> RoseTree
529+
-> Tree Text
531530
-> Builder
532-
namespaceToToc allTracers skipSymbols accns (RoseTree x nested) = text
531+
namespaceToToc allTracers skipSymbols accns (Node x nested) = text
533532
where
534533
ns = reverse (x : accns)
535534

536535
inner = mconcat (map (namespaceToToc allTracers skipSymbols (x : accns)) nested)
537536

537+
indent lvl txt = mconcat (replicate lvl "\t") <> txt
538+
538539
text :: Builder
539540
text =
540541
indent (length accns)

trace-dispatcher/src/Cardano/Logging/DocuGenerator/RoseTree.hs

Lines changed: 0 additions & 50 deletions
This file was deleted.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
module Cardano.Logging.DocuGenerator.Tree (Tree (..), foldTree, printTree, printList, toForest) where
2+
3+
import Data.Function (on)
4+
import Data.List (groupBy, intersperse)
5+
import Data.Text.Internal (Text)
6+
import Data.Text.Internal.Builder (Builder)
7+
import Data.Tree (Forest, Tree (..), foldTree, unfoldForest)
8+
9+
-- T ::= ∙ x
10+
-- |
11+
-- ∙ x
12+
-- T
13+
-- T
14+
-- ...
15+
-- T
16+
--
17+
-- Example:
18+
--
19+
-- ∙ BlockFetch
20+
-- ∙ Client
21+
-- ∙ AcknowledgedFetchRequest
22+
-- ∙ AddedFetchRequest
23+
-- ∙ ClientMetrics
24+
-- ∙ Decision
25+
-- ∙ Remote
26+
printTree :: Tree Text -> Text
27+
printTree =
28+
foldTree (\x -> mconcat . intersperse "\n" . ("" <> x :) . map ("\t" <>))
29+
30+
printList :: (a -> Builder) -> [a] -> Builder
31+
printList fmt = mconcat . intersperse "\n" . map fmt
32+
33+
-- Convert a list of namespaces to a tree representation
34+
toForest :: [[Text]] -> Forest Text
35+
toForest = unfoldForest build . groupByHead
36+
where
37+
groupByHead = groupBy (on (==) head)
38+
39+
build :: [[Text]] -> (Text, [[[Text]]])
40+
build group@(representative : _) = (head representative, (groupByHead . filter (not . null) . map tail) group)

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1-
module Cardano.Logging.DocuResult (DocuResult(..), unpackDocu, isTracer, isMetric, isDatapoint) where
2-
import Data.Text.Internal.Builder
1+
module Cardano.Logging.DocuResult
2+
(DocuResult(..)
3+
, unpackDocu
4+
, isTracer
5+
, isMetric
6+
, isDatapoint)
7+
where
8+
9+
import Data.Text.Internal.Builder
310

411
data DocuResult =
512
DocuTracer Builder
Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
module Cardano.Logging.Utils (module Cardano.Logging.Utils)
2-
where
1+
module Cardano.Logging.Utils
2+
( module Cardano.Logging.Utils )
3+
where
34

45
import Cardano.Logging.Types (HowToConnect)
56

67
import Control.Concurrent (threadDelay)
78
import Control.Exception (SomeAsyncException (..), fromException, tryJust)
89
import Control.Tracer (stdoutTracer, traceWith)
910
import qualified Data.Text as T
10-
import Data.Text.Internal.Builder (Builder)
1111
import qualified Data.Text.Lazy as TL (toStrict)
12-
import qualified Data.Text.Lazy.Builder as T (fromText, toLazyText)
12+
import qualified Data.Text.Lazy.Builder as T (toLazyText)
1313
import qualified Data.Text.Lazy.Builder.Int as T
1414
import qualified Data.Text.Lazy.Builder.RealFloat as T (realFloat)
1515
import GHC.Conc (labelThread, myThreadId)
@@ -28,7 +28,7 @@ runInLoop action howToConnect prevDelayInSecs maxReconnectDelay =
2828
where
2929
excludeAsyncExceptions e =
3030
case fromException e of
31-
Just SomeAsyncException{} -> Nothing
31+
Just SomeAsyncException {} -> Nothing
3232
_ -> Just e
3333

3434
logTrace = traceWith stdoutTracer
@@ -38,24 +38,16 @@ runInLoop action howToConnect prevDelayInSecs maxReconnectDelay =
3838

3939
-- | Convenience function for a Show instance to be converted to text immediately
4040
{-# INLINE showT #-}
41-
showT :: (Show a) => a -> T.Text
41+
showT :: Show a => a -> T.Text
4242
showT = T.pack . show
4343

4444
{-# INLINE showTHex #-}
45-
showTHex :: (Integral a) => a -> T.Text
45+
showTHex :: Integral a => a -> T.Text
4646
showTHex = TL.toStrict . T.toLazyText . T.hexadecimal
4747

4848
{-# INLINE showTReal #-}
49-
showTReal :: (RealFloat a) => a -> T.Text
49+
showTReal :: RealFloat a => a -> T.Text
5050
showTReal = TL.toStrict . T.toLazyText . T.realFloat
5151

5252
threadLabelMe :: String -> IO ()
5353
threadLabelMe label = myThreadId >>= flip labelThread label
54-
55-
indent :: Int -> Builder -> Builder
56-
indent lvl txt
57-
| lvl == 0 = txt
58-
| lvl /= 0 = T.fromText tab <> indent (lvl - 1) txt
59-
where
60-
tab :: T.Text
61-
tab = "\t"

trace-dispatcher/trace-dispatcher.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ library
4646
Cardano.Logging.ConfigurationParser
4747
Cardano.Logging.Consistency
4848
Cardano.Logging.DocuGenerator
49-
Cardano.Logging.DocuGenerator.RoseTree
49+
Cardano.Logging.DocuGenerator.Tree
5050
Cardano.Logging.DocuResult
5151
Cardano.Logging.Formatter
5252
Cardano.Logging.Forwarding

0 commit comments

Comments
 (0)