From 80fea91e3801b95a112cf9e2411c521732eda94d Mon Sep 17 00:00:00 2001 From: Botond Szirtes Date: Mon, 29 Jul 2024 14:19:32 +0200 Subject: [PATCH] Add an option to expose Prometheus metrics via http/s server Signed-off-by: Botond Szirtes --- pkg/networkservice/metrics/stats/common.go | 18 ++++ .../metrics/stats/prometheus.go | 87 +++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 pkg/networkservice/metrics/stats/prometheus.go diff --git a/pkg/networkservice/metrics/stats/common.go b/pkg/networkservice/metrics/stats/common.go index 60d613d1..bf1d5600 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/opentelemetry" "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 opentelemetry.IsPrometheusEnabled() { + 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..dec2cb0d --- /dev/null +++ b/pkg/networkservice/metrics/stats/prometheus.go @@ -0,0 +1,87 @@ +package stats + +import ( + "github.com/networkservicemesh/sdk/pkg/tools/opentelemetry" + "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 opentelemetry.IsPrometheusEnabled() { + 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) + } +}