From 521a4231d6f78c2481f8145ba1b26c6a825c7edd Mon Sep 17 00:00:00 2001 From: Robert Cowham Date: Wed, 7 Apr 2021 14:29:17 +0100 Subject: [PATCH] Write metrics to a cached file and rename it - avoids race conditions with node_exporter --- p4prom.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/p4prom.go b/p4prom.go index 1b580c8..5f0ab29 100644 --- a/p4prom.go +++ b/p4prom.go @@ -67,13 +67,14 @@ func readServerID(logger *logrus.Logger, instance string) string { return "" } -// Writes metrics to appropriate file +// Writes metrics to appropriate file - writes to temp file first and renames it after func (p4p *P4Prometheus) writeMetricsFile(metrics []byte) { var f *os.File var err error - f, err = os.Create(p4p.config.MetricsOutput) + tmpFile := p4p.config.MetricsOutput + ".tmp" + f, err = os.Create(tmpFile) if err != nil { - p4p.logger.Errorf("Error opening %s: %v", p4p.config.MetricsOutput, err) + p4p.logger.Errorf("Error opening %s: %v", tmpFile, err) return } f.Write(bytes.ToValidUTF8(metrics, []byte{'?'})) @@ -81,10 +82,14 @@ func (p4p *P4Prometheus) writeMetricsFile(metrics []byte) { if err != nil { p4p.logger.Errorf("Error closing file: %v", err) } - err = os.Chmod(p4p.config.MetricsOutput, 0644) + err = os.Chmod(tmpFile, 0644) if err != nil { p4p.logger.Errorf("Error chmod-ing file: %v", err) } + err = os.Rename(tmpFile, p4p.config.MetricsOutput) + if err != nil { + p4p.logger.Errorf("Error renaming: %s to %s - %v", tmpFile, p4p.config.MetricsOutput, err) + } } // Returns a tailer object for specified file