diff --git a/go.mod b/go.mod index 6fe027f8..21cffd92 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,7 @@ require ( github.com/networkservicemesh/sdk v0.5.1-0.20240820090035-6fad31a9f0aa github.com/networkservicemesh/sdk-kernel v0.0.0-20240820090342-573b7f288d21 github.com/pkg/errors v0.9.1 + github.com/prometheus/client_golang v1.17.0 github.com/stretchr/testify v1.8.4 github.com/vishvananda/netlink v1.2.1-beta.2.0.20220630165224-c591ada0fb2b github.com/vishvananda/netns v0.0.0-20211101163701-50045581ed74 @@ -52,7 +53,6 @@ require ( github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect github.com/open-policy-agent/opa v0.44.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/prometheus/client_golang v1.17.0 // indirect github.com/prometheus/client_model v0.5.0 // indirect github.com/prometheus/common v0.44.0 // indirect github.com/prometheus/procfs v0.11.1 // indirect diff --git a/pkg/networkservice/metrics/stats/common.go b/pkg/networkservice/metrics/stats/common.go index 60d613d1..5b88147e 100644 --- a/pkg/networkservice/metrics/stats/common.go +++ b/pkg/networkservice/metrics/stats/common.go @@ -32,6 +32,7 @@ import ( "github.com/networkservicemesh/api/pkg/api/networkservice" "github.com/networkservicemesh/sdk/pkg/tools/log" + "github.com/networkservicemesh/sdk/pkg/tools/prometheus" "github.com/pkg/errors" "github.com/networkservicemesh/sdk-vpp/pkg/tools/ifindex" @@ -67,6 +68,23 @@ func retrieveMetrics(ctx context.Context, statsConn *core.StatsConnection, segme segment.Metrics[addName+"rx_packets"] = strconv.FormatUint(iface.Rx.Packets, 10) segment.Metrics[addName+"tx_packets"] = strconv.FormatUint(iface.Tx.Packets, 10) segment.Metrics[addName+"drops"] = strconv.FormatUint(iface.Drops, 10) + + if prometheus.IsEnabled() { + if addName == "server_" { + ServerRxBytes.Set(float64(iface.Rx.Bytes)) + ServerTxBytes.Set(float64(iface.Tx.Bytes)) + ServerRxPackets.Set(float64(iface.Rx.Packets)) + ServerTxPackets.Set(float64(iface.Tx.Packets)) + ServerDrops.Set(float64(iface.Drops)) + } else { + ClientRxBytes.Set(float64(iface.Rx.Bytes)) + ClientTxBytes.Set(float64(iface.Tx.Bytes)) + ClientRxPackets.Set(float64(iface.Rx.Packets)) + ClientTxPackets.Set(float64(iface.Tx.Packets)) + ClientDrops.Set(float64(iface.Drops)) + } + } + break } } diff --git a/pkg/networkservice/metrics/stats/prometheus.go b/pkg/networkservice/metrics/stats/prometheus.go new file mode 100644 index 00000000..872f3da5 --- /dev/null +++ b/pkg/networkservice/metrics/stats/prometheus.go @@ -0,0 +1,87 @@ +package stats + +import ( + prom "github.com/networkservicemesh/sdk/pkg/tools/prometheus" + "github.com/prometheus/client_golang/prometheus" +) + +var ( + ClientRxBytes = prometheus.NewGauge( + prometheus.GaugeOpts{ + Name: "client_rx_bytes_total", + Help: "Total received bytes by client", + }, + ) + ClientTxBytes = prometheus.NewGauge( + prometheus.GaugeOpts{ + Name: "client_tx_bytes_total", + Help: "Total transmitted bytes by client", + }, + ) + ClientRxPackets = prometheus.NewGauge( + prometheus.GaugeOpts{ + Name: "client_rx_packets_total", + Help: "Total received packets by client", + }, + ) + ClientTxPackets = prometheus.NewGauge( + prometheus.GaugeOpts{ + Name: "client_tx_packets_total", + Help: "Total transmitted packets by client", + }, + ) + ClientDrops = prometheus.NewGauge( + prometheus.GaugeOpts{ + Name: "client_drops_total", + Help: "Total drops by client", + }, + ) + ServerRxBytes = prometheus.NewGauge( + prometheus.GaugeOpts{ + Name: "server_rx_bytes_total", + Help: "Total received bytes by server", + }, + ) + ServerTxBytes = prometheus.NewGauge( + prometheus.GaugeOpts{ + Name: "server_tx_bytes_total", + Help: "Total transmitted bytes by server", + }, + ) + + ServerRxPackets = prometheus.NewGauge( + prometheus.GaugeOpts{ + Name: "server_rx_packets_total", + Help: "Total received packets by server", + }, + ) + + ServerTxPackets = prometheus.NewGauge( + prometheus.GaugeOpts{ + Name: "server_tx_packets_total", + Help: "Total transmitted packets by server", + }, + ) + + ServerDrops = prometheus.NewGauge( + prometheus.GaugeOpts{ + Name: "server_drops_total", + Help: "Total drops by server", + }, + ) +) + +func init() { + if prom.IsEnabled() { + prometheus.MustRegister(ClientRxBytes) + prometheus.MustRegister(ClientTxBytes) + prometheus.MustRegister(ClientRxPackets) + prometheus.MustRegister(ClientTxPackets) + prometheus.MustRegister(ClientDrops) + prometheus.MustRegister(ServerRxBytes) + prometheus.MustRegister(ServerTxBytes) + prometheus.MustRegister(ServerRxPackets) + prometheus.MustRegister(ServerTxPackets) + prometheus.MustRegister(ServerDrops) + } +}