mirror of
https://github.com/prometheus/node_exporter.git
synced 2024-12-28 06:59: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 (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/beevik/ntp"
|
"github.com/beevik/ntp"
|
||||||
|
@ -41,6 +42,7 @@ var (
|
||||||
ntpOffsetTolerance = kingpin.Flag("collector.ntp.local-offset-tolerance", "Offset between local clock and local ntpd time to tolerate").Default("1ms").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 {
|
type ntpCollector struct {
|
||||||
|
@ -143,6 +145,7 @@ func (c *ntpCollector) Update(ch chan<- prometheus.Metric) error {
|
||||||
// configuration from node_exporter user to the developer.
|
// configuration from node_exporter user to the developer.
|
||||||
|
|
||||||
maxerr := *ntpOffsetTolerance
|
maxerr := *ntpOffsetTolerance
|
||||||
|
leapMidnightMutex.Lock()
|
||||||
if resp.Leap == ntp.LeapAddSecond || resp.Leap == ntp.LeapDelSecond {
|
if resp.Leap == ntp.LeapAddSecond || resp.Leap == ntp.LeapDelSecond {
|
||||||
// state of leapMidnight is cached as leap flag is dropped right after midnight
|
// state of leapMidnight is cached as leap flag is dropped right after midnight
|
||||||
leapMidnight = resp.Time.Truncate(hour24).Add(hour24)
|
leapMidnight = resp.Time.Truncate(hour24).Add(hour24)
|
||||||
|
@ -151,6 +154,7 @@ func (c *ntpCollector) Update(ch chan<- prometheus.Metric) error {
|
||||||
// tolerate leap smearing
|
// tolerate leap smearing
|
||||||
maxerr += time.Second
|
maxerr += time.Second
|
||||||
}
|
}
|
||||||
|
leapMidnightMutex.Unlock()
|
||||||
|
|
||||||
if resp.Validate() == nil && resp.RootDistance <= *ntpMaxDistance && resp.MinError <= maxerr {
|
if resp.Validate() == nil && resp.RootDistance <= *ntpMaxDistance && resp.MinError <= maxerr {
|
||||||
ch <- c.sanity.mustNewConstMetric(1)
|
ch <- c.sanity.mustNewConstMetric(1)
|
||||||
|
|
Loading…
Reference in a new issue