Skip to content

Commit

Permalink
feat(conntrack-metrics): add conntrack_total_connections metric and f…
Browse files Browse the repository at this point in the history
…ix comments
  • Loading branch information
SRodi committed Jan 21, 2025
1 parent 191464d commit 3fa4154
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 13 deletions.
6 changes: 6 additions & 0 deletions pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,12 @@ func InitializeMetrics() {
ConntrackBytesReplyDescription,
)

ConntrackTotalConnections = exporter.CreatePrometheusGaugeVecForMetric(
exporter.DefaultRegistry,
utils.ConntrackTotalConnectionsName,
ConntrackTotalConnectionsDescription,
)

isInitialized = true
metricsLogger.Info("Metrics initialized")
}
Expand Down
18 changes: 10 additions & 8 deletions pkg/metrics/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
11 changes: 6 additions & 5 deletions pkg/plugin/conntrack/conntrack_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -126,8 +126,8 @@ func (ct *Conntrack) Run(ctx context.Context) error {
var keysToDelete []conntrackCtV4Key

// metrics counters
var packetsCountForward, packetsCountReply uint32
var bytesCountForward, bytesCountReply uint64
var packetsCountForward, packetsCountReply, totConnections uint32 = 0, 0, 0
var bytesCountForward, bytesCountReply uint64 = 0, 0

iter := ct.ctMap.Iterate()
for iter.Next(&key, &value) {
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions pkg/utils/metric_names.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3fa4154

Please sign in to comment.