Skip to content

Commit 57c7542

Browse files
authored
refactor: optimize logs (#268)
1 parent 8b17632 commit 57c7542

File tree

5 files changed

+244
-131
lines changed

5 files changed

+244
-131
lines changed

agent/agent.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,14 @@ func SetupAgent(options ac.AgentOptions) {
9898

9999
var _bf loader.BPF
100100
go func(_bf *loader.BPF) {
101+
defer wg.Done()
101102
options.LoadPorgressChannel <- "🍩 Kyanos starting..."
102103
kernelVersion := compatible.GetCurrentKernelVersion()
103104
options.Kv = &kernelVersion
104105
var err error
105106
defer func() {
106107
if err != nil {
107-
common.AgentLog.Error("Failed to load BPF programs: ", err)
108+
common.AgentLog.Errorf("Failed to load BPF programs: %+v", errors.Unwrap(errors.Unwrap(err)))
108109
_bf.Err = err
109110
options.LoadPorgressChannel <- "❌ Kyanos start failed"
110111
options.LoadPorgressChannel <- "quit"
@@ -151,7 +152,6 @@ func SetupAgent(options ac.AgentOptions) {
151152
time.Sleep(500 * time.Millisecond)
152153
options.LoadPorgressChannel <- "quit"
153154
}
154-
defer wg.Done()
155155
}(&_bf)
156156
defer func() {
157157
_bf.Close()

agent/agent_test.go

+18-18
Original file line numberDiff line numberDiff line change
@@ -586,25 +586,25 @@ func TestSslEventsCanRelatedToKernEvents(t *testing.T) {
586586
bpf.AttachSyscallWriteExit,
587587
bpf.AttachKProbeSecuritySocketSendmsgEntry,
588588
bpf.AttachKProbeSecuritySocketRecvmsgEntry,
589-
func() link.Link {
589+
func() (link.Link, error) {
590590
return ApplyKernelVersionFunctions(t, bpf.AgentStepTDEV_IN)
591591
},
592-
func() link.Link {
592+
func() (link.Link, error) {
593593
return ApplyKernelVersionFunctions(t, bpf.AgentStepTUSER_COPY)
594594
},
595-
func() link.Link {
595+
func() (link.Link, error) {
596596
return ApplyKernelVersionFunctions(t, bpf.AgentStepTIP_IN)
597597
},
598-
func() link.Link {
598+
func() (link.Link, error) {
599599
return ApplyKernelVersionFunctions(t, bpf.AgentStepTTCP_IN)
600600
},
601-
func() link.Link {
601+
func() (link.Link, error) {
602602
return ApplyKernelVersionFunctions(t, bpf.AgentStepTIP_OUT)
603603
},
604-
func() link.Link {
604+
func() (link.Link, error) {
605605
return ApplyKernelVersionFunctions(t, bpf.AgentStepTDEV_OUT)
606606
},
607-
func() link.Link {
607+
func() (link.Link, error) {
608608
return ApplyKernelVersionFunctions(t, bpf.AgentStepTQDISC_OUT)
609609
},
610610
},
@@ -964,7 +964,7 @@ func TestIpXmit(t *testing.T) {
964964
bpf.AttachSyscallWriteEntry,
965965
bpf.AttachSyscallWriteExit,
966966
bpf.AttachKProbeSecuritySocketSendmsgEntry,
967-
func() link.Link {
967+
func() (link.Link, error) {
968968
return ApplyKernelVersionFunctions(t, bpf.AgentStepTIP_OUT)
969969
},
970970
},
@@ -994,10 +994,10 @@ func TestDevQueueXmit(t *testing.T) {
994994
bpf.AttachSyscallWriteEntry,
995995
bpf.AttachSyscallWriteExit,
996996
bpf.AttachKProbeSecuritySocketSendmsgEntry,
997-
func() link.Link {
997+
func() (link.Link, error) {
998998
return ApplyKernelVersionFunctions(t, bpf.AgentStepTIP_OUT)
999999
},
1000-
func() link.Link {
1000+
func() (link.Link, error) {
10011001
return ApplyKernelVersionFunctions(t, bpf.AgentStepTQDISC_OUT)
10021002
},
10031003
}, "GET DevQueueXmit\n", Write, Read,
@@ -1029,10 +1029,10 @@ func TestDevHardStartXmit(t *testing.T) {
10291029
bpf.AttachSyscallWriteEntry,
10301030
bpf.AttachSyscallWriteExit,
10311031
bpf.AttachKProbeSecuritySocketSendmsgEntry,
1032-
func() link.Link {
1032+
func() (link.Link, error) {
10331033
return ApplyKernelVersionFunctions(t, bpf.AgentStepTIP_OUT)
10341034
},
1035-
func() link.Link {
1035+
func() (link.Link, error) {
10361036
return ApplyKernelVersionFunctions(t, bpf.AgentStepTDEV_OUT)
10371037
},
10381038
}, "GET DevHardStartXmit\n", Write, Read,
@@ -1110,10 +1110,10 @@ func TestIpRcvCore(t *testing.T) {
11101110
bpf.AttachSyscallWriteExit,
11111111
bpf.AttachKProbeSecuritySocketSendmsgEntry,
11121112
bpf.AttachKProbeSecuritySocketRecvmsgEntry,
1113-
func() link.Link {
1113+
func() (link.Link, error) {
11141114
return ApplyKernelVersionFunctions(t, bpf.AgentStepTDEV_IN)
11151115
},
1116-
func() link.Link {
1116+
func() (link.Link, error) {
11171117
return ApplyKernelVersionFunctions(t, bpf.AgentStepTIP_IN)
11181118
},
11191119
},
@@ -1150,10 +1150,10 @@ func TestTcpV4DoRcv(t *testing.T) {
11501150
bpf.AttachSyscallWriteExit,
11511151
bpf.AttachKProbeSecuritySocketSendmsgEntry,
11521152
bpf.AttachKProbeSecuritySocketRecvmsgEntry,
1153-
func() link.Link {
1153+
func() (link.Link, error) {
11541154
return ApplyKernelVersionFunctions(t, bpf.AgentStepTDEV_IN)
11551155
},
1156-
func() link.Link {
1156+
func() (link.Link, error) {
11571157
return ApplyKernelVersionFunctions(t, bpf.AgentStepTTCP_IN)
11581158
},
11591159
},
@@ -1190,10 +1190,10 @@ func TestSkbCopyDatagramIter(t *testing.T) {
11901190
bpf.AttachSyscallWriteExit,
11911191
bpf.AttachKProbeSecuritySocketSendmsgEntry,
11921192
bpf.AttachKProbeSecuritySocketRecvmsgEntry,
1193-
func() link.Link {
1193+
func() (link.Link, error) {
11941194
return ApplyKernelVersionFunctions(t, bpf.AgentStepTDEV_IN)
11951195
},
1196-
func() link.Link {
1196+
func() (link.Link, error) {
11971197
return ApplyKernelVersionFunctions(t, bpf.AgentStepTUSER_COPY)
11981198
},
11991199
},

agent/agent_utils_test.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ func StartAgent0(bpfAttachFunctions []bpf.AttachBpfProgFunction,
5252
progs := list.New()
5353
for _, each := range bpfAttachFunctions {
5454
if each != nil {
55-
progs.PushBack(each())
55+
l, err := each()
56+
if err != nil {
57+
log.Fatalf("Attach failed: %v", err)
58+
}
59+
progs.PushBack(l)
5660
}
5761
}
5862
return progs
@@ -777,15 +781,15 @@ func min(a, b int) int {
777781
// compatilbeMode = b
778782
// }
779783

780-
func ApplyKernelVersionFunctions(t *testing.T, step bpf.AgentStepT) link.Link {
784+
func ApplyKernelVersionFunctions(t *testing.T, step bpf.AgentStepT) (link.Link, error) {
781785
v := compatible.GetCurrentKernelVersion()
782786
if step == bpf.AgentStepTNIC_IN {
783787
if v.SupportCapability(compatible.SupportXDP) {
784788
l, err := bpf.AttachXdp()
785789
if err != nil {
786790
t.Fatal(err)
787791
} else {
788-
return l
792+
return l, nil
789793
}
790794
} else {
791795
t.FailNow()
@@ -813,11 +817,11 @@ func ApplyKernelVersionFunctions(t *testing.T, step bpf.AgentStepT) link.Link {
813817
log.Fatalf("Attach failed: %v, functions: %v", err, functions)
814818
}
815819
} else {
816-
return l
820+
return l, nil
817821
}
818822
}
819823
t.FailNow()
820-
return nil
824+
return nil, nil
821825
}
822826

823827
func KernRcvTestWithHTTP(t *testing.T, progs []bpf.AttachBpfProgFunction, kernEvtFilter FindInterestedKernEventOptions, kernEvtAsserts KernDataEventAssertConditions) {

0 commit comments

Comments
 (0)