Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(conntrack-metrics): legacy control plane basic mode #1253

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,36 @@ func InitializeMetrics() {
utils.InterfaceName,
)

ConntrackPacketsForward = exporter.CreatePrometheusGaugeVecForMetric(
exporter.DefaultRegistry,
utils.ConntrackPacketsForwardGaugeName,
ConntrackPacketForwardDescription,
)

ConntrackPacketsReply = exporter.CreatePrometheusGaugeVecForMetric(
exporter.DefaultRegistry,
utils.ConntrackPacketsReplyGaugeName,
ConntrackPacketReplyDescription,
)

ConntrackBytesForward = exporter.CreatePrometheusGaugeVecForMetric(
exporter.DefaultRegistry,
utils.ConntrackBytesForwardGaugeName,
ConntrackBytesForwardDescription,
)

ConntrackBytesReply = exporter.CreatePrometheusGaugeVecForMetric(
exporter.DefaultRegistry,
utils.ConntrackBytesReplyGaugeName,
ConntrackBytesReplyDescription,
)

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

isInitialized = true
metricsLogger.Info("Metrics initialized")
}
Expand Down
14 changes: 14 additions & 0 deletions pkg/metrics/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ const (
// Control plane metrics
pluginManagerFailedToReconcileCounterDescription = "Number of times the plugin manager failed to reconcile the plugins"
lostEventsCounterDescription = "Number of events lost in control plane"

// Conntrack metrics
ConntrackPacketForwardDescription = "Number of forward packets"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

forward or forwarded?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should use ‘forward’ since in this context we refer to ‘direction’ more than ‘action’. We also want to be consistent with the actual metrics implementation in BPF, which uses 'forward' and 'reply'.

packets_*_count indicates the number of packets sent and received in the forward and reply direction.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be better, then, to say 'Number of packets marked "forward"'.

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 @@ -89,6 +96,13 @@ var (

InfinibandStatsGauge GaugeVec
InfinibandStatusParamsGauge GaugeVec

// Conntrack
ConntrackPacketsForward GaugeVec
ConntrackPacketsReply GaugeVec
ConntrackBytesForward GaugeVec
ConntrackBytesReply GaugeVec
ConntrackTotalConnections GaugeVec
)

func ToPrometheusType(metric interface{}) prometheus.Collector {
Expand Down
33 changes: 33 additions & 0 deletions pkg/plugin/conntrack/conntrack_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@ import (
"github.com/microsoft/retina/internal/ktime"
"github.com/microsoft/retina/pkg/loader"
"github.com/microsoft/retina/pkg/log"
"github.com/microsoft/retina/pkg/metrics"
plugincommon "github.com/microsoft/retina/pkg/plugin/common"
_ "github.com/microsoft/retina/pkg/plugin/conntrack/_cprog" // nolint // This is needed so cprog is included when vendoring
"github.com/microsoft/retina/pkg/utils"
"github.com/pkg/errors"
"go.uber.org/zap"
)

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

// Init initializes the conntrack eBPF map in the kernel for the first time.
Expand Down Expand Up @@ -88,6 +91,10 @@ 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
if conntrackMetrics == 1 {
conntrackMetricsEnabled = true
}
return nil
}

Expand Down Expand Up @@ -118,6 +125,10 @@ func (ct *Conntrack) Run(ctx context.Context) error {
// List of keys to be deleted
var keysToDelete []conntrackCtV4Key

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

iter := ct.ctMap.Iterate()
for iter.Next(&key, &value) {
noOfCtEntries++
Expand All @@ -133,6 +144,18 @@ func (ct *Conntrack) Run(ctx context.Context) error {
dstIP := utils.Int2ip(key.DstIp).To4()
sourcePortShort := uint32(utils.HostToNetShort(key.SrcPort))
destinationPortShort := uint32(utils.HostToNetShort(key.DstPort))

// Add conntrack metrics.
if conntrackMetricsEnabled {
// Basic metrics, node-level
ctMeta := value.ConntrackMetadata
totConnections++
packetsCountForward += ctMeta.PacketsForwardCount
packetsCountReply += ctMeta.PacketsReplyCount
bytesCountForward += ctMeta.BytesForwardCount
bytesCountReply += ctMeta.BytesReplyCount
}

ct.l.Debug("conntrack entry",
zap.String("src_ip", srcIP.String()),
zap.Uint32("src_port", sourcePortShort),
Expand All @@ -151,6 +174,16 @@ func (ct *Conntrack) Run(ctx context.Context) error {
if err := iter.Err(); err != nil {
ct.l.Error("Iterate failed", zap.Error(err))
}

// create metrics
if conntrackMetricsEnabled {
metrics.ConntrackPacketsForward.WithLabelValues().Set(float64(packetsCountForward))
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
for _, key := range keysToDelete {
if err := ct.ctMap.Delete(key); err != nil {
Expand Down
7 changes: 7 additions & 0 deletions pkg/utils/metric_names.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ const (
// Common Gauges across os distributions
NodeConnectivityStatusName = "node_connectivity_status"
NodeConnectivityLatencySecondsName = "node_connectivity_latency_seconds"

// Conntrack
ConntrackPacketsForwardGaugeName = "conntrack_packets_forward"
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
Loading