From 59ac3072b18ff1683059fbb93527ae781fe4752f Mon Sep 17 00:00:00 2001 From: Lapo Luchini Date: Fri, 20 Jun 2025 13:12:26 +0200 Subject: [PATCH] feat: increase time resolution when possible (collectors: os, system, time) (#2047) --- docs/collector.time.md | 2 +- internal/collector/filetime/filetime.go | 2 +- internal/collector/logon/logon.go | 2 +- internal/collector/os/os.go | 2 +- internal/collector/process/process_worker.go | 2 +- internal/collector/system/system.go | 2 +- internal/collector/time/time.go | 2 +- internal/collector/update/update.go | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/docs/collector.time.md b/docs/collector.time.md index 6754169f..2f8724c4 100644 --- a/docs/collector.time.md +++ b/docs/collector.time.md @@ -30,7 +30,7 @@ Matching is case-sensitive. | `windows_time_ntp_round_trip_delay_seconds` | Total roundtrip delay experienced by the NTP client in receiving a response from the server for the most recent request, in seconds. This is the time elapsed on the NTP client between transmitting a request to the NTP server and receiving a valid response from the server. | gauge | None | | `windows_time_ntp_server_outgoing_responses_total` | Total number of requests responded to by the NTP server. | counter | None | | `windows_time_ntp_server_incoming_requests_total` | Total number of requests received by the NTP server. | counter | None | -| `windows_time_current_timestamp_seconds` | Current time as reported by the operating system, in [Unix time](https://en.wikipedia.org/wiki/Unix_time). See [time.Unix()](https://golang.org/pkg/time/#Unix) for details | gauge | None | +| `windows_time_current_timestamp_seconds` | Current time as reported by the operating system, in [Unix time](https://en.wikipedia.org/wiki/Unix_time). See [time.UnixMicro()](https://golang.org/pkg/time/#UnixMicro) for details | gauge | None | | `windows_time_timezone` | Current timezone as reported by the operating system. | gauge | `timezone` | | `windows_time_clock_sync_source` | This value reflects the sync source of the system clock. | gauge | `type` | diff --git a/internal/collector/filetime/filetime.go b/internal/collector/filetime/filetime.go index 6e7f798e..65089b8a 100644 --- a/internal/collector/filetime/filetime.go +++ b/internal/collector/filetime/filetime.go @@ -172,7 +172,7 @@ func (c *Collector) collectGlobFilePath(ch chan<- prometheus.Metric, filePattern ch <- prometheus.MustNewConstMetric( c.fileMTime, prometheus.GaugeValue, - float64(fileInfo.ModTime().UTC().Unix()), + float64(fileInfo.ModTime().UTC().UnixMicro())/1e6, filePath, ) } diff --git a/internal/collector/logon/logon.go b/internal/collector/logon/logon.go index f7c17193..b7bd867d 100644 --- a/internal/collector/logon/logon.go +++ b/internal/collector/logon/logon.go @@ -96,7 +96,7 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) error { ch <- prometheus.MustNewConstMetric( c.sessionInfo, prometheus.GaugeValue, - float64(session.LogonTime.Unix()), + float64(session.LogonTime.UnixMicro())/1e6, session.LogonId.String(), session.UserName, session.LogonDomain, session.LogonType.String(), ) } diff --git a/internal/collector/os/os.go b/internal/collector/os/os.go index d84be008..bf02442e 100644 --- a/internal/collector/os/os.go +++ b/internal/collector/os/os.go @@ -290,7 +290,7 @@ func (c *Collector) collectTime(ch chan<- prometheus.Metric) error { ch <- prometheus.MustNewConstMetric( c.time, prometheus.GaugeValue, - float64(time.Now().Unix()), + float64(time.Now().UnixMicro())/1e6, ) ch <- prometheus.MustNewConstMetric( diff --git a/internal/collector/process/process_worker.go b/internal/collector/process/process_worker.go index b98a3a7c..5356de93 100644 --- a/internal/collector/process/process_worker.go +++ b/internal/collector/process/process_worker.go @@ -142,7 +142,7 @@ func (c *Collector) collectWorker() { name, pidString, parentPID, strconv.Itoa(int(processGroupID)), processOwner, cmdLine, ) - startTime := float64(time.Now().Unix() - int64(data.ElapsedTime)) + startTime := float64(time.Now().UnixMicro())/1e6 - data.ElapsedTime ch <- prometheus.MustNewConstMetric( c.startTimeOld, diff --git a/internal/collector/system/system.go b/internal/collector/system/system.go index 043a9a06..3aef4903 100644 --- a/internal/collector/system/system.go +++ b/internal/collector/system/system.go @@ -141,7 +141,7 @@ func (c *Collector) Build(_ *slog.Logger, _ *mi.Session) error { nil, ) - c.bootTimeTimestamp = float64(time.Now().Unix() - int64(kernel32.GetTickCount64()/1000)) + c.bootTimeTimestamp = float64(uint64(time.Now().UnixMilli())-kernel32.GetTickCount64()) / 1000 var err error diff --git a/internal/collector/time/time.go b/internal/collector/time/time.go index e878ba7b..8cc3b49a 100644 --- a/internal/collector/time/time.go +++ b/internal/collector/time/time.go @@ -245,7 +245,7 @@ func (c *Collector) collectTime(ch chan<- prometheus.Metric) error { ch <- prometheus.MustNewConstMetric( c.currentTime, prometheus.GaugeValue, - float64(time.Now().Unix()), + float64(time.Now().UnixMicro())/1e6, ) timeZoneInfo, err := kernel32.GetDynamicTimeZoneInformation() diff --git a/internal/collector/update/update.go b/internal/collector/update/update.go index bcc75ded..cd1cb84f 100644 --- a/internal/collector/update/update.go +++ b/internal/collector/update/update.go @@ -405,7 +405,7 @@ func (c *Collector) fetchUpdates(logger *slog.Logger, usd *ole.IDispatch) ([]pro metricsBuf = append(metricsBuf, prometheus.MustNewConstMetric( c.lastScrapeMetric, prometheus.GaugeValue, - float64(time.Now().Unix()), + float64(time.Now().UnixMicro())/1e6, )) return metricsBuf, nil