Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix accesslog #814

Merged
merged 4 commits into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions pkg/controller/telemetry/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,6 @@

workloadLabels.reporter = "-"
serviceLabels.reporter = "-"
accesslog.direction = "-"
if data.direction == constants.INBOUND {
workloadLabels.reporter = "destination"
serviceLabels.reporter = "destination"
Expand All @@ -261,7 +260,7 @@
serviceLabels.reporter = "source"
accesslog.direction = "OUTBOUND"
}
if data.state == TCP_CLOSTED {
if data.state == TCP_CLOSTED && accesslog.sourceWorkload != "-" {

Check warning on line 263 in pkg/controller/telemetry/metric.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/telemetry/metric.go#L263

Added line #L263 was not covered by tests
OutputAccesslog(data, accesslog)
}
m.mutex.Lock()
Expand Down Expand Up @@ -350,6 +349,7 @@
trafficLabels.requestProtocol = "tcp"
trafficLabels.responseFlags = "-"
trafficLabels.connectionSecurityPolicy = "mutual_tls"

accesslog.destinationAddress = dstIp + ":" + fmt.Sprintf("%d", data.dstPort)
accesslog.sourceAddress = srcIp + ":" + fmt.Sprintf("%d", data.srcPort)

Expand All @@ -361,8 +361,7 @@
networkAddr.Address, _ = netip.AddrFromSlice(address)
workload := m.workloadCache.GetWorkloadByAddr(networkAddr)
if workload == nil {
log.Debugf("cannot find workload %s", networkAddr.Address.String())
return nil, ""
return nil, networkAddr.Address.String()

Check warning on line 364 in pkg/controller/telemetry/metric.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/telemetry/metric.go#L364

Added line #L364 was not covered by tests
LiZhenCheng9527 marked this conversation as resolved.
Show resolved Hide resolved
}
return workload, networkAddr.Address.String()
}
Expand Down Expand Up @@ -399,7 +398,16 @@

func buildServiceMetric(dstWorkload, srcWorkload *workloadapi.Workload, dstPort uint16) (serviceMetricLabels, logInfo) {
trafficLabels := serviceMetricLabels{}
accesslog := logInfo{}
accesslog := logInfo{
direction: "-",
sourceAddress: "-",
sourceWorkload: "-",
sourceNamespace: "-",
destinationAddress: "-",
destinationService: "-",
destinationWorkload: "-",
destinationNamespace: "-",
}

if dstWorkload != nil {
namespacedhost := ""
Expand Down Expand Up @@ -437,8 +445,12 @@
trafficLabels.destinationPrincipal = buildPrincipal(dstWorkload)

accesslog.destinationWorkload = dstWorkload.Name
accesslog.destinationNamespace = svcNamespace
accesslog.destinationService = svcHost
if svcNamespace != "" {
accesslog.destinationNamespace = svcNamespace
}
if svcHost != "" {
accesslog.destinationService = svcHost
}
}

if srcWorkload != nil {
Expand Down
23 changes: 18 additions & 5 deletions pkg/controller/telemetry/metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -584,10 +584,11 @@ func TestBuildServiceMetric(t *testing.T) {
data *requestMetric
}
tests := []struct {
name string
args args
want serviceMetricLabels
wantErr bool
name string
args args
want serviceMetricLabels
wantErr bool
wantLogInfo logInfo
}{
{
name: "normal capability test",
Expand All @@ -596,6 +597,7 @@ func TestBuildServiceMetric(t *testing.T) {
src: [4]uint32{521736970, 0, 0, 0},
dst: [4]uint32{383822016, 0, 0, 0},
dstPort: uint16(80),
srcPort: uint16(8000),
direction: uint32(2),
sentBytes: uint32(156),
receivedBytes: uint32(1024),
Expand Down Expand Up @@ -626,6 +628,16 @@ func TestBuildServiceMetric(t *testing.T) {
connectionSecurityPolicy: "mutual_tls",
},
wantErr: false,
wantLogInfo: logInfo{
direction: "-",
sourceAddress: "10.19.25.31:8000",
sourceWorkload: "kmesh",
sourceNamespace: "kmesh-system",
destinationAddress: "192.168.224.22:80",
destinationService: "kmesh.kmesh-system.svc.cluster.local",
destinationWorkload: "kmesh",
destinationNamespace: "kmesh-system",
},
},
}
for _, tt := range tests {
Expand All @@ -635,8 +647,9 @@ func TestBuildServiceMetric(t *testing.T) {
}
m.workloadCache.AddOrUpdateWorkload(dstWorkload)
m.workloadCache.AddOrUpdateWorkload(srcWorkload)
got, _ := m.buildServiceMetric(tt.args.data)
got, accesslog := m.buildServiceMetric(tt.args.data)
assert.Equal(t, tt.want, got)
assert.Equal(t, tt.wantLogInfo, accesslog)
})
}
}
Expand Down
Loading