mirror of
https://github.com/prometheus-community/windows_exporter.git
synced 2026-03-02 16:46:35 +00:00
chore: release 0.29.0.rc0 (#1600)
This commit is contained in:
16
pkg/collector/tcp/const.go
Normal file
16
pkg/collector/tcp/const.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package tcp
|
||||
|
||||
// Win32_PerfRawData_Tcpip_TCPv4 docs
|
||||
// - https://msdn.microsoft.com/en-us/library/aa394341(v=vs.85).aspx
|
||||
// The TCPv6 performance object uses the same fields.
|
||||
const (
|
||||
ConnectionFailures = "Connection Failures"
|
||||
ConnectionsActive = "Connections Active"
|
||||
ConnectionsEstablished = "Connections Established"
|
||||
ConnectionsPassive = "Connections Passive"
|
||||
ConnectionsReset = "Connections Reset"
|
||||
SegmentsPersec = "Segments/sec"
|
||||
SegmentsReceivedPersec = "Segments Received/sec"
|
||||
SegmentsRetransmittedPersec = "Segments Retransmitted/sec"
|
||||
SegmentsSentPersec = "Segments Sent/sec"
|
||||
)
|
||||
@@ -3,10 +3,11 @@
|
||||
package tcp
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
"github.com/alecthomas/kingpin/v2"
|
||||
"github.com/go-kit/log"
|
||||
"github.com/go-kit/log/level"
|
||||
"github.com/prometheus-community/windows_exporter/pkg/perflib"
|
||||
"github.com/prometheus-community/windows_exporter/pkg/perfdata"
|
||||
"github.com/prometheus-community/windows_exporter/pkg/types"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/yusufpapurcu/wmi"
|
||||
@@ -22,6 +23,9 @@ var ConfigDefaults = Config{}
|
||||
type Collector struct {
|
||||
config Config
|
||||
|
||||
perfDataCollector4 *perfdata.Collector
|
||||
perfDataCollector6 *perfdata.Collector
|
||||
|
||||
connectionFailures *prometheus.Desc
|
||||
connectionsActive *prometheus.Desc
|
||||
connectionsEstablished *prometheus.Desc
|
||||
@@ -53,15 +57,39 @@ func (c *Collector) GetName() string {
|
||||
return Name
|
||||
}
|
||||
|
||||
func (c *Collector) GetPerfCounter(_ log.Logger) ([]string, error) {
|
||||
return []string{"TCPv4"}, nil
|
||||
func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) {
|
||||
return []string{}, nil
|
||||
}
|
||||
|
||||
func (c *Collector) Close() error {
|
||||
func (c *Collector) Close(_ *slog.Logger) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Collector) Build(_ log.Logger, _ *wmi.Client) error {
|
||||
func (c *Collector) Build(_ *slog.Logger, _ *wmi.Client) error {
|
||||
counters := []string{
|
||||
ConnectionFailures,
|
||||
ConnectionsActive,
|
||||
ConnectionsEstablished,
|
||||
ConnectionsPassive,
|
||||
ConnectionsReset,
|
||||
SegmentsPersec,
|
||||
SegmentsReceivedPersec,
|
||||
SegmentsRetransmittedPersec,
|
||||
SegmentsSentPersec,
|
||||
}
|
||||
|
||||
var err error
|
||||
|
||||
c.perfDataCollector4, err = perfdata.NewCollector("TCPv4", nil, counters)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create TCPv4 collector: %w", err)
|
||||
}
|
||||
|
||||
c.perfDataCollector6, err = perfdata.NewCollector("TCPv6", nil, counters)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create TCPv6 collector: %w", err)
|
||||
}
|
||||
|
||||
c.connectionFailures = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "connection_failures_total"),
|
||||
"(TCP.ConnectionFailures)",
|
||||
@@ -116,111 +144,96 @@ func (c *Collector) Build(_ log.Logger, _ *wmi.Client) error {
|
||||
[]string{"af"},
|
||||
nil,
|
||||
)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Collect sends the metric values for each metric
|
||||
// to the provided prometheus Metric channel.
|
||||
func (c *Collector) Collect(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error {
|
||||
logger = log.With(logger, "collector", Name)
|
||||
if err := c.collect(ctx, logger, ch); err != nil {
|
||||
_ = level.Error(logger).Log("msg", "failed collecting tcp metrics", "err", err)
|
||||
func (c *Collector) Collect(_ *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error {
|
||||
logger = logger.With(slog.String("collector", Name))
|
||||
if err := c.collect(ch); err != nil {
|
||||
logger.Error("failed collecting tcp metrics",
|
||||
slog.Any("err", err),
|
||||
)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Win32_PerfRawData_Tcpip_TCPv4 docs
|
||||
// - https://msdn.microsoft.com/en-us/library/aa394341(v=vs.85).aspx
|
||||
// The TCPv6 performance object uses the same fields.
|
||||
type tcp struct {
|
||||
ConnectionFailures float64 `perflib:"Connection Failures"`
|
||||
ConnectionsActive float64 `perflib:"Connections Active"`
|
||||
ConnectionsEstablished float64 `perflib:"Connections Established"`
|
||||
ConnectionsPassive float64 `perflib:"Connections Passive"`
|
||||
ConnectionsReset float64 `perflib:"Connections Reset"`
|
||||
SegmentsPersec float64 `perflib:"Segments/sec"`
|
||||
SegmentsReceivedPersec float64 `perflib:"Segments Received/sec"`
|
||||
SegmentsRetransmittedPersec float64 `perflib:"Segments Retransmitted/sec"`
|
||||
SegmentsSentPersec float64 `perflib:"Segments Sent/sec"`
|
||||
}
|
||||
|
||||
func writeTCPCounters(metrics tcp, labels []string, c *Collector, ch chan<- prometheus.Metric) {
|
||||
func writeTCPCounters(metrics map[string]perfdata.CounterValues, labels []string, c *Collector, ch chan<- prometheus.Metric) {
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.connectionFailures,
|
||||
prometheus.CounterValue,
|
||||
metrics.ConnectionFailures,
|
||||
metrics[ConnectionFailures].FirstValue,
|
||||
labels...,
|
||||
)
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.connectionsActive,
|
||||
prometheus.CounterValue,
|
||||
metrics.ConnectionsActive,
|
||||
metrics[ConnectionsActive].FirstValue,
|
||||
labels...,
|
||||
)
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.connectionsEstablished,
|
||||
prometheus.GaugeValue,
|
||||
metrics.ConnectionsEstablished,
|
||||
metrics[ConnectionsEstablished].FirstValue,
|
||||
labels...,
|
||||
)
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.connectionsPassive,
|
||||
prometheus.CounterValue,
|
||||
metrics.ConnectionsPassive,
|
||||
metrics[ConnectionsPassive].FirstValue,
|
||||
labels...,
|
||||
)
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.connectionsReset,
|
||||
prometheus.CounterValue,
|
||||
metrics.ConnectionsReset,
|
||||
metrics[ConnectionsReset].FirstValue,
|
||||
labels...,
|
||||
)
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.segmentsTotal,
|
||||
prometheus.CounterValue,
|
||||
metrics.SegmentsPersec,
|
||||
metrics[SegmentsPersec].FirstValue,
|
||||
labels...,
|
||||
)
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.segmentsReceivedTotal,
|
||||
prometheus.CounterValue,
|
||||
metrics.SegmentsReceivedPersec,
|
||||
metrics[SegmentsReceivedPersec].FirstValue,
|
||||
labels...,
|
||||
)
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.segmentsRetransmittedTotal,
|
||||
prometheus.CounterValue,
|
||||
metrics.SegmentsRetransmittedPersec,
|
||||
metrics[SegmentsRetransmittedPersec].FirstValue,
|
||||
labels...,
|
||||
)
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.segmentsSentTotal,
|
||||
prometheus.CounterValue,
|
||||
metrics.SegmentsSentPersec,
|
||||
metrics[SegmentsSentPersec].FirstValue,
|
||||
labels...,
|
||||
)
|
||||
}
|
||||
|
||||
func (c *Collector) collect(ctx *types.ScrapeContext, logger log.Logger, ch chan<- prometheus.Metric) error {
|
||||
logger = log.With(logger, "collector", Name)
|
||||
var dst []tcp
|
||||
|
||||
// TCPv4 counters
|
||||
if err := perflib.UnmarshalObject(ctx.PerfObjects["TCPv4"], &dst, logger); err != nil {
|
||||
return err
|
||||
}
|
||||
if len(dst) != 0 {
|
||||
writeTCPCounters(dst[0], []string{"ipv4"}, c, ch)
|
||||
func (c *Collector) collect(ch chan<- prometheus.Metric) error {
|
||||
data, err := c.perfDataCollector4.Collect()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to collect TCPv4 metrics: %w", err)
|
||||
}
|
||||
|
||||
// TCPv6 counters
|
||||
if err := perflib.UnmarshalObject(ctx.PerfObjects["TCPv6"], &dst, logger); err != nil {
|
||||
return err
|
||||
}
|
||||
if len(dst) != 0 {
|
||||
writeTCPCounters(dst[0], []string{"ipv6"}, c, ch)
|
||||
writeTCPCounters(data[perfdata.EmptyInstance], []string{"ipv4"}, c, ch)
|
||||
|
||||
data, err = c.perfDataCollector6.Collect()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to collect TCPv6 metrics: %w", err)
|
||||
}
|
||||
|
||||
writeTCPCounters(data[perfdata.EmptyInstance], []string{"ipv6"}, c, ch)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user