diff --git a/pkg/metrics/metrics.go b/pkg/metrics/metrics.go index 77e3ac28d..413a8dd51 100644 --- a/pkg/metrics/metrics.go +++ b/pkg/metrics/metrics.go @@ -2,6 +2,7 @@ package metrics import ( "errors" + "strconv" "github.com/prometheus/client_golang/prometheus" "github.com/sirupsen/logrus" @@ -118,11 +119,21 @@ var ( ) errorsCounter = defineMetric( "errors_total", - "errors counter", + "Errors counter", TypeCounter, "component", "error", ) + flowEnrichmentCounterCounter = defineMetric( + "flows_enrichment_total", + "Statistics on flows enrichment", + TypeCounter, + "hasDNS", + "hasRTT", + "hasDrops", + "hasNetEvents", + "hasXlat", + ) ) func (def *MetricDefinition) mapLabels(labels []string) prometheus.Labels { @@ -154,6 +165,7 @@ type Metrics struct { NetworkEventsCounter *EvictionCounter BufferSizeGauge *BufferSizeGauge Errors *ErrorCounter + FlowEnrichmentCounter *FlowEnrichmentCounter } func NewMetrics(settings *Settings) *Metrics { @@ -168,6 +180,7 @@ func NewMetrics(settings *Settings) *Metrics { m.NetworkEventsCounter = &EvictionCounter{vec: m.NewCounterVec(&networkEvents)} m.BufferSizeGauge = &BufferSizeGauge{vec: m.NewGaugeVec(&bufferSize)} m.Errors = &ErrorCounter{vec: m.NewCounterVec(&errorsCounter)} + m.FlowEnrichmentCounter = &FlowEnrichmentCounter{vec: m.NewCounterVec(&flowEnrichmentCounterCounter)} return m } @@ -255,6 +268,20 @@ func (c *EvictionCounter) WithSource(source string) prometheus.Counter { return c.vec.WithLabelValues(source, "") } +type FlowEnrichmentCounter struct { + vec *prometheus.CounterVec +} + +func (c *FlowEnrichmentCounter) Increase(hasDNS, hasRTT, hasDrops, hasNetEvents, hasXlat bool) { + c.vec.WithLabelValues( + strconv.FormatBool(hasDNS), + strconv.FormatBool(hasRTT), + strconv.FormatBool(hasDrops), + strconv.FormatBool(hasNetEvents), + strconv.FormatBool(hasXlat), + ).Inc() +} + func (m *Metrics) CreateTimeSpendInLookupAndDelete() prometheus.Histogram { return m.NewHistogram(&lookupAndDeleteMapDurationSeconds, []float64{.001, .01, .1, 1, 10, 100, 1000, 10000}) } diff --git a/pkg/model/flow_content_test.go b/pkg/model/flow_content_test.go new file mode 100644 index 000000000..78a324ef1 --- /dev/null +++ b/pkg/model/flow_content_test.go @@ -0,0 +1,27 @@ +package model + +import ( + "testing" + + "github.com/netobserv/netobserv-ebpf-agent/pkg/ebpf" + "github.com/stretchr/testify/assert" +) + +func TestAccumulate(t *testing.T) { + baseMetrics := ebpf.BpfFlowMetrics{ + Bytes: 10, + } + additionalMetrics := []ebpf.BpfAdditionalMetrics{ + {DnsRecord: ebpf.BpfDnsRecordT{Id: 5}}, + {FlowRtt: 500}, + } + flowPayload := BpfFlowContent{BpfFlowMetrics: &ebpf.BpfFlowMetrics{}} + flowPayload.AccumulateBase(&baseMetrics) + for _, a := range additionalMetrics { + flowPayload.AccumulateAdditional(&a) + } + + assert.EqualValues(t, 10, flowPayload.Bytes) + assert.EqualValues(t, 5, flowPayload.AdditionalMetrics.DnsRecord.Id) + assert.EqualValues(t, 500, flowPayload.AdditionalMetrics.FlowRtt) +} diff --git a/pkg/tracer/tracer.go b/pkg/tracer/tracer.go index ec4d1efe2..7d4190dd8 100644 --- a/pkg/tracer/tracer.go +++ b/pkg/tracer/tracer.go @@ -892,10 +892,11 @@ func (m *FlowFetcher) LookupAndDeleteMap(met *metrics.Metrics) map[ebpf.BpfFlowI met.Errors.WithErrorName("flow-fetcher", "CannotDeleteAdditionalMetric").Inc() } } else { - for i := range additionalMetrics { - flowPayload.AccumulateAdditional(&additionalMetrics[i]) + for iMet := range additionalMetrics { + flowPayload.AccumulateAdditional(&additionalMetrics[iMet]) } } + m.increaseEnrichmentStats(met, &flowPayload) flows[id] = flowPayload } met.BufferSizeGauge.WithBufferName("hashmap-total").Set(float64(count)) @@ -905,6 +906,20 @@ func (m *FlowFetcher) LookupAndDeleteMap(met *metrics.Metrics) map[ebpf.BpfFlowI return flows } +func (m *FlowFetcher) increaseEnrichmentStats(met *metrics.Metrics, flow *model.BpfFlowContent) { + if flow.AdditionalMetrics != nil { + met.FlowEnrichmentCounter.Increase( + flow.AdditionalMetrics.DnsRecord.Id != 0, + flow.AdditionalMetrics.FlowRtt != 0, + flow.AdditionalMetrics.PktDrops.Packets != 0, + flow.AdditionalMetrics.NetworkEventsIdx != 0, + flow.AdditionalMetrics.TranslatedFlow.ZoneId != 0, + ) + } else { + met.FlowEnrichmentCounter.Increase(false, false, false, false, false) + } +} + // ReadGlobalCounter reads the global counter and updates drop flows counter metrics func (m *FlowFetcher) ReadGlobalCounter(met *metrics.Metrics) { var allCPUValue []uint32