From 23f95c8e04bb21f7a20d93097ace6a5de61c882b Mon Sep 17 00:00:00 2001 From: Ben Kochie Date: Sun, 22 Jul 2018 14:36:33 +0200 Subject: [PATCH] Fix ntp collector thread safety (#1014) Make the ntp collector thread safe by wrapping a mutex lock around the leapMidnight variable. Signed-off-by: Ben Kochie --- collector/ntp.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/collector/ntp.go b/collector/ntp.go index 2580353c..e545a1d6 100644 --- a/collector/ntp.go +++ b/collector/ntp.go @@ -18,6 +18,7 @@ package collector import ( "fmt" "net" + "sync" "time" "github.com/beevik/ntp" @@ -40,7 +41,8 @@ var ( ntpMaxDistance = kingpin.Flag("collector.ntp.max-distance", "Max accumulated distance to the root").Default("3.46608s").Duration() ntpOffsetTolerance = kingpin.Flag("collector.ntp.local-offset-tolerance", "Offset between local clock and local ntpd time to tolerate").Default("1ms").Duration() - leapMidnight time.Time + leapMidnight time.Time + leapMidnightMutex = &sync.Mutex{} ) type ntpCollector struct { @@ -143,6 +145,7 @@ func (c *ntpCollector) Update(ch chan<- prometheus.Metric) error { // configuration from node_exporter user to the developer. maxerr := *ntpOffsetTolerance + leapMidnightMutex.Lock() if resp.Leap == ntp.LeapAddSecond || resp.Leap == ntp.LeapDelSecond { // state of leapMidnight is cached as leap flag is dropped right after midnight leapMidnight = resp.Time.Truncate(hour24).Add(hour24) @@ -151,6 +154,7 @@ func (c *ntpCollector) Update(ch chan<- prometheus.Metric) error { // tolerate leap smearing maxerr += time.Second } + leapMidnightMutex.Unlock() if resp.Validate() == nil && resp.RootDistance <= *ntpMaxDistance && resp.MinError <= maxerr { ch <- c.sanity.mustNewConstMetric(1)