From bcc9773ea7ff34fd94978c068717eb2722f3b3f5 Mon Sep 17 00:00:00 2001 From: Simone Rodigari Date: Tue, 21 Jan 2025 17:25:12 +0000 Subject: [PATCH] feat(conntrack-metrics): add conntrack_total_connections metric and fix comments --- pkg/metrics/metrics.go | 6 ++++++ pkg/metrics/types.go | 18 ++++++++++-------- pkg/plugin/conntrack/conntrack_linux.go | 9 +++++---- pkg/utils/metric_names.go | 1 + 4 files changed, 22 insertions(+), 12 deletions(-) diff --git a/pkg/metrics/metrics.go b/pkg/metrics/metrics.go index 17c2bae292..91d8201a33 100644 --- a/pkg/metrics/metrics.go +++ b/pkg/metrics/metrics.go @@ -182,6 +182,12 @@ func InitializeMetrics() { ConntrackBytesReplyDescription, ) + ConntrackTotalConnections = exporter.CreatePrometheusGaugeVecForMetric( + exporter.DefaultRegistry, + utils.ConntrackTotalConnectionsName, + ConntrackTotalConnectionsDescription, + ) + isInitialized = true metricsLogger.Info("Metrics initialized") } diff --git a/pkg/metrics/types.go b/pkg/metrics/types.go index 1f4e2d0a66..d934d0338e 100644 --- a/pkg/metrics/types.go +++ b/pkg/metrics/types.go @@ -45,10 +45,11 @@ const ( lostEventsCounterDescription = "Number of events lost in control plane" // Conntrack metrics - ConntrackPacketForwardDescription = "Number of forward packets" - ConntrackPacketReplyDescription = "Number of reply packets" - ConntrackBytesForwardDescription = "Number of forward bytes" - ConntrackBytesReplyDescription = "Number of reply bytes" + ConntrackPacketForwardDescription = "Number of forward packets" + ConntrackPacketReplyDescription = "Number of reply packets" + ConntrackBytesForwardDescription = "Number of forward bytes" + ConntrackBytesReplyDescription = "Number of reply bytes" + ConntrackTotalConnectionsDescription = "Total number of connections" ) // Metric Counters @@ -97,10 +98,11 @@ var ( InfinibandStatusParamsGauge GaugeVec // Conntrack - ConntrackPacketsForward GaugeVec - ConntrackPacketsReply GaugeVec - ConntrackBytesForward GaugeVec - ConntrackBytesReply GaugeVec + ConntrackPacketsForward GaugeVec + ConntrackPacketsReply GaugeVec + ConntrackBytesForward GaugeVec + ConntrackBytesReply GaugeVec + ConntrackTotalConnections GaugeVec ) func ToPrometheusType(metric interface{}) prometheus.Collector { diff --git a/pkg/plugin/conntrack/conntrack_linux.go b/pkg/plugin/conntrack/conntrack_linux.go index bf0a44c870..fb61f33f9b 100644 --- a/pkg/plugin/conntrack/conntrack_linux.go +++ b/pkg/plugin/conntrack/conntrack_linux.go @@ -24,7 +24,7 @@ import ( "go.uber.org/zap" ) -var conntrackMetricsEnabled = false // global variable to enable conntrack metrics +var conntrackMetricsEnabled = false // conntrack metrics global variable //go:generate go run github.com/cilium/ebpf/cmd/bpf2go@master -cflags "-g -O2 -Wall -D__TARGET_ARCH_${GOARCH} -Wall" -target ${GOARCH} -type ct_v4_key conntrack ./_cprog/conntrack.c -- -I../lib/_${GOARCH} -I../lib/common/libbpf/_src -I../lib/common/libbpf/_include/linux -I../lib/common/libbpf/_include/uapi/linux -I../lib/common/libbpf/_include/asm @@ -91,7 +91,7 @@ func GenerateDynamic(ctx context.Context, dynamicHeaderPath string, conntrackMet if err != nil { return errors.Wrap(err, "failed to write conntrack dynamic header") } - // set a global variable to enable conntrack metrics + // set a global variable if conntrackMetrics == 1 { conntrackMetricsEnabled = true } @@ -126,7 +126,7 @@ func (ct *Conntrack) Run(ctx context.Context) error { var keysToDelete []conntrackCtV4Key // metrics counters - var packetsCountForward, packetsCountReply uint32 + var packetsCountForward, packetsCountReply, totConnections uint32 var bytesCountForward, bytesCountReply uint64 iter := ct.ctMap.Iterate() @@ -148,8 +148,8 @@ func (ct *Conntrack) Run(ctx context.Context) error { // Add conntrack metrics. if conntrackMetricsEnabled { // Basic metrics, node-level - // for each ct_entry increment counters ctMeta := value.ConntrackMetadata + totConnections++ packetsCountForward += ctMeta.PacketsForwardCount packetsCountReply += ctMeta.PacketsReplyCount bytesCountForward += ctMeta.BytesForwardCount @@ -181,6 +181,7 @@ func (ct *Conntrack) Run(ctx context.Context) error { metrics.ConntrackBytesForward.WithLabelValues().Set(float64(bytesCountForward)) metrics.ConntrackPacketsReply.WithLabelValues().Set(float64(packetsCountReply)) metrics.ConntrackBytesReply.WithLabelValues().Set(float64(bytesCountReply)) + metrics.ConntrackTotalConnections.WithLabelValues().Set(float64(totConnections)) } // Delete the conntrack entries diff --git a/pkg/utils/metric_names.go b/pkg/utils/metric_names.go index bd5a3339e1..6d949dc8a0 100644 --- a/pkg/utils/metric_names.go +++ b/pkg/utils/metric_names.go @@ -40,6 +40,7 @@ const ( ConntrackPacketsReplyGaugeName = "conntrack_packets_reply" ConntrackBytesForwardGaugeName = "conntrack_bytes_forward" ConntrackBytesReplyGaugeName = "conntrack_bytes_reply" + ConntrackTotalConnectionsName = "conntrack_total_connections" ) // IsAdvancedMetric is a helper function to determine if a name is an advanced metric