From b998636893c36b8315d214d9d8c5f33e49b79301 Mon Sep 17 00:00:00 2001 From: Tobias Klausmann Date: Wed, 16 Feb 2022 17:43:15 +0100 Subject: [PATCH] Improve error logging for missing config and QL dir (#10260) * Improve error logging for missing config and QL dir Currently, when Prometheus can't open its config file or the query logging dir under the data dir, it only logs what it has been given default or commandline/config. Depending on the environment this can be less than helpful, since the working directory may be unclear to the user. I have specifically kept the existing error messages as intact as possible to a) still log the parameter as given and b) cause as little disruption for log-parsers/-analyzers as possible. So in case of the config file or the data dir being non-absolute paths, I use os.GetWd to find the working dir and assemble an absolute path for error logging purposes. If GetWd fails, we just log "unknown", as recovering from an error there would be very complex measure, likely not worth the code/effort. Example errors: ``` $ ./prometheus ts=2022-02-06T16:00:53.034Z caller=main.go:445 level=error msg="Error loading config (--config.file=prometheus.yml)" fullpath=/home/klausman/src/prometheus/prometheus.yml err="open prometheus.yml: no such file or directory" $ touch prometheus.yml $ ./prometheus [...] ts=2022-02-06T16:01:00.992Z caller=query_logger.go:99 level=error component=activeQueryTracker msg="Error opening query log file" file=data/queries.active fullpath=/home/klausman/src/prometheus/data/queries.active err="open data/queries.active: permission denied" panic: Unable to create mmap-ed active query log [...] $ ``` Signed-off-by: Tobias Klausmann * Replace our own logic with just using filepath.Abs() Signed-off-by: Tobias Klausmann * Further simplification Signed-off-by: Tobias Klausmann * Review edits Signed-off-by: Tobias Klausmann * Review edits Signed-off-by: Tobias Klausmann * Review edits Signed-off-by: Tobias Klausmann --- cmd/prometheus/main.go | 6 +++++- promql/query_logger.go | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/cmd/prometheus/main.go b/cmd/prometheus/main.go index 6842832c61..d1563b1d50 100644 --- a/cmd/prometheus/main.go +++ b/cmd/prometheus/main.go @@ -428,7 +428,11 @@ func main() { // Throw error for invalid config before starting other components. var cfgFile *config.Config if cfgFile, err = config.LoadFile(cfg.configFile, agentMode, false, log.NewNopLogger()); err != nil { - level.Error(logger).Log("msg", fmt.Sprintf("Error loading config (--config.file=%s)", cfg.configFile), "err", err) + absPath, pathErr := filepath.Abs(cfg.configFile) + if pathErr != nil { + absPath = cfg.configFile + } + level.Error(logger).Log("msg", fmt.Sprintf("Error loading config (--config.file=%s)", cfg.configFile), "file", absPath, "err", err) os.Exit(2) } if cfg.tsdb.EnableExemplarStorage { diff --git a/promql/query_logger.go b/promql/query_logger.go index ecf93765ce..2c324fb90b 100644 --- a/promql/query_logger.go +++ b/promql/query_logger.go @@ -83,7 +83,11 @@ func logUnfinishedQueries(filename string, filesize int, logger log.Logger) { func getMMapedFile(filename string, filesize int, logger log.Logger) ([]byte, error) { file, err := os.OpenFile(filename, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0o666) if err != nil { - level.Error(logger).Log("msg", "Error opening query log file", "file", filename, "err", err) + absPath, pathErr := filepath.Abs(filename) + if pathErr != nil { + absPath = filename + } + level.Error(logger).Log("msg", "Error opening query log file", "file", absPath, "err", err) return nil, err }