Skip to content

Commit 950659e

Browse files
authored
Merge pull request #6002 from IntersectMBO/cardano-tracer-logrotate
cardano-tracer: Fix multiple empty logs being produced.
2 parents 8e6c3b9 + 65e3a1f commit 950659e

File tree

5 files changed

+33
-33
lines changed

5 files changed

+33
-33
lines changed

cardano-tracer/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## 0.3 (September 26, 2024)
44

5+
* Fix the creation of empty logs.
56
* Abondon `snap` webserver in favour of `wai`/`warp` for Prometheus and EKG Monitoring.
67
* Add dynamic routing to EKG stores of all connected nodes.
78
* Derive URL compliant routes from connected node names (instead of plain node names).
@@ -54,4 +55,4 @@ Initial version.
5455

5556

5657

57-
[i5140]: https://github.com/IntersectMBO/cardano-node/issues/5140
58+
[i5140]: https://github.com/IntersectMBO/cardano-node/issues/5140

cardano-tracer/src/Cardano/Tracer/Handlers/Logs/File.hs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,17 @@ writeTraceObjectsToFile registry loggingParams@LoggingParams{logRoot, logFormat}
4949

5050
unless (null itemsToWrite) do
5151
readRegistry registry >>= \handleMap -> do
52-
case Map.lookup (nodeName, loggingParams) handleMap of
52+
let key = (nodeName, loggingParams)
53+
case Map.lookup key handleMap of
5354
Nothing -> do
5455
rootDirAbs <- makeAbsolute logRoot
5556

5657
let subDirForLogs :: FilePath
5758
subDirForLogs = rootDirAbs </> T.unpack nodeName
5859

59-
createEmptyLogRotation currentLogLock nodeName loggingParams registry subDirForLogs logFormat
60+
createEmptyLogRotation currentLogLock key registry subDirForLogs
6061
handles <- readRegistry registry
61-
let handle = fst (fromJust (Map.lookup (nodeName, loggingParams) handles))
62+
let handle = fst (fromJust (Map.lookup key handles))
6263
BS8.hPutStr handle preparedLines
6364
hFlush handle
6465
Just (handle, _filePath) -> do

cardano-tracer/src/Cardano/Tracer/Handlers/Logs/Rotator.hs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Cardano.Tracer.Environment
1010
import Cardano.Tracer.Handlers.Logs.Utils (createOrUpdateEmptyLog, getTimeStampFromLog,
1111
isItLog)
1212
import Cardano.Tracer.MetaTrace
13-
import Cardano.Tracer.Types (HandleRegistry, NodeName)
13+
import Cardano.Tracer.Types (HandleRegistry, HandleRegistryKey, NodeName)
1414
import Cardano.Tracer.Utils (showProblemIfAny, readRegistry)
1515

1616
import Control.Concurrent.Async (forConcurrently_)
@@ -77,7 +77,7 @@ checkRootDir
7777
-> RotationParams
7878
-> LoggingParams
7979
-> IO ()
80-
checkRootDir currentLogLock registry rotParams loggingParams@LoggingParams{logRoot, logFormat} = do
80+
checkRootDir currentLogLock registry rotParams loggingParams@LoggingParams{logRoot} = do
8181
logRootAbs <- makeAbsolute logRoot
8282
whenM (doesDirectoryExist logRootAbs) do
8383
logsSubDirs <- listDirectories logRootAbs
@@ -92,27 +92,25 @@ checkRootDir currentLogLock registry rotParams loggingParams@LoggingParams{logRo
9292
let nodeName :: NodeName
9393
nodeName = Text.pack (takeFileName logSubDir)
9494

95-
for_ @Maybe (Map.lookup (nodeName, loggingParams) handles) \(handle, filePath) -> let
96-
nodeName' :: NodeName
97-
nodeName' = Text.pack filePath
98-
in
99-
checkLogs currentLogLock handle nodeName' loggingParams registry rotParams logFormat (logRootAbs </> logSubDir)
95+
key :: HandleRegistryKey
96+
key = (nodeName, loggingParams)
97+
98+
for_ @Maybe (Map.lookup key handles) \(handle, _filePath) ->
99+
checkLogs currentLogLock handle key registry rotParams (logRootAbs </> logSubDir)
100100

101101
-- | We check the log files:
102102
-- 1. If there are too big log files.
103103
-- 2. If there are too old log files.
104104
checkLogs
105105
:: Lock
106106
-> Handle
107-
-> NodeName
108-
-> LoggingParams
107+
-> HandleRegistryKey
109108
-> HandleRegistry
110109
-> RotationParams
111-
-> LogFormat
112110
-> FilePath
113111
-> IO ()
114-
checkLogs currentLogLock handle nodeName loggingParams registry
115-
RotationParams{rpLogLimitBytes, rpMaxAgeMinutes, rpKeepFilesNum} format subDirForLogs = do
112+
checkLogs currentLogLock handle key@(_, LoggingParams{logFormat = format}) registry
113+
RotationParams{rpLogLimitBytes, rpMaxAgeMinutes, rpKeepFilesNum} subDirForLogs = do
116114

117115
logs <- map (subDirForLogs </>) . filter (isItLog format) <$> listFiles subDirForLogs
118116
unless (null logs) do
@@ -122,23 +120,21 @@ checkLogs currentLogLock handle nodeName loggingParams registry
122120
-- Usage of partial function 'last' is safe here (we already checked the list isn't empty).
123121
-- Only previous logs should be checked if they are outdated.
124122
allOtherLogs = dropEnd 1 fromOldestToNewest
125-
checkIfCurrentLogIsFull currentLogLock handle nodeName loggingParams registry format rpLogLimitBytes subDirForLogs
123+
checkIfCurrentLogIsFull currentLogLock handle key registry rpLogLimitBytes subDirForLogs
126124
checkIfThereAreOldLogs allOtherLogs rpMaxAgeMinutes rpKeepFilesNum
127125

128126
-- | If the current log file is full (it's size is too big), the new log will be created.
129127
checkIfCurrentLogIsFull
130128
:: Lock
131129
-> Handle
132-
-> NodeName
133-
-> LoggingParams
130+
-> HandleRegistryKey
134131
-> HandleRegistry
135-
-> LogFormat
136132
-> Word64
137133
-> FilePath
138134
-> IO ()
139-
checkIfCurrentLogIsFull currentLogLock handle nodeName loggingParams registry format maxSizeInBytes subDirForLogs =
135+
checkIfCurrentLogIsFull currentLogLock handle key registry maxSizeInBytes subDirForLogs =
140136
whenM logIsFull do
141-
createOrUpdateEmptyLog currentLogLock nodeName loggingParams registry subDirForLogs format
137+
createOrUpdateEmptyLog currentLogLock key registry subDirForLogs
142138

143139
where
144140
logIsFull :: IO Bool

cardano-tracer/src/Cardano/Tracer/Handlers/Logs/Utils.hs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module Cardano.Tracer.Handlers.Logs.Utils
1111
) where
1212

1313
import Cardano.Tracer.Configuration (LogFormat (..), LoggingParams (..))
14-
import Cardano.Tracer.Types (HandleRegistry, NodeName)
14+
import Cardano.Tracer.Types (HandleRegistry, HandleRegistryKey)
1515
import Cardano.Tracer.Utils (modifyRegistry_)
1616

1717
import Control.Concurrent.Extra (Lock, withLock)
@@ -49,31 +49,29 @@ isItLog format pathToLog = hasProperPrefix && hasTimestamp && hasProperExt
4949

5050
createEmptyLogRotation
5151
:: Lock
52-
-> NodeName
53-
-> LoggingParams
52+
-> HandleRegistryKey
5453
-> HandleRegistry
5554
-> FilePath
56-
-> LogFormat
5755
-> IO ()
58-
createEmptyLogRotation currentLogLock nodeName loggingParams registry subDirForLogs format = do
56+
createEmptyLogRotation currentLogLock key registry subDirForLogs = do
5957
-- The root directory (as a parent for subDirForLogs) will be created as well if needed.
6058
createDirectoryIfMissing True subDirForLogs
61-
createOrUpdateEmptyLog currentLogLock nodeName loggingParams registry subDirForLogs format
59+
createOrUpdateEmptyLog currentLogLock key registry subDirForLogs
6260

6361
-- | Create an empty log file (with the current timestamp in the name).
64-
createOrUpdateEmptyLog :: Lock -> NodeName -> LoggingParams -> HandleRegistry -> FilePath -> LogFormat -> IO ()
65-
createOrUpdateEmptyLog currentLogLock nodeName loggingParams registry subDirForLogs format = do
62+
createOrUpdateEmptyLog :: Lock -> HandleRegistryKey -> HandleRegistry -> FilePath -> IO ()
63+
createOrUpdateEmptyLog currentLogLock key@(_, LoggingParams{logFormat = format}) registry subDirForLogs = do
6664
withLock currentLogLock do
6765
ts <- formatTime defaultTimeLocale timeStampFormat . systemToUTCTime <$> getSystemTime
6866
let pathToLog = subDirForLogs </> logPrefix <> ts <.> logExtension format
6967

7068
modifyRegistry_ registry \handles -> do
7169

72-
for_ @Maybe (Map.lookup (nodeName, loggingParams) handles) \(handle, _filePath) ->
70+
for_ @Maybe (Map.lookup key handles) \(handle, _filePath) ->
7371
hClose handle
7472

7573
newHandle <- openFile pathToLog WriteMode
76-
let newMap = Map.insert (nodeName, loggingParams) (newHandle, pathToLog) handles
74+
let newMap = Map.insert key (newHandle, pathToLog) handles
7775
pure newMap
7876

7977
getTimeStampFromLog :: FilePath -> Maybe UTCTime

cardano-tracer/src/Cardano/Tracer/Types.hs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module Cardano.Tracer.Types
1111
, ProtocolsBrake
1212
, Registry (..)
1313
, HandleRegistry
14+
, HandleRegistryKey
1415
) where
1516

1617
import Cardano.Tracer.Configuration
@@ -61,5 +62,8 @@ type ProtocolsBrake = TVar Bool
6162
type Registry :: Type -> Type -> Type
6263
newtype Registry a b = Registry { getRegistry :: MVar (Map a b) }
6364

65+
type HandleRegistryKey :: Type
66+
type HandleRegistryKey = (NodeName, LoggingParams)
67+
6468
type HandleRegistry :: Type
65-
type HandleRegistry = Registry (NodeName, LoggingParams) (Handle, FilePath)
69+
type HandleRegistry = Registry HandleRegistryKey (Handle, FilePath)

0 commit comments

Comments
 (0)