From de5261507512c15ce0f95c32b04ca1a553bf1f34 Mon Sep 17 00:00:00 2001 From: Conor Schaub <169469+akston@users.noreply.github.com> Date: Mon, 3 Oct 2022 11:20:34 -0400 Subject: [PATCH] Include peer configured names in metric label values #17 --- collector.go | 16 ++++++++-------- collector_test.go | 18 +++++++++--------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/collector.go b/collector.go index bf72814..41a53f6 100644 --- a/collector.go +++ b/collector.go @@ -55,28 +55,28 @@ func New(devices func() ([]*wgtypes.Device, error), peerNames map[string]string) PeerAllowedIPsInfo: prometheus.NewDesc( "wireguard_peer_allowed_ips_info", "Metadata about each of a peer's allowed IP subnets for a given device.", - append(labels, []string{"allowed_ips", "family"}...), + append(labels, []string{"allowed_ips", "family", "name"}...), nil, ), PeerReceiveBytes: prometheus.NewDesc( "wireguard_peer_receive_bytes_total", "Number of bytes received from a given peer.", - labels, + append(labels, []string{"name"}...), nil, ), PeerTransmitBytes: prometheus.NewDesc( "wireguard_peer_transmit_bytes_total", "Number of bytes transmitted to a given peer.", - labels, + append(labels, []string{"name"}...), nil, ), PeerLastHandshake: prometheus.NewDesc( "wireguard_peer_last_handshake_seconds", "UNIX timestamp for the last handshake with a given peer.", - labels, + append(labels, []string{"name"}...), nil, ), @@ -142,7 +142,7 @@ func (c *collector) Collect(ch chan<- prometheus.Metric) { c.PeerAllowedIPsInfo, prometheus.GaugeValue, 1, - d.Name, pub, ip.String(), ipFamily(ip.IP), + d.Name, pub, ip.String(), ipFamily(ip.IP), name, ) } @@ -150,14 +150,14 @@ func (c *collector) Collect(ch chan<- prometheus.Metric) { c.PeerReceiveBytes, prometheus.CounterValue, float64(p.ReceiveBytes), - d.Name, pub, + d.Name, pub, name, ) ch <- prometheus.MustNewConstMetric( c.PeerTransmitBytes, prometheus.CounterValue, float64(p.TransmitBytes), - d.Name, pub, + d.Name, pub, name, ) // Expose last handshake of 0 unless a last handshake time is set. @@ -170,7 +170,7 @@ func (c *collector) Collect(ch chan<- prometheus.Metric) { c.PeerLastHandshake, prometheus.GaugeValue, last, - d.Name, pub, + d.Name, pub, name, ) } } diff --git a/collector_test.go b/collector_test.go index f972008..9a7501f 100644 --- a/collector_test.go +++ b/collector_test.go @@ -68,15 +68,15 @@ func TestCollector(t *testing.T) { `wireguard_device_info{device="wg1",public_key="AgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgI="} 1`, `wireguard_peer_info{device="wg0",endpoint="[fd00::1]:51820",name="foo",public_key="AwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwM="} 1`, `wireguard_peer_info{device="wg1",endpoint="",name="foo",public_key="AwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwM="} 1`, - `wireguard_peer_allowed_ips_info{allowed_ips="192.168.1.0/24",device="wg0",family="IPv4",public_key="AwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwM="} 1`, - `wireguard_peer_allowed_ips_info{allowed_ips="2001:db8::/32",device="wg0",family="IPv6",public_key="AwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwM="} 1`, - `wireguard_peer_allowed_ips_info{allowed_ips="0.0.0.0/0",device="wg1",family="IPv4",public_key="AwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwM="} 1`, - `wireguard_peer_last_handshake_seconds{device="wg0",public_key="AwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwM="} 10`, - `wireguard_peer_last_handshake_seconds{device="wg1",public_key="AwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwM="} 0`, - `wireguard_peer_receive_bytes_total{device="wg0",public_key="AwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwM="} 1`, - `wireguard_peer_receive_bytes_total{device="wg1",public_key="AwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwM="} 0`, - `wireguard_peer_transmit_bytes_total{device="wg0",public_key="AwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwM="} 2`, - `wireguard_peer_transmit_bytes_total{device="wg1",public_key="AwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwM="} 0`, + `wireguard_peer_allowed_ips_info{allowed_ips="192.168.1.0/24",device="wg0",family="IPv4",name="foo",public_key="AwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwM="} 1`, + `wireguard_peer_allowed_ips_info{allowed_ips="2001:db8::/32",device="wg0",family="IPv6",name="foo",public_key="AwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwM="} 1`, + `wireguard_peer_allowed_ips_info{allowed_ips="0.0.0.0/0",device="wg1",family="IPv4",name="foo",public_key="AwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwM="} 1`, + `wireguard_peer_last_handshake_seconds{device="wg0",name="foo",public_key="AwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwM="} 10`, + `wireguard_peer_last_handshake_seconds{device="wg1",name="foo",public_key="AwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwM="} 0`, + `wireguard_peer_receive_bytes_total{device="wg0",name="foo",public_key="AwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwM="} 1`, + `wireguard_peer_receive_bytes_total{device="wg1",name="foo",public_key="AwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwM="} 0`, + `wireguard_peer_transmit_bytes_total{device="wg0",name="foo",public_key="AwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwM="} 2`, + `wireguard_peer_transmit_bytes_total{device="wg1",name="foo",public_key="AwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwM="} 0`, }, }, }