os: remove deprecated metrics (#2116)

This commit is contained in:
Jan-Otto Kröpke
2025-07-05 15:44:57 +02:00
committed by GitHub
parent ed15b3c671
commit 373d741260
3 changed files with 7 additions and 259 deletions

View File

@@ -23,18 +23,13 @@ import (
"log/slog"
"strconv"
"strings"
"time"
"github.com/alecthomas/kingpin/v2"
"github.com/prometheus-community/windows_exporter/internal/headers/kernel32"
"github.com/prometheus-community/windows_exporter/internal/headers/netapi32"
"github.com/prometheus-community/windows_exporter/internal/headers/psapi"
"github.com/prometheus-community/windows_exporter/internal/headers/sysinfoapi"
"github.com/prometheus-community/windows_exporter/internal/mi"
"github.com/prometheus-community/windows_exporter/internal/osversion"
"github.com/prometheus-community/windows_exporter/internal/types"
"github.com/prometheus/client_golang/prometheus"
"golang.org/x/sys/windows"
"golang.org/x/sys/windows/registry"
)
@@ -51,40 +46,6 @@ type Collector struct {
hostname *prometheus.Desc
osInformation *prometheus.Desc
// Deprecated: Use windows_system_processes instead.
processes *prometheus.Desc
// Deprecated: Use windows_system_process_limit instead.
processesLimit *prometheus.Desc
// users
// Deprecated: Use `sum(windows_terminal_services_session_info{state="active"})` instead.
users *prometheus.Desc
// physicalMemoryFreeBytes
// Deprecated: Use windows_memory_physical_free_bytes instead.
physicalMemoryFreeBytes *prometheus.Desc
// processMemoryLimitBytes
// Deprecated: Use windows_memory_process_memory_limit_bytes instead.
processMemoryLimitBytes *prometheus.Desc
// time
// Deprecated: Use windows_time_current_timestamp_seconds instead.
time *prometheus.Desc
// timezone
// Deprecated: Use windows_time_timezone instead.
timezone *prometheus.Desc
// virtualMemoryBytes
// Deprecated: Use windows_memory_commit_limit instead.
virtualMemoryBytes *prometheus.Desc
// virtualMemoryFreeBytes
// Deprecated: Use windows_memory_commit_limit instead.
virtualMemoryFreeBytes *prometheus.Desc
// visibleMemoryBytes
// Deprecated: Use windows_memory_physical_total_bytes instead.
visibleMemoryBytes *prometheus.Desc
}
func New(config *Config) *Collector {
@@ -111,12 +72,7 @@ func (c *Collector) Close() error {
return nil
}
func (c *Collector) Build(logger *slog.Logger, _ *mi.Session) error {
logger.Warn("The os collector holds a number of deprecated metrics and will be removed mid 2025. "+
"See https://github.com/prometheus-community/windows_exporter/pull/1596 for more information.",
slog.String("collector", Name),
)
func (c *Collector) Build(_ *slog.Logger, _ *mi.Session) error {
productName, revision, err := c.getWindowsVersion()
if err != nil {
return fmt.Errorf("failed to get Windows version: %w", err)
@@ -153,67 +109,6 @@ func (c *Collector) Build(logger *slog.Logger, _ *mi.Session) error {
},
nil,
)
c.physicalMemoryFreeBytes = prometheus.NewDesc(
prometheus.BuildFQName(types.Namespace, Name, "physical_memory_free_bytes"),
"Deprecated: Use `windows_memory_physical_free_bytes` instead.",
nil,
nil,
)
c.time = prometheus.NewDesc(
prometheus.BuildFQName(types.Namespace, Name, "time"),
"Deprecated: Use windows_time_current_timestamp_seconds instead.",
nil,
nil,
)
c.timezone = prometheus.NewDesc(
prometheus.BuildFQName(types.Namespace, Name, "timezone"),
"Deprecated: Use windows_time_timezone instead.",
[]string{"timezone"},
nil,
)
c.processes = prometheus.NewDesc(
prometheus.BuildFQName(types.Namespace, Name, "processes"),
"Deprecated: Use `windows_system_processes` instead.",
nil,
nil,
)
c.processesLimit = prometheus.NewDesc(
prometheus.BuildFQName(types.Namespace, Name, "processes_limit"),
"Deprecated: Use `windows_system_process_limit` instead.",
nil,
nil,
)
c.processMemoryLimitBytes = prometheus.NewDesc(
prometheus.BuildFQName(types.Namespace, Name, "process_memory_limit_bytes"),
"Deprecated: Use `windows_memory_process_memory_limit_bytes` instead.",
nil,
nil,
)
c.users = prometheus.NewDesc(
prometheus.BuildFQName(types.Namespace, Name, "users"),
"Deprecated: Use `sum(windows_terminal_services_session_info{state=\"active\"})` instead.",
nil,
nil,
)
c.virtualMemoryBytes = prometheus.NewDesc(
prometheus.BuildFQName(types.Namespace, Name, "virtual_memory_bytes"),
"Deprecated: Use `windows_memory_commit_limit` instead.",
nil,
nil,
)
c.visibleMemoryBytes = prometheus.NewDesc(
prometheus.BuildFQName(types.Namespace, Name, "visible_memory_bytes"),
"Deprecated: Use `windows_memory_physical_total_bytes` instead.",
nil,
nil,
)
c.virtualMemoryFreeBytes = prometheus.NewDesc(
prometheus.BuildFQName(types.Namespace, Name, "virtual_memory_free_bytes"),
"Deprecated: Use `windows_memory_commit_limit - windows_memory_committed_bytes` instead.",
nil,
nil,
)
return nil
}
@@ -223,46 +118,19 @@ func (c *Collector) Build(logger *slog.Logger, _ *mi.Session) error {
func (c *Collector) Collect(ch chan<- prometheus.Metric) error {
errs := make([]error, 0)
c.collect(ch)
if err := c.collectProcessCount(ch); err != nil {
errs = append(errs, fmt.Errorf("failed to collect process count metrics: %w", err))
}
ch <- prometheus.MustNewConstMetric(
c.osInformation,
prometheus.GaugeValue,
1.0,
)
if err := c.collectHostname(ch); err != nil {
errs = append(errs, fmt.Errorf("failed to collect hostname metrics: %w", err))
}
if err := c.collectLoggedInUserCount(ch); err != nil {
errs = append(errs, fmt.Errorf("failed to collect user count metrics: %w", err))
}
if err := c.collectMemory(ch); err != nil {
errs = append(errs, fmt.Errorf("failed to collect memory metrics: %w", err))
}
if err := c.collectTime(ch); err != nil {
errs = append(errs, fmt.Errorf("failed to collect time metrics: %w", err))
}
return errors.Join(errs...)
}
func (c *Collector) collectLoggedInUserCount(ch chan<- prometheus.Metric) error {
workstationInfo, err := netapi32.GetWorkstationInfo()
if err != nil {
return err
}
ch <- prometheus.MustNewConstMetric(
c.users,
prometheus.GaugeValue,
float64(workstationInfo.LoggedOnUsers),
)
return nil
}
func (c *Collector) collectHostname(ch chan<- prometheus.Metric) error {
hostname, err := sysinfoapi.GetComputerName(sysinfoapi.ComputerNameDNSHostname)
if err != nil {
@@ -291,101 +159,6 @@ func (c *Collector) collectHostname(ch chan<- prometheus.Metric) error {
return nil
}
func (c *Collector) collectProcessCount(ch chan<- prometheus.Metric) error {
gpi, err := psapi.GetPerformanceInfo()
if err != nil {
return err
}
ch <- prometheus.MustNewConstMetric(c.processes,
prometheus.GaugeValue,
float64(gpi.ProcessCount),
)
return nil
}
func (c *Collector) collectTime(ch chan<- prometheus.Metric) error {
timeZoneInfo, err := kernel32.GetDynamicTimeZoneInformation()
if err != nil {
return err
}
// timeZoneKeyName contains the english name of the timezone.
timezoneName := windows.UTF16ToString(timeZoneInfo.TimeZoneKeyName[:])
ch <- prometheus.MustNewConstMetric(
c.time,
prometheus.GaugeValue,
float64(time.Now().UnixMicro())/1e6,
)
ch <- prometheus.MustNewConstMetric(
c.timezone,
prometheus.GaugeValue,
1.0,
timezoneName,
)
return nil
}
func (c *Collector) collectMemory(ch chan<- prometheus.Metric) error {
memoryStatusEx, err := sysinfoapi.GlobalMemoryStatusEx()
if err != nil {
return err
}
ch <- prometheus.MustNewConstMetric(
c.physicalMemoryFreeBytes,
prometheus.GaugeValue,
float64(memoryStatusEx.AvailPhys),
)
ch <- prometheus.MustNewConstMetric(
c.virtualMemoryFreeBytes,
prometheus.GaugeValue,
float64(memoryStatusEx.AvailPageFile),
)
ch <- prometheus.MustNewConstMetric(
c.virtualMemoryBytes,
prometheus.GaugeValue,
float64(memoryStatusEx.TotalPageFile),
)
ch <- prometheus.MustNewConstMetric(
c.visibleMemoryBytes,
prometheus.GaugeValue,
float64(memoryStatusEx.TotalPhys),
)
ch <- prometheus.MustNewConstMetric(
c.processMemoryLimitBytes,
prometheus.GaugeValue,
float64(memoryStatusEx.TotalVirtual),
)
return nil
}
func (c *Collector) collect(ch chan<- prometheus.Metric) {
ch <- prometheus.MustNewConstMetric(
c.osInformation,
prometheus.GaugeValue,
1.0,
)
// Windows has no defined limit, and is based off available resources. This currently isn't calculated by WMI and is set to default value.
// https://techcommunity.microsoft.com/t5/windows-blog-archive/pushing-the-limits-of-windows-processes-and-threads/ba-p/723824
// https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-operatingsystem
ch <- prometheus.MustNewConstMetric(
c.processesLimit,
prometheus.GaugeValue,
float64(4294967295),
)
}
func (c *Collector) getWindowsVersion() (string, string, error) {
// Get build number and product name from registry
ntKey, err := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Windows NT\CurrentVersion`, registry.QUERY_VALUE)