mirror of
https://github.com/prometheus-community/windows_exporter.git
synced 2026-10-17 18:19:04 +02:00
2.5 KiB
2.5 KiB
service collector
EnumServicesStatusExreturns state and PID of all services in one call. The configuration needsOpenService,QueryServiceConfigandCloseServiceHandleper service, which costs CPU in services.exe as well, so it is cached inconfig_cache.go. Never cache state or PID.NotifyServiceStatusChangedoes not report configuration changes. The cache watchesHKLM\SYSTEM\CurrentControlSet\ServiceswithRegNotifyChangeKeyValueinstead. Arm it withREG_NOTIFY_THREAD_AGNOSTIC: without it the registration ends when the arming OS thread exits, and Go goroutines are not bound to one.- The delayed auto-start flag and the triggers are queried with
QueryServiceConfig2on the same handle and cached with the configuration. The SCM ignores the delayed flag unless the start type is auto, so it is only queried for auto-start services.x/sys/windowshas noSERVICE_TRIGGER_INFO; the mirror inservice.goonly readscTriggers. - The delayed flag is the
DelayedAutostartvalue of the service key, so a change updates the key's last write time. Triggers are stored in theTriggerInfosubkey. Adding the first trigger creates it, which updates the last write time of the service key (TestCollectorPicksUpConfigChangeschecks this). That a later trigger change made withsc triggerinfoalso does is assumed, not verified: the SCM is believed to rewrite the wholeTriggerInfosubkey. If it only changes deeper keys, the cache picks the change up after at mostconfigCacheMaxAge. - The subtree notification fires every few seconds on a normal machine (W32Time,
Tcpip, VSS, driver
Enumkeys, Hyper-V). Treat it as a hint only and compare the last write time of each service's own key, which changes only for its own values and direct subkeys. - Registry write times have a resolution of one clock tick, so two writes in
the same tick leave the time unchanged. Only cache a configuration once its
key is older than
configCacheMinKeyAge, and never cache an unavailable one (for example a missing MUI display name), which can change without a write. - The cache tests use a temporary key below
HKEY_CURRENT_USER, so they run without elevation. Registry write times have a coarse resolution; sleep before a write that must get a newer time. TestCollectorPicksUpConfigChangescreates a real service and changes it through the SCM. It needs an elevated process: it skips locally without one and fails in CI, whereserviceis inWINDOWS_EXPORTER_TEST_COLLECTORS.