-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core: aggregate errors that happen too often during infra loading
Signed-off-by: Eloi Charpentier <[email protected]>
- Loading branch information
Showing
5 changed files
with
147 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
core/kt-osrd-utils/src/main/kotlin/fr/sncf/osrd/utils/LogAggregator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package fr.sncf.osrd.utils | ||
|
||
/** | ||
* This class can be used to aggregate logs: when a specific error/warning happens thousands of time | ||
* in a row, we only report the first $n and only log the total error number afterward. The class | ||
* must be initialized once before the loop, errors should be logged with `logError`, and then | ||
* `logAggregatedSummary` should be called once at the end. | ||
*/ | ||
data class LogAggregator( | ||
/** Function to use to log anything (e.g. `{ logger.warn(it) }` ). */ | ||
val logFunction: (str: String) -> Unit, | ||
/** String to be used for collapsed errors, using %d and .format for the remaining number. */ | ||
val summaryErrorMessage: String = "... and %d other similar errors", | ||
/** Max number of errors before collapsing the rest. */ | ||
val maxReportedErrors: Int = 3, | ||
) { | ||
private var nErrors = 0 | ||
private var savedErrors = mutableListOf<String>() | ||
|
||
/** Registers an error. Does not log anything before the `reportSummary` call. */ | ||
fun registerError(msg: String) { | ||
nErrors++ | ||
if (savedErrors.size < maxReportedErrors) savedErrors.add(msg) | ||
} | ||
|
||
/** Logs the errors, collapsing the ones after `maxReportedErrors`. */ | ||
fun logAggregatedSummary() { | ||
for (err in savedErrors) logFunction(err) | ||
val remainingErrors = nErrors - savedErrors.size | ||
if (remainingErrors > 0) logFunction(summaryErrorMessage.format(remainingErrors)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters