mirror of
https://github.com/prometheus/node_exporter.git
synced 2024-12-26 22:19:44 -08:00
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>
This commit is contained in:
parent
140b8b85c3
commit
23f95c8e04
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue