Skip to content

Commit e56e0ac

Browse files
authored
Merge pull request #1377 from yp969803/issue1370
refactored updateConnectionMetricCache to report connection-metrics correctly
2 parents 4f0ae8f + 663d8a7 commit e56e0ac

File tree

2 files changed

+9
-50
lines changed

2 files changed

+9
-50
lines changed

pkg/controller/telemetry/metric.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ func (m *MetricController) Run(ctx context.Context, mapOfTcpInfo *ebpf.Map) {
521521
}
522522
m.updateServiceMetricCache(reqMetric, serviceLabels, tcpConns[reqMetric.conSrcDstInfo])
523523
if m.EnableConnectionMetric.Load() && reqMetric.duration > LONG_CONN_METRIC_THRESHOLD {
524-
m.updateConnectionMetricCache(reqMetric, tcpConns[reqMetric.conSrcDstInfo], connectionLabels)
524+
m.updateConnectionMetricCache(reqMetric, connectionLabels)
525525
}
526526
m.mutex.Unlock()
527527

@@ -907,7 +907,7 @@ func (m *MetricController) updateServiceMetricCache(reqMetric requestMetric, lab
907907
}
908908
}
909909

910-
func (m *MetricController) updateConnectionMetricCache(reqMetric requestMetric, connMetric connMetric, labels connectionMetricLabels) {
910+
func (m *MetricController) updateConnectionMetricCache(reqMetric requestMetric, labels connectionMetricLabels) {
911911
v, ok := m.connectionMetricCache[labels]
912912
if ok {
913913
v.ConnSentBytes = v.ConnSentBytes + float64(reqMetric.sentBytes)
@@ -916,10 +916,10 @@ func (m *MetricController) updateConnectionMetricCache(reqMetric requestMetric,
916916
v.ConnTotalRetrans = v.ConnTotalRetrans + float64(reqMetric.totalRetrans)
917917
} else {
918918
newConnectionMetricInfo := connectionMetricInfo{}
919-
newConnectionMetricInfo.ConnSentBytes = float64(connMetric.sentBytes)
920-
newConnectionMetricInfo.ConnReceivedBytes = float64(connMetric.receivedBytes)
921-
newConnectionMetricInfo.ConnPacketLost = float64(connMetric.packetLost)
922-
newConnectionMetricInfo.ConnTotalRetrans = float64(connMetric.totalRetrans)
919+
newConnectionMetricInfo.ConnSentBytes = float64(reqMetric.sentBytes)
920+
newConnectionMetricInfo.ConnReceivedBytes = float64(reqMetric.receivedBytes)
921+
newConnectionMetricInfo.ConnPacketLost = float64(reqMetric.packetLost)
922+
newConnectionMetricInfo.ConnTotalRetrans = float64(reqMetric.totalRetrans)
923923
m.connectionMetricCache[labels] = &newConnectionMetricInfo
924924
}
925925
if reqMetric.state == TCP_CLOSED {

pkg/controller/telemetry/metric_test.go

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -491,9 +491,8 @@ func TestBuildServiceMetricsToPrometheus(t *testing.T) {
491491

492492
func TestBuildConnectionMetricsToPrometheus(t *testing.T) {
493493
type args struct {
494-
data requestMetric
495-
labels connectionMetricLabels
496-
tcpConns map[connectionSrcDst]connMetric
494+
data requestMetric
495+
labels connectionMetricLabels
497496
}
498497
tests := []struct {
499498
id int32
@@ -542,26 +541,6 @@ func TestBuildConnectionMetricsToPrometheus(t *testing.T) {
542541
responseFlags: "-",
543542
connectionSecurityPolicy: "mutual_tls",
544543
},
545-
tcpConns: map[connectionSrcDst]connMetric{
546-
{
547-
src: [4]uint32{183763210, 0, 0, 0},
548-
dst: [4]uint32{183762951, 0, 0, 0},
549-
direction: constants.INBOUND,
550-
}: {
551-
sentBytes: 0x0000003,
552-
receivedBytes: 0x0000004,
553-
packetLost: 0x0000001,
554-
totalRetrans: 0x0000002,
555-
totalReports: 1,
556-
},
557-
{
558-
src: [4]uint32{183763210, 0, 0, 0},
559-
dst: [4]uint32{183762951, 0, 0, 0},
560-
direction: constants.OUTBOUND,
561-
}: {
562-
totalReports: 3,
563-
},
564-
},
565544
},
566545
want: []float64{
567546
3,
@@ -611,26 +590,6 @@ func TestBuildConnectionMetricsToPrometheus(t *testing.T) {
611590
responseFlags: "-",
612591
connectionSecurityPolicy: "mutual_tls",
613592
},
614-
tcpConns: map[connectionSrcDst]connMetric{
615-
{
616-
src: [4]uint32{183763210, 0, 0, 0},
617-
dst: [4]uint32{183762951, 0, 0, 0},
618-
direction: constants.INBOUND,
619-
}: {
620-
sentBytes: 0x0000003,
621-
receivedBytes: 0x0000004,
622-
packetLost: 0x0000001,
623-
totalRetrans: 0x0000002,
624-
totalReports: 1,
625-
},
626-
{
627-
src: [4]uint32{183763210, 0, 0, 0},
628-
dst: [4]uint32{183762951, 0, 0, 0},
629-
direction: constants.OUTBOUND,
630-
}: {
631-
totalReports: 3,
632-
},
633-
},
634593
},
635594
want: []float64{
636595
4,
@@ -681,7 +640,7 @@ func TestBuildConnectionMetricsToPrometheus(t *testing.T) {
681640
}
682641

683642
deleteConnection = []*connectionMetricLabels{}
684-
m.updateConnectionMetricCache(tt.args.data, tt.args.tcpConns[tt.args.data.conSrcDstInfo], tt.args.labels)
643+
m.updateConnectionMetricCache(tt.args.data, tt.args.labels)
685644
assert.Equal(t, m.connectionMetricCache[tt.args.labels].ConnSentBytes, tt.want[0])
686645
assert.Equal(t, m.connectionMetricCache[tt.args.labels].ConnReceivedBytes, tt.want[1])
687646
assert.Equal(t, m.connectionMetricCache[tt.args.labels].ConnPacketLost, tt.want[2])

0 commit comments

Comments
 (0)