Skip to content

Commit

Permalink
Add enrichment stats
Browse files Browse the repository at this point in the history
New metric: netobserv_agent_flows_enrichment_total
Example of query:
sum(rate(netobserv_agent_flows_enrichment_total[1m])) by (hasDNS, hasRTT, hasDrops, hasNetEvents)
  • Loading branch information
jotak committed Dec 19, 2024
1 parent bb70b5a commit 74270b0
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 3 deletions.
29 changes: 28 additions & 1 deletion pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package metrics

import (
"errors"
"strconv"

"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -154,6 +165,7 @@ type Metrics struct {
NetworkEventsCounter *EvictionCounter
BufferSizeGauge *BufferSizeGauge
Errors *ErrorCounter
FlowEnrichmentCounter *FlowEnrichmentCounter
}

func NewMetrics(settings *Settings) *Metrics {
Expand All @@ -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
}

Expand Down Expand Up @@ -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})
}
Expand Down
27 changes: 27 additions & 0 deletions pkg/model/flow_content_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
19 changes: 17 additions & 2 deletions pkg/tracer/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
Expand Down

0 comments on commit 74270b0

Please sign in to comment.