diff options
author | Ben Kochie <superq@gmail.com> | 2018-07-22 14:36:33 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-07-22 14:36:33 +0200 |
commit | 23f95c8e04bb21f7a20d93097ace6a5de61c882b (patch) | |
tree | 8c6adaf4f0cf3c54b89a47e9c98a93d11808f317 | |
parent | 140b8b85c37553f5baebf433ac9be284cd1dd80d (diff) | |
download | prometheus_node_collector-23f95c8e04bb21f7a20d93097ace6a5de61c882b.tar.bz2 prometheus_node_collector-23f95c8e04bb21f7a20d93097ace6a5de61c882b.tar.xz prometheus_node_collector-23f95c8e04bb21f7a20d93097ace6a5de61c882b.zip |
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 <superq@gmail.com>
-rw-r--r-- | collector/ntp.go | 6 |
1 files changed, 5 insertions, 1 deletions
diff --git a/collector/ntp.go b/collector/ntp.go index 2580353..e545a1d 100644 --- a/collector/ntp.go +++ b/collector/ntp.go | |||
@@ -18,6 +18,7 @@ package collector | |||
18 | import ( | 18 | import ( |
19 | "fmt" | 19 | "fmt" |
20 | "net" | 20 | "net" |
21 | "sync" | ||
21 | "time" | 22 | "time" |
22 | 23 | ||
23 | "github.com/beevik/ntp" | 24 | "github.com/beevik/ntp" |
@@ -40,7 +41,8 @@ var ( | |||
40 | ntpMaxDistance = kingpin.Flag("collector.ntp.max-distance", "Max accumulated distance to the root").Default("3.46608s").Duration() | 41 | ntpMaxDistance = kingpin.Flag("collector.ntp.max-distance", "Max accumulated distance to the root").Default("3.46608s").Duration() |
41 | ntpOffsetTolerance = kingpin.Flag("collector.ntp.local-offset-tolerance", "Offset between local clock and local ntpd time to tolerate").Default("1ms").Duration() | 42 | ntpOffsetTolerance = kingpin.Flag("collector.ntp.local-offset-tolerance", "Offset between local clock and local ntpd time to tolerate").Default("1ms").Duration() |
42 | 43 | ||
43 | leapMidnight time.Time | 44 | leapMidnight time.Time |
45 | leapMidnightMutex = &sync.Mutex{} | ||
44 | ) | 46 | ) |
45 | 47 | ||
46 | type ntpCollector struct { | 48 | type ntpCollector struct { |
@@ -143,6 +145,7 @@ func (c *ntpCollector) Update(ch chan<- prometheus.Metric) error { | |||
143 | // configuration from node_exporter user to the developer. | 145 | // configuration from node_exporter user to the developer. |
144 | 146 | ||
145 | maxerr := *ntpOffsetTolerance | 147 | maxerr := *ntpOffsetTolerance |
148 | leapMidnightMutex.Lock() | ||
146 | if resp.Leap == ntp.LeapAddSecond || resp.Leap == ntp.LeapDelSecond { | 149 | if resp.Leap == ntp.LeapAddSecond || resp.Leap == ntp.LeapDelSecond { |
147 | // state of leapMidnight is cached as leap flag is dropped right after midnight | 150 | // state of leapMidnight is cached as leap flag is dropped right after midnight |
148 | leapMidnight = resp.Time.Truncate(hour24).Add(hour24) | 151 | leapMidnight = resp.Time.Truncate(hour24).Add(hour24) |
@@ -151,6 +154,7 @@ func (c *ntpCollector) Update(ch chan<- prometheus.Metric) error { | |||
151 | // tolerate leap smearing | 154 | // tolerate leap smearing |
152 | maxerr += time.Second | 155 | maxerr += time.Second |
153 | } | 156 | } |
157 | leapMidnightMutex.Unlock() | ||
154 | 158 | ||
155 | if resp.Validate() == nil && resp.RootDistance <= *ntpMaxDistance && resp.MinError <= maxerr { | 159 | if resp.Validate() == nil && resp.RootDistance <= *ntpMaxDistance && resp.MinError <= maxerr { |
156 | ch <- c.sanity.mustNewConstMetric(1) | 160 | ch <- c.sanity.mustNewConstMetric(1) |