Skip to content

Commit db3cf42

Browse files
committed
WIP
1 parent f2188dd commit db3cf42

3 files changed

Lines changed: 103 additions & 26 deletions

File tree

pkg/module/metrics/forward.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ func NewForwardCountMetrics(ctxOptions *api.MetricsContextOptions, fl *log.ZapLo
4040
return nil
4141
}
4242

43-
l := fl.Named("forward-metricsmodule")
44-
l.Info("Creating forward count metrics", zap.Any("options", ctxOptions))
43+
fl = fl.Named("forward-metricsmodule")
44+
fl.Info("Creating forward count metrics", zap.Any("options", ctxOptions))
4545
return &ForwardMetrics{
4646
baseMetricObject: newBaseMetricsObject(ctxOptions, fl, isLocalContext),
4747
enableStandalone: enableStandalone,

pkg/module/metrics/forward_test.go

Lines changed: 77 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -340,26 +340,24 @@ func TestNewForward(t *testing.T) {
340340
}
341341
}
342342

343-
func TestStandaloneForwardMetrics(t *testing.T) {
343+
func TestStandaloneForwardPacketsMetric(t *testing.T) {
344344
logger, err := log.SetupZapLogger(log.GetDefaultLogOpts())
345345
assert.NoError(t, err)
346346

347347
ctxOptions := &api.MetricsContextOptions{
348-
MetricName: "forward",
348+
MetricName: utils.ForwardPacketsGaugeName,
349349
SourceLabels: append([]string{utils.Direction}, DefaultCtxOptions()...),
350350
}
351351

352-
forward := NewForwardCountMetrics(ctxOptions, logger, LocalContext, true)
353-
forward.Init(ctxOptions.MetricName)
352+
forwardPackets := NewForwardCountMetrics(ctxOptions, logger, LocalContext, true)
353+
forwardPackets.Init(ctxOptions.MetricName)
354354

355355
originalGetHNS := GetHNSMetadata
356-
GetHNSMetadata = func(flow *flow.Flow) *utils.HNSStatsMetadata {
356+
GetHNSMetadata = func(_ *flow.Flow) *utils.HNSStatsMetadata {
357357
return &utils.HNSStatsMetadata{
358358
EndpointStats: &utils.EndpointStats{
359359
PacketsReceived: 42,
360360
PacketsSent: 99,
361-
BytesReceived: 42,
362-
BytesSent: 99,
363361
},
364362
}
365363
}
@@ -373,14 +371,14 @@ func TestStandaloneForwardMetrics(t *testing.T) {
373371
},
374372
}
375373

376-
forward.ProcessFlow(testFlow)
374+
forwardPackets.ProcessFlow(testFlow)
377375

378376
mfs, err := exporter.AdvancedRegistry.Gather()
379377
assert.NoError(t, err)
380378
var validMetricCount int
381379

382380
for _, mf := range mfs {
383-
if !strings.Contains(mf.GetName(), TotalCountName) && !strings.Contains(mf.GetName(), TotalBytesName) {
381+
if !strings.Contains(mf.GetName(), TotalCountName) {
384382
continue
385383
}
386384
t.Logf("Metric Family: %s", mf.GetName())
@@ -393,8 +391,8 @@ func TestStandaloneForwardMetrics(t *testing.T) {
393391
assert.Equal(t, "1.1.1.1", labelMap["ip"])
394392
assert.Equal(t, "default", labelMap["namespace"])
395393
assert.Equal(t, "test-pod", labelMap["podname"])
396-
assert.Equal(t, "", labelMap["workload_kind"])
397-
assert.Equal(t, "", labelMap["workload_name"])
394+
assert.Empty(t, labelMap["workload_kind"])
395+
assert.Empty(t, labelMap["workload_name"])
398396

399397
if labelMap["direction"] == "ingress" {
400398
assert.Equal(t, float64(42), m.GetGauge().GetValue())
@@ -406,5 +404,72 @@ func TestStandaloneForwardMetrics(t *testing.T) {
406404
}
407405
}
408406

409-
assert.Equal(t, 4, validMetricCount, "Expected 4 metric samples with correct labels and values")
407+
assert.Equal(t, 2, validMetricCount, "Expected 2 metric samples with correct labels and values")
408+
}
409+
410+
func TestStandaloneForwardBytesMetric(t *testing.T) {
411+
logger, err := log.SetupZapLogger(log.GetDefaultLogOpts())
412+
assert.NoError(t, err)
413+
414+
ctxOptions := &api.MetricsContextOptions{
415+
MetricName: utils.ForwardBytesGaugeName,
416+
SourceLabels: append([]string{utils.Direction}, DefaultCtxOptions()...),
417+
}
418+
419+
forwardBytes := NewForwardCountMetrics(ctxOptions, logger, LocalContext, true)
420+
forwardBytes.Init(ctxOptions.MetricName)
421+
422+
originalGetHNS := GetHNSMetadata
423+
GetHNSMetadata = func(_ *flow.Flow) *utils.HNSStatsMetadata {
424+
return &utils.HNSStatsMetadata{
425+
EndpointStats: &utils.EndpointStats{
426+
BytesReceived: 42,
427+
BytesSent: 99,
428+
},
429+
}
430+
}
431+
defer func() { GetHNSMetadata = originalGetHNS }()
432+
433+
testFlow := &flow.Flow{
434+
IP: &flow.IP{Source: "1.1.1.1"},
435+
Source: &flow.Endpoint{
436+
Namespace: "default",
437+
PodName: "test-pod",
438+
},
439+
}
440+
441+
forwardBytes.ProcessFlow(testFlow)
442+
443+
mfs, err := exporter.AdvancedRegistry.Gather()
444+
assert.NoError(t, err)
445+
var validMetricCount int
446+
447+
for _, mf := range mfs {
448+
if !strings.Contains(mf.GetName(), TotalBytesName) {
449+
continue
450+
}
451+
t.Logf("Metric Family: %s", mf.GetName())
452+
453+
for _, m := range mf.GetMetric() {
454+
labelMap := map[string]string{}
455+
for _, label := range m.GetLabel() {
456+
labelMap[label.GetName()] = label.GetValue()
457+
}
458+
assert.Equal(t, "1.1.1.1", labelMap["ip"])
459+
assert.Equal(t, "default", labelMap["namespace"])
460+
assert.Equal(t, "test-pod", labelMap["podname"])
461+
assert.Empty(t, labelMap["workload_kind"])
462+
assert.Empty(t, labelMap["workload_name"])
463+
464+
if labelMap["direction"] == "ingress" {
465+
assert.Equal(t, float64(42), m.GetGauge().GetValue())
466+
validMetricCount++
467+
} else {
468+
assert.Equal(t, float64(99), m.GetGauge().GetValue())
469+
validMetricCount++
470+
}
471+
}
472+
}
473+
474+
assert.Equal(t, 2, validMetricCount, "Expected 2 metric samples with correct labels and values")
410475
}

pkg/module/metrics/metrics_module_linux_test.go

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,9 @@ func TestModule_Reconcile(t *testing.T) {
371371
ctrl := gomock.NewController(t)
372372
defer ctrl.Finish()
373373
l := log.Logger().Named("test")
374+
cfg := kcfg.Config{
375+
EnableStandalone: false,
376+
}
374377

375378
testDropMetric := &DropCountMetrics{
376379
baseMetricObject: baseMetricObject{
@@ -381,6 +384,7 @@ func TestModule_Reconcile(t *testing.T) {
381384
DestinationLabels: []string{"pod"},
382385
AdditionalLabels: []string{"namespace"},
383386
},
387+
l: l,
384388
},
385389
}
386390
testDropMetric.Init("drop_count")
@@ -391,6 +395,7 @@ func TestModule_Reconcile(t *testing.T) {
391395
MetricName: "drop_bytes",
392396
SourceLabels: []string{"ip"},
393397
},
398+
l: l,
394399
},
395400
}
396401
testDropMetricBytes.Init("drop_bytes")
@@ -403,6 +408,7 @@ func TestModule_Reconcile(t *testing.T) {
403408
DestinationLabels: []string{"pod"},
404409
AdditionalLabels: []string{"namespace"},
405410
},
411+
l: l,
406412
},
407413
}
408414
testForwardMetric.Init("forward_count")
@@ -413,6 +419,7 @@ func TestModule_Reconcile(t *testing.T) {
413419
MetricName: "forward_bytes",
414420
SourceLabels: []string{"ip"},
415421
},
422+
l: l,
416423
},
417424
}
418425
testForwardMetricBytes.Init("forward_bytes")
@@ -437,10 +444,11 @@ func TestModule_Reconcile(t *testing.T) {
437444
},
438445
},
439446
m: &Module{
440-
RWMutex: &sync.RWMutex{},
441-
registry: make(map[string]AdvMetricsInterface),
442-
l: l,
443-
moduleCtx: context.Background(),
447+
RWMutex: &sync.RWMutex{},
448+
registry: make(map[string]AdvMetricsInterface),
449+
l: l,
450+
moduleCtx: context.Background(),
451+
daemonConfig: &cfg,
444452
},
445453
expectErr: false,
446454
},
@@ -468,8 +476,9 @@ func TestModule_Reconcile(t *testing.T) {
468476
"drop_count": testDropMetric,
469477
"forward_count": testForwardMetric,
470478
},
471-
moduleCtx: context.Background(),
472-
l: l,
479+
moduleCtx: context.Background(),
480+
l: l,
481+
daemonConfig: &cfg,
473482
},
474483
expectErr: false,
475484
},
@@ -507,8 +516,9 @@ func TestModule_Reconcile(t *testing.T) {
507516
"forward_count": testForwardMetric,
508517
"forward_bytes": testForwardMetricBytes,
509518
},
510-
moduleCtx: context.Background(),
511-
l: l,
519+
moduleCtx: context.Background(),
520+
l: l,
521+
daemonConfig: &cfg,
512522
},
513523
expectErr: false,
514524
},
@@ -546,8 +556,9 @@ func TestModule_Reconcile(t *testing.T) {
546556
"forward_count": testForwardMetric,
547557
"forward_bytes": testForwardMetricBytes,
548558
},
549-
moduleCtx: context.Background(),
550-
l: l,
559+
moduleCtx: context.Background(),
560+
l: l,
561+
daemonConfig: &cfg,
551562
},
552563
expectErr: true,
553564
},
@@ -568,8 +579,9 @@ func TestModule_Reconcile(t *testing.T) {
568579
registry: map[string]AdvMetricsInterface{
569580
"drop_count": testDropMetric,
570581
},
571-
moduleCtx: context.Background(),
572-
l: l,
582+
moduleCtx: context.Background(),
583+
l: l,
584+
daemonConfig: &cfg,
573585
currentSpec: &api.MetricsSpec{
574586
ContextOptions: []api.MetricsContextOptions{
575587
{

0 commit comments

Comments
 (0)