mirror of
https://github.com/prometheus-community/windows_exporter.git
synced 2026-03-09 12:06:35 +00:00
process: Use registry collector for V1 data (#1814)
Signed-off-by: Jan-Otto Kröpke <mail@jkroepke.de>
This commit is contained in:
@@ -17,12 +17,12 @@ package udp
|
||||
|
||||
// The TCPv6 performance object uses the same fields.
|
||||
// https://learn.microsoft.com/en-us/dotnet/api/system.net.networkinformation.tcpstate?view=net-8.0.
|
||||
const (
|
||||
datagramsNoPortPerSec = "Datagrams No Port/sec"
|
||||
datagramsReceivedPerSec = "Datagrams Received/sec"
|
||||
datagramsReceivedErrors = "Datagrams Received Errors"
|
||||
datagramsSentPerSec = "Datagrams Sent/sec"
|
||||
)
|
||||
type perfDataCounterValues struct {
|
||||
DatagramsNoPortPerSec float64 `perfdata:"Datagrams No Port/sec"`
|
||||
DatagramsReceivedPerSec float64 `perfdata:"Datagrams Received/sec"`
|
||||
DatagramsReceivedErrors float64 `perfdata:"Datagrams Received Errors"`
|
||||
DatagramsSentPerSec float64 `perfdata:"Datagrams Sent/sec"`
|
||||
}
|
||||
|
||||
// Datagrams No Port/sec is the rate of received UDP datagrams for which there was no application at the destination port.
|
||||
// Datagrams Received Errors is the number of received UDP datagrams that could not be delivered for reasons other than the lack of an application at the destination port.
|
||||
@@ -16,13 +16,12 @@
|
||||
package udp
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
"github.com/alecthomas/kingpin/v2"
|
||||
"github.com/prometheus-community/windows_exporter/internal/mi"
|
||||
"github.com/prometheus-community/windows_exporter/internal/perfdata"
|
||||
"github.com/prometheus-community/windows_exporter/internal/pdh"
|
||||
"github.com/prometheus-community/windows_exporter/internal/types"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
)
|
||||
@@ -38,8 +37,10 @@ var ConfigDefaults = Config{}
|
||||
type Collector struct {
|
||||
config Config
|
||||
|
||||
perfDataCollector4 *perfdata.Collector
|
||||
perfDataCollector6 *perfdata.Collector
|
||||
perfDataCollector4 *pdh.Collector
|
||||
perfDataCollector6 *pdh.Collector
|
||||
perfDataObject4 []perfDataCounterValues
|
||||
perfDataObject6 []perfDataCounterValues
|
||||
|
||||
datagramsNoPortTotal *prometheus.Desc
|
||||
datagramsReceivedTotal *prometheus.Desc
|
||||
@@ -79,21 +80,14 @@ func (c *Collector) Close() error {
|
||||
}
|
||||
|
||||
func (c *Collector) Build(_ *slog.Logger, _ *mi.Session) error {
|
||||
counters := []string{
|
||||
datagramsNoPortPerSec,
|
||||
datagramsReceivedPerSec,
|
||||
datagramsReceivedErrors,
|
||||
datagramsSentPerSec,
|
||||
}
|
||||
|
||||
var err error
|
||||
|
||||
c.perfDataCollector4, err = perfdata.NewCollector("UDPv4", nil, counters)
|
||||
c.perfDataCollector4, err = pdh.NewCollector[perfDataCounterValues]("UDPv4", nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create UDPv4 collector: %w", err)
|
||||
}
|
||||
|
||||
c.perfDataCollector6, err = perfdata.NewCollector("UDPv6", nil, counters)
|
||||
c.perfDataCollector6, err = pdh.NewCollector[perfDataCounterValues]("UDPv6", nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create UDPv6 collector: %w", err)
|
||||
}
|
||||
@@ -133,54 +127,46 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) error {
|
||||
}
|
||||
|
||||
func (c *Collector) collect(ch chan<- prometheus.Metric) error {
|
||||
data, err := c.perfDataCollector4.Collect()
|
||||
err := c.perfDataCollector4.Collect(&c.perfDataObject4)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to collect UDPv4 metrics: %w", err)
|
||||
}
|
||||
|
||||
if _, ok := data[perfdata.InstanceEmpty]; !ok {
|
||||
return errors.New("no data for UDPv4")
|
||||
}
|
||||
c.writeUDPCounters(ch, c.perfDataObject4, []string{"ipv4"})
|
||||
|
||||
c.writeUDPCounters(ch, data[perfdata.InstanceEmpty], []string{"ipv4"})
|
||||
|
||||
data, err = c.perfDataCollector6.Collect()
|
||||
err = c.perfDataCollector6.Collect(&c.perfDataObject6)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to collect UDPv6 metrics: %w", err)
|
||||
}
|
||||
|
||||
if _, ok := data[perfdata.InstanceEmpty]; !ok {
|
||||
return errors.New("no data for UDPv6")
|
||||
}
|
||||
|
||||
c.writeUDPCounters(ch, data[perfdata.InstanceEmpty], []string{"ipv6"})
|
||||
c.writeUDPCounters(ch, c.perfDataObject6, []string{"ipv6"})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Collector) writeUDPCounters(ch chan<- prometheus.Metric, metrics map[string]perfdata.CounterValue, labels []string) {
|
||||
func (c *Collector) writeUDPCounters(ch chan<- prometheus.Metric, metrics []perfDataCounterValues, labels []string) {
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.datagramsNoPortTotal,
|
||||
prometheus.CounterValue,
|
||||
metrics[datagramsNoPortPerSec].FirstValue,
|
||||
metrics[0].DatagramsNoPortPerSec,
|
||||
labels...,
|
||||
)
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.datagramsReceivedErrorsTotal,
|
||||
prometheus.CounterValue,
|
||||
metrics[datagramsReceivedErrors].FirstValue,
|
||||
metrics[0].DatagramsReceivedErrors,
|
||||
labels...,
|
||||
)
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.datagramsReceivedTotal,
|
||||
prometheus.GaugeValue,
|
||||
metrics[datagramsReceivedPerSec].FirstValue,
|
||||
metrics[0].DatagramsReceivedPerSec,
|
||||
labels...,
|
||||
)
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.datagramsSentTotal,
|
||||
prometheus.CounterValue,
|
||||
metrics[datagramsSentPerSec].FirstValue,
|
||||
metrics[0].DatagramsSentPerSec,
|
||||
labels...,
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user