From 57784e318449e711ffc994ee88609397086330a4 Mon Sep 17 00:00:00 2001 From: ArthurChiao Date: Wed, 18 Nov 2020 15:59:25 +0800 Subject: [PATCH] metrics: add cilium_datapath_nat_gc_entries Signed-off-by: ArthurChiao --- pkg/maps/ctmap/ctmap.go | 11 ++++++++++- pkg/maps/ctmap/metrics.go | 13 ++++++++++++- pkg/metrics/metrics.go | 13 +++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/pkg/maps/ctmap/ctmap.go b/pkg/maps/ctmap/ctmap.go index ae20fe1ec23f..4973efd412f6 100644 --- a/pkg/maps/ctmap/ctmap.go +++ b/pkg/maps/ctmap/ctmap.go @@ -91,6 +91,9 @@ const ( metricsAlive = "alive" metricsDeleted = "deleted" + + metricsIngress = "ingress" + metricsEgress = "egress" ) type action int @@ -555,7 +558,13 @@ func PurgeOrphanNATEntries(ctMapTCP, ctMapAny *Map) *NatGCStats { return nil } - stats := newNatGCStats(natMap) + family := gcFamilyIPv4 + if ctMapTCP.mapType.isIPv6() { + family = gcFamilyIPv6 + } + stats := newNatGCStats(natMap, family) + defer stats.finish() + cb := func(key bpf.MapKey, value bpf.MapValue) { natKey := key.(nat.NatKey) natVal := value.(nat.NatEntry) diff --git a/pkg/maps/ctmap/metrics.go b/pkg/maps/ctmap/metrics.go index 0c61801b65cf..5102854fe9cc 100644 --- a/pkg/maps/ctmap/metrics.go +++ b/pkg/maps/ctmap/metrics.go @@ -126,6 +126,9 @@ func (s *gcStats) finish() { type NatGCStats struct { *bpf.DumpStats + // family is the address family + Family gcFamily + IngressAlive uint32 IngressDeleted uint32 EgressDeleted uint32 @@ -133,8 +136,16 @@ type NatGCStats struct { // to correctly count EgressAlive, so skip it } -func newNatGCStats(m NatMap) NatGCStats { +func newNatGCStats(m NatMap, family gcFamily) NatGCStats { return NatGCStats{ DumpStats: m.DumpStats(), + Family: family, } } + +func (s *NatGCStats) finish() { + family := s.Family.String() + metrics.NatGCSize.WithLabelValues(family, metricsIngress, metricsAlive).Set(float64(s.IngressAlive)) + metrics.NatGCSize.WithLabelValues(family, metricsIngress, metricsDeleted).Set(float64(s.IngressDeleted)) + metrics.NatGCSize.WithLabelValues(family, metricsEgress, metricsDeleted).Set(float64(s.EgressDeleted)) +} diff --git a/pkg/metrics/metrics.go b/pkg/metrics/metrics.go index c8c5666fa58a..d326496e3867 100644 --- a/pkg/metrics/metrics.go +++ b/pkg/metrics/metrics.go @@ -314,6 +314,9 @@ var ( // ConntrackGCSize the number of entries in the conntrack table ConntrackGCSize = NoOpGaugeVec + // NatGCSize the number of entries in the nat table + NatGCSize = NoOpGaugeVec + // ConntrackGCDuration the duration of the conntrack GC process in milliseconds. ConntrackGCDuration = NoOpObserverVec @@ -898,6 +901,16 @@ func CreateConfiguration(metricsEnabled []string) (Configuration, []prometheus.C collectors = append(collectors, ConntrackGCSize) c.ConntrackGCSizeEnabled = true + NatGCSize = prometheus.NewGaugeVec(prometheus.GaugeOpts{ + Namespace: Namespace, + Subsystem: SubsystemDatapath, + Name: "nat_gc_entries", + Help: "The number of alive and deleted nat entries at the end " + + "of a garbage collector run labeled by datapath family.", + }, []string{LabelDatapathFamily, LabelDirection, LabelStatus}) + + collectors = append(collectors, NatGCSize) + case Namespace + "_" + SubsystemDatapath + "_conntrack_gc_duration_seconds": ConntrackGCDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{ Namespace: Namespace,