Skip to content

Commit 05f60a1

Browse files
authored
Merge pull request #814 from LiZhenCheng9527/fix-accesslog
Fix accesslog
2 parents 7c2bdbf + 36825ab commit 05f60a1

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

pkg/controller/telemetry/metric.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,6 @@ func (m *MetricController) Run(ctx context.Context, mapOfTcpInfo *ebpf.Map) {
250250

251251
workloadLabels.reporter = "-"
252252
serviceLabels.reporter = "-"
253-
accesslog.direction = "-"
254253
if data.direction == constants.INBOUND {
255254
workloadLabels.reporter = "destination"
256255
serviceLabels.reporter = "destination"
@@ -261,7 +260,7 @@ func (m *MetricController) Run(ctx context.Context, mapOfTcpInfo *ebpf.Map) {
261260
serviceLabels.reporter = "source"
262261
accesslog.direction = "OUTBOUND"
263262
}
264-
if data.state == TCP_CLOSTED {
263+
if data.state == TCP_CLOSTED && accesslog.sourceWorkload != "-" {
265264
OutputAccesslog(data, accesslog)
266265
}
267266
m.mutex.Lock()
@@ -350,6 +349,7 @@ func (m *MetricController) buildServiceMetric(data *requestMetric) (serviceMetri
350349
trafficLabels.requestProtocol = "tcp"
351350
trafficLabels.responseFlags = "-"
352351
trafficLabels.connectionSecurityPolicy = "mutual_tls"
352+
353353
accesslog.destinationAddress = dstIp + ":" + fmt.Sprintf("%d", data.dstPort)
354354
accesslog.sourceAddress = srcIp + ":" + fmt.Sprintf("%d", data.srcPort)
355355

@@ -361,8 +361,7 @@ func (m *MetricController) getWorkloadByAddress(address []byte) (*workloadapi.Wo
361361
networkAddr.Address, _ = netip.AddrFromSlice(address)
362362
workload := m.workloadCache.GetWorkloadByAddr(networkAddr)
363363
if workload == nil {
364-
log.Debugf("cannot find workload %s", networkAddr.Address.String())
365-
return nil, ""
364+
return nil, networkAddr.Address.String()
366365
}
367366
return workload, networkAddr.Address.String()
368367
}
@@ -399,7 +398,16 @@ func buildWorkloadMetric(dstWorkload, srcWorkload *workloadapi.Workload) workloa
399398

400399
func buildServiceMetric(dstWorkload, srcWorkload *workloadapi.Workload, dstPort uint16) (serviceMetricLabels, logInfo) {
401400
trafficLabels := serviceMetricLabels{}
402-
accesslog := logInfo{}
401+
accesslog := logInfo{
402+
direction: "-",
403+
sourceAddress: "-",
404+
sourceWorkload: "-",
405+
sourceNamespace: "-",
406+
destinationAddress: "-",
407+
destinationService: "-",
408+
destinationWorkload: "-",
409+
destinationNamespace: "-",
410+
}
403411

404412
if dstWorkload != nil {
405413
namespacedhost := ""
@@ -437,8 +445,12 @@ func buildServiceMetric(dstWorkload, srcWorkload *workloadapi.Workload, dstPort
437445
trafficLabels.destinationPrincipal = buildPrincipal(dstWorkload)
438446

439447
accesslog.destinationWorkload = dstWorkload.Name
440-
accesslog.destinationNamespace = svcNamespace
441-
accesslog.destinationService = svcHost
448+
if svcNamespace != "" {
449+
accesslog.destinationNamespace = svcNamespace
450+
}
451+
if svcHost != "" {
452+
accesslog.destinationService = svcHost
453+
}
442454
}
443455

444456
if srcWorkload != nil {

pkg/controller/telemetry/metric_test.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -584,10 +584,11 @@ func TestBuildServiceMetric(t *testing.T) {
584584
data *requestMetric
585585
}
586586
tests := []struct {
587-
name string
588-
args args
589-
want serviceMetricLabels
590-
wantErr bool
587+
name string
588+
args args
589+
want serviceMetricLabels
590+
wantErr bool
591+
wantLogInfo logInfo
591592
}{
592593
{
593594
name: "normal capability test",
@@ -596,6 +597,7 @@ func TestBuildServiceMetric(t *testing.T) {
596597
src: [4]uint32{521736970, 0, 0, 0},
597598
dst: [4]uint32{383822016, 0, 0, 0},
598599
dstPort: uint16(80),
600+
srcPort: uint16(8000),
599601
direction: uint32(2),
600602
sentBytes: uint32(156),
601603
receivedBytes: uint32(1024),
@@ -626,6 +628,16 @@ func TestBuildServiceMetric(t *testing.T) {
626628
connectionSecurityPolicy: "mutual_tls",
627629
},
628630
wantErr: false,
631+
wantLogInfo: logInfo{
632+
direction: "-",
633+
sourceAddress: "10.19.25.31:8000",
634+
sourceWorkload: "kmesh",
635+
sourceNamespace: "kmesh-system",
636+
destinationAddress: "192.168.224.22:80",
637+
destinationService: "kmesh.kmesh-system.svc.cluster.local",
638+
destinationWorkload: "kmesh",
639+
destinationNamespace: "kmesh-system",
640+
},
629641
},
630642
}
631643
for _, tt := range tests {
@@ -635,8 +647,9 @@ func TestBuildServiceMetric(t *testing.T) {
635647
}
636648
m.workloadCache.AddOrUpdateWorkload(dstWorkload)
637649
m.workloadCache.AddOrUpdateWorkload(srcWorkload)
638-
got, _ := m.buildServiceMetric(tt.args.data)
650+
got, accesslog := m.buildServiceMetric(tt.args.data)
639651
assert.Equal(t, tt.want, got)
652+
assert.Equal(t, tt.wantLogInfo, accesslog)
640653
})
641654
}
642655
}

0 commit comments

Comments
 (0)