From f6f06b4b27dbfd27deedd323dfeae676229c91c1 Mon Sep 17 00:00:00 2001 From: Nimrod Gilboa Markevich Date: Sun, 15 May 2022 13:52:44 +0300 Subject: [PATCH] Add tap.cpuprofile flag. Change memprofile flag to tap.memprofile --- agent/go.mod | 1 + agent/go.sum | 2 ++ tap/go.mod | 1 + tap/go.sum | 2 ++ tap/passive_tapper.go | 13 ++++++++++++- 5 files changed, 18 insertions(+), 1 deletion(-) diff --git a/agent/go.mod b/agent/go.mod index d51900a64..8dda7fd57 100644 --- a/agent/go.mod +++ b/agent/go.mod @@ -100,6 +100,7 @@ require ( github.com/peterbourgon/diskv v2.0.1+incompatible // indirect github.com/pierrec/lz4 v2.6.1+incompatible // indirect github.com/pkg/errors v0.9.1 // indirect + github.com/pkg/profile v1.6.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/russross/blackfriday v1.6.0 // indirect github.com/santhosh-tekuri/jsonschema/v5 v5.0.0 // indirect diff --git a/agent/go.sum b/agent/go.sum index 90849a01b..754f79924 100644 --- a/agent/go.sum +++ b/agent/go.sum @@ -576,6 +576,8 @@ github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/profile v1.6.0 h1:hUDfIISABYI59DyeB3OTay/HxSRwTQ8rB/H83k6r5dM= +github.com/pkg/profile v1.6.0/go.mod h1:qBsxPvzyUincmltOk6iyRVxHYg4adc0OFOv72ZdLa18= github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= diff --git a/tap/go.mod b/tap/go.mod index b8ec83594..fe238668f 100644 --- a/tap/go.mod +++ b/tap/go.mod @@ -25,6 +25,7 @@ require ( github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/op/go-logging v0.0.0-20160315200505-970db520ece7 // indirect + github.com/pkg/profile v1.6.0 // indirect github.com/struCoder/pidusage v0.2.1 // indirect github.com/tklauser/go-sysconf v0.3.10 // indirect github.com/tklauser/numcpus v0.4.0 // indirect diff --git a/tap/go.sum b/tap/go.sum index 4deeca6cd..74a36403b 100644 --- a/tap/go.sum +++ b/tap/go.sum @@ -114,6 +114,8 @@ github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1y github.com/op/go-logging v0.0.0-20160315200505-970db520ece7 h1:lDH9UUVJtmYCjyT0CI4q8xvlXPxeZ0gYCVvWbmPlp88= github.com/op/go-logging v0.0.0-20160315200505-970db520ece7/go.mod h1:HzydrMdWErDVzsI23lYNej1Htcns9BCg93Dk0bBINWk= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/profile v1.6.0 h1:hUDfIISABYI59DyeB3OTay/HxSRwTQ8rB/H83k6r5dM= +github.com/pkg/profile v1.6.0/go.mod h1:qBsxPvzyUincmltOk6iyRVxHYg4adc0OFOv72ZdLa18= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= diff --git a/tap/passive_tapper.go b/tap/passive_tapper.go index 77871105b..a62ecdaa1 100644 --- a/tap/passive_tapper.go +++ b/tap/passive_tapper.go @@ -17,6 +17,7 @@ import ( "strings" "time" + "github.com/pkg/profile" "github.com/shirou/gopsutil/cpu" "github.com/struCoder/pidusage" "github.com/up9inc/mizu/logger" @@ -54,7 +55,8 @@ var staleTimeoutSeconds = flag.Int("staletimout", 120, "Max time in seconds to k var servicemesh = flag.Bool("servicemesh", false, "Record decrypted traffic if the cluster is configured with a service mesh and with mtls") var tls = flag.Bool("tls", false, "Enable TLS tapper") -var memprofile = flag.String("memprofile", "", "Write memory profile") +var cpuprofile = flag.String("tap.cpuprofile", "", "Write cpu profile") // cpuprofile flag clashes with one of the dependencies +var memprofile = flag.String("tap.memprofile", "", "Write memory profile") type TapOpts struct { HostMode bool @@ -68,6 +70,15 @@ var mainPacketInputChan chan source.TcpPacketInfo // global var tlsTapperInstance *tlstapper.TlsTapper // global func StartPassiveTapper(opts *TapOpts, outputItems chan *api.OutputChannelItem, extensionsRef []*api.Extension, options *api.TrafficFilteringOptions) { + if *cpuprofile != "" { + p := profile.Start(profile.CPUProfile, profile.ProfilePath(*cpuprofile), profile.NoShutdownHook) + go func() { + <-time.After(time.Second * 90) + p.Stop() + logger.Log.Info("Stopped CPU profiling") + }() + } + extensions = extensionsRef filteringOptions = options