From 9334269f2bbc00cb38cdb66f5a2dbcb02cb5e1d5 Mon Sep 17 00:00:00 2001 From: Julien Pivotto Date: Mon, 1 Feb 2021 21:18:42 +0100 Subject: [PATCH] backfill: move checkErr before we close the mmaped file When printing the error, we still need access to the mmapped byte array of the file. Therefore, we make sure that we run it before closing the file. I could have done something more complex like a defer, or not closing the file, knowing that we would exit the program anyway. However, I think that in case we extend this in the future, or this is copy/paster elsewhere, we should continue closing the file. As it is small enough, I went for the solution to call the function 3 times instead of playing with a defer. Signed-off-by: Julien Pivotto --- cmd/promtool/main.go | 2 +- cmd/promtool/tsdb.go | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/cmd/promtool/main.go b/cmd/promtool/main.go index 631b8d40a..e5b302520 100644 --- a/cmd/promtool/main.go +++ b/cmd/promtool/main.go @@ -207,7 +207,7 @@ func main() { os.Exit(checkErr(dumpSamples(*dumpPath, *dumpMinTime, *dumpMaxTime))) //TODO(aSquare14): Work on adding support for custom block size. case openMetricsImportCmd.FullCommand(): - os.Exit(checkErr(backfillOpenMetrics(*importFilePath, *importDBPath, *importHumanReadable))) + os.Exit(backfillOpenMetrics(*importFilePath, *importDBPath, *importHumanReadable)) } } diff --git a/cmd/promtool/tsdb.go b/cmd/promtool/tsdb.go index 85d8da112..8d04eadab 100644 --- a/cmd/promtool/tsdb.go +++ b/cmd/promtool/tsdb.go @@ -618,16 +618,16 @@ func checkErr(err error) int { return 0 } -func backfillOpenMetrics(path string, outputDir string, humanReadable bool) (err error) { +func backfillOpenMetrics(path string, outputDir string, humanReadable bool) int { inputFile, err := fileutil.OpenMmapFile(path) if err != nil { - return err + return checkErr(err) } defer inputFile.Close() if err := os.MkdirAll(outputDir, 0777); err != nil { - return errors.Wrap(err, "create output dir") + return checkErr(errors.Wrap(err, "create output dir")) } - return backfill(5000, inputFile.Bytes(), outputDir, humanReadable) + return checkErr(backfill(5000, inputFile.Bytes(), outputDir, humanReadable)) }