@@ -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}
0 commit comments