diff --git a/agent.go b/agent.go index 3a10db6..a5d2613 100644 --- a/agent.go +++ b/agent.go @@ -16,28 +16,19 @@ import ( "github.com/projecteru2/agent/types" "github.com/projecteru2/agent/utils" "github.com/projecteru2/agent/version" + "github.com/projecteru2/core/log" "github.com/jinzhu/configor" - log "github.com/sirupsen/logrus" + zerolog "github.com/rs/zerolog/log" cli "github.com/urfave/cli/v2" _ "go.uber.org/automaxprocs" ) -func setupLogLevel(l string) error { - level, err := log.ParseLevel(l) - if err != nil { - return err - } - log.SetLevel(level) - log.SetOutput(os.Stdout) - return nil -} - func initConfig(c *cli.Context) *types.Config { config := &types.Config{} if err := configor.Load(config, c.String("config")); err != nil { - log.Fatalf("[main] load config failed %v", err) + log.Fatalf(c.Context, err, "[main] load config failed %v", err) } config.Prepare(c) @@ -48,8 +39,8 @@ func initConfig(c *cli.Context) *types.Config { func serve(c *cli.Context) error { rand.Seed(time.Now().UnixNano()) - if err := setupLogLevel(c.String("log-level")); err != nil { - log.Fatal(err) + if err := log.SetupLog(c.String("log-level")); err != nil { + zerolog.Fatal().Err(err).Send() } config := initConfig(c) @@ -74,7 +65,7 @@ func serve(c *cli.Context) error { _ = utils.Pool.Submit(func() { defer wg.Done() if err := workloadsManager.Run(ctx); err != nil { - log.Errorf("[agent] workload manager err: %v, exiting", err) + log.Error(c.Context, err, "[agent] workload manager failed") errChan <- err } }) @@ -86,7 +77,7 @@ func serve(c *cli.Context) error { _ = utils.Pool.Submit(func() { defer wg.Done() if err := nodeManager.Run(ctx); err != nil { - log.Errorf("[agent] node manager err: %v, exiting", err) + log.Error(c.Context, err, "[agent] node manager failed") errChan <- err } }) @@ -97,15 +88,15 @@ func serve(c *cli.Context) error { _ = utils.Pool.Submit(func() { select { case <-ctx.Done(): - log.Info("[agent] Agent exiting") - case <-errChan: - log.Error("[agent] Got error, exiting") + log.Info(c.Context, "[agent] Agent exiting") + case err := <-errChan: + log.Error(c.Context, err, "[agent] Got error, exiting") cancel() case sig := <-signalChan: - log.Infof("[agent] Agent caught system signal %v", sig) + log.Infof(c.Context, "[agent] Agent caught system signal %v", sig) if sig != syscall.SIGUSR1 { if err := nodeManager.Exit(); err != nil { - log.Errorf("[agent] node manager exits with err: %v", err) + log.Error(c.Context, err, "[agent] node manager exits with err") } } cancel() @@ -245,6 +236,6 @@ func main() { Action: serve, } if err := app.Run(os.Args); err != nil { - log.Errorf("Error running agent: %v", err) + zerolog.Fatal().Err(err).Send() } } diff --git a/api/http.go b/api/http.go index 776a96a..9b4eeb5 100644 --- a/api/http.go +++ b/api/http.go @@ -12,10 +12,10 @@ import ( "github.com/projecteru2/agent/manager/workload" "github.com/projecteru2/agent/types" "github.com/projecteru2/agent/version" + "github.com/projecteru2/core/log" "github.com/bmizerany/pat" "github.com/prometheus/client_golang/prometheus/promhttp" - log "github.com/sirupsen/logrus" ) // JSON define a json @@ -57,7 +57,7 @@ func (h *Handler) log(w http.ResponseWriter, req *http.Request) { if hijack, ok := w.(http.Hijacker); ok { conn, buf, err := hijack.Hijack() if err != nil { - log.Errorf("[apiLog] connect failed %v", err) + log.Error(req.Context(), err, "[apiLog] connect failed") return } defer conn.Close() @@ -98,13 +98,13 @@ func (h *Handler) Serve() { http.Handle("/", restfulAPIServer) http.Handle("/metrics", promhttp.Handler()) - log.Infof("[apiServe] http api started %s", h.config.API.Addr) + log.Infof(nil, "[apiServe] http api started %s", h.config.API.Addr) //nolint server := &http.Server{ Addr: h.config.API.Addr, ReadHeaderTimeout: 3 * time.Second, } if err := server.ListenAndServe(); err != nil { - log.Errorf("http api failed %s", err) + log.Error(nil, err, "http api start failed") //nolint } } diff --git a/common/error.go b/common/error.go index 15fd493..1374971 100644 --- a/common/error.go +++ b/common/error.go @@ -1,6 +1,6 @@ package common -import "errors" +import "github.com/pkg/errors" // ErrNotImplemented . var ( @@ -25,4 +25,12 @@ var ( ErrSyscallFailed = errors.New("syscall fail, Not a syscall.Stat_t") // ErrDevNotFound . ErrDevNotFound = errors.New("device not found") + // ErrJournalDisable . + ErrJournalDisable = errors.New("journal disabled") + // ErrInvaildContainer . + ErrInvaildContainer = errors.New("invaild container") + // ErrGetLockFailed . + ErrGetLockFailed = errors.New("get lock failed") + // ErrInvaildVM . + ErrInvaildVM = errors.New("invaild vm") ) diff --git a/go.mod b/go.mod index c049ca2..b9e162d 100644 --- a/go.mod +++ b/go.mod @@ -12,11 +12,12 @@ require ( github.com/jinzhu/configor v1.2.1 github.com/panjf2000/ants/v2 v2.5.0 github.com/patrickmn/go-cache v2.1.0+incompatible - github.com/projecteru2/core v0.0.0-20221011033901-ff9e2848a9e1 + github.com/pkg/errors v0.9.1 + github.com/projecteru2/core v0.0.0-20221027062222-5e76472a0774 github.com/projecteru2/libyavirt v0.0.0-20220621042712-95cdc6363b1c github.com/prometheus/client_golang v1.13.0 + github.com/rs/zerolog v1.28.0 github.com/shirou/gopsutil v3.20.11+incompatible - github.com/sirupsen/logrus v1.9.0 github.com/stretchr/testify v1.8.0 github.com/urfave/cli/v2 v2.17.1 go.uber.org/automaxprocs v1.5.1 @@ -42,17 +43,19 @@ require ( github.com/gogo/protobuf v1.3.2 // indirect github.com/golang/protobuf v1.5.2 // indirect github.com/juju/errors v1.0.0 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.16 // indirect github.com/matttproud/golang_protobuf_extensions v1.0.2 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/moby/term v0.0.0-20220808134915-39b0c02b01ae // indirect github.com/opencontainers/go-digest v1.0.0 // indirect github.com/opencontainers/image-spec v1.1.0-rc2 // indirect - github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/prometheus/client_model v0.2.0 // indirect github.com/prometheus/common v0.37.0 // indirect github.com/prometheus/procfs v0.8.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect + github.com/sirupsen/logrus v1.9.0 // indirect github.com/stretchr/objx v0.4.0 // indirect github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect go.uber.org/zap v1.23.0 // indirect diff --git a/go.sum b/go.sum index b9cb85b..59afd6d 100644 --- a/go.sum +++ b/go.sum @@ -75,6 +75,7 @@ github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnht github.com/cncf/xds/go v0.0.0-20210312221358-fbca930ec8ed/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf h1:iW4rZ826su+pqaw19uhpSCzhj44qo35pNgKFGqzDKkU= github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= +github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/cornelk/hashmap v1.0.8 h1:nv0AWgw02n+iDcawr5It4CjQIAcdMMKRrs10HOJYlrc= github.com/cornelk/hashmap v1.0.8/go.mod h1:RfZb7JO3RviW/rT6emczVuC/oxpdz4UsSB2LJSclR1k= github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w= @@ -118,6 +119,7 @@ github.com/go-ole/go-ole v0.0.0-20180213002836-a1ec82a652eb/go.mod h1:7FAglXiTm7 github.com/go-ping/ping v0.0.0-20210407214646-e4e642a95741 h1:b0sLP++Tsle+s57tqg5sUk1/OQsC6yMCciVeqNzOcwU= github.com/go-ping/ping v0.0.0-20210407214646-e4e642a95741/go.mod h1:35JbSyV/BYqHwwRA6Zr1uVDm1637YlNOU61wI797NPI= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= +github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= @@ -205,6 +207,12 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= +github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/matttproud/golang_protobuf_extensions v1.0.2 h1:hAHbPm5IJGijwng3PWk09JkG9WeqChjprR5s9bBZ+OM= github.com/matttproud/golang_protobuf_extensions v1.0.2/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4= @@ -236,8 +244,8 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE 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/prashantv/gostub v1.1.0 h1:BTyx3RfQjRHnUWaGF9oQos79AlQ5k8WNktv7VGvVH4g= -github.com/projecteru2/core v0.0.0-20221011033901-ff9e2848a9e1 h1:/HOTU06kNTYVKXCI9G4ks6Vp23ItwGo5m7MmyRqpuP4= -github.com/projecteru2/core v0.0.0-20221011033901-ff9e2848a9e1/go.mod h1:193eVQXbJKmemk4aYY1k7679JE5kEKi+5CBBYFbrOnM= +github.com/projecteru2/core v0.0.0-20221027062222-5e76472a0774 h1:hVUfTHt/IXDNWOCXIJeyAlHpWQDVYvpOMlkcKte/768= +github.com/projecteru2/core v0.0.0-20221027062222-5e76472a0774/go.mod h1:Z2unoE5X/P8RrYa8PsMGbTlmEgZWgxOl8W3DRwR17Bw= github.com/projecteru2/libyavirt v0.0.0-20220621042712-95cdc6363b1c h1:bsQMtiuDy3vH/+2Bm4o+0dSqhHGrzvNfoHW9C/7Tyqw= github.com/projecteru2/libyavirt v0.0.0-20220621042712-95cdc6363b1c/go.mod h1:M4iuzX/GsUF8cnkZujoJ8OWULi0+fuwg0D4Zahhzco0= github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= @@ -267,6 +275,9 @@ github.com/prometheus/procfs v0.8.0 h1:ODq8ZFEaYeCaZOJlZZdJA2AbQR98dSHSM1KW/You5 github.com/prometheus/procfs v0.8.0/go.mod h1:z7EfXMXOkbkqb9IINtpCn86r/to3BnA0uaxHdg830/4= github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= +github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= +github.com/rs/zerolog v1.28.0 h1:MirSo27VyNi7RJYP3078AA1+Cyzd2GB66qy3aUHvsWY= +github.com/rs/zerolog v1.28.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6usyD0= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/shirou/gopsutil v3.20.11+incompatible h1:LJr4ZQK4mPpIV5gOa4jCOKOGb4ty4DZO54I4FGqIpto= @@ -440,9 +451,12 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20221006211917-84dc82d7e875 h1:AzgQNqF+FKwyQ5LbVrVqOcuuFB67N47F9+htZYH0wFM= golang.org/x/sys v0.0.0-20221006211917-84dc82d7e875/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= diff --git a/logs/enc.go b/logs/enc.go index d52512e..8cf2abd 100644 --- a/logs/enc.go +++ b/logs/enc.go @@ -6,6 +6,7 @@ import ( "io" "sync" + "github.com/projecteru2/agent/common" "github.com/projecteru2/agent/types" "github.com/coreos/go-systemd/journal" @@ -41,8 +42,6 @@ func (e *StreamEncoder) Close() error { return e.wt.Close() } -var errJournalDisabled = fmt.Errorf("journal disabled") - // JournalEncoder . type JournalEncoder struct { sync.Mutex @@ -51,7 +50,7 @@ type JournalEncoder struct { // CreateJournalEncoder . func CreateJournalEncoder() (*JournalEncoder, error) { if !journal.Enabled() { - return nil, errJournalDisabled + return nil, common.ErrJournalDisable } return &JournalEncoder{}, nil } diff --git a/logs/writer.go b/logs/writer.go index c1674d1..b4cfda6 100644 --- a/logs/writer.go +++ b/logs/writer.go @@ -11,7 +11,7 @@ import ( "github.com/projecteru2/agent/types" "github.com/projecteru2/agent/utils" - log "github.com/sirupsen/logrus" + "github.com/projecteru2/core/log" ) // Discard . @@ -64,15 +64,15 @@ func NewWriter(ctx context.Context, addr string, stdout bool) (writer *Writer, e switch { case err == common.ErrInvalidScheme: - log.Infof("[writer] create an empty writer for %s success", addr) + log.Infof(ctx, "[writer] create an empty writer for %s success", addr) writer.enc = NewStreamEncoder(discard{}) - case err == errJournalDisabled: + case err == common.ErrJournalDisable: return nil, err case err != nil: - log.Errorf("[writer] failed to create writer encoder for %s, err: %v, will retry", addr, err) + log.Errorf(ctx, err, "[writer] failed to create writer encoder for %s, will retry", addr) writer.needReconnect = true default: - log.Infof("[writer] create writer for %s success", addr) + log.Infof(ctx, "[writer] create writer for %s success", addr) } _ = utils.Pool.Submit(func() { writer.keepalive(ctx) }) @@ -82,7 +82,7 @@ func NewWriter(ctx context.Context, addr string, stdout bool) (writer *Writer, e // Write write log to remote func (w *Writer) Write(logline *types.Log) error { if w.stdout { - log.Info(logline) + log.Info(nil, logline) //nolint } if len(w.addr) == 0 && len(w.scheme) == 0 { return nil @@ -109,7 +109,7 @@ func (w *Writer) close() error { w.enc = nil } }) - log.Infof("[writer] writer for %s closed", w.addr) + log.Infof(nil, "[writer] writer for %s closed", w.addr) //nolint return err } @@ -159,7 +159,7 @@ func (w *Writer) createEncoder() (enc Encoder, err error) { case "journal": enc, err = CreateJournalEncoder() default: - log.Errorf("[writer] Invalid scheme: %s", w.scheme) + log.Errorf(nil, err, "[writer] Invalid scheme: %s", w.scheme) //nolint err = common.ErrInvalidScheme } return enc, err @@ -174,17 +174,17 @@ func (w *Writer) reconnect() { return } - log.Debugf("[writer] Reconnecting to %s...", w.addr) + log.Debugf(nil, "[writer] Reconnecting to %s...", w.addr) //nolint enc, err := w.createEncoder() if err == nil { w.withLock(func() { w.enc = enc w.needReconnect = false }) - log.Debugf("[writer] Connect to %s successfully", w.addr) + log.Debugf(nil, "[writer] Connect to %s successfully", w.addr) //nolint return } - log.Warnf("[writer] Failed to connect to %s: %s", w.addr, err) + log.Warnf(nil, "[writer] Failed to connect to %s: %s", w.addr, err) //nolint } func (w *Writer) keepalive(ctx context.Context) { @@ -198,7 +198,7 @@ func (w *Writer) keepalive(ctx context.Context) { // leave some time for the pending writing time.Sleep(CloseWaitInterval) if err := w.close(); err != nil { - log.Errorf("[keepalive] failed to close writer %s, err: %s", w.addr, err) + log.Errorf(nil, err, "[keepalive] failed to close writer %s", w.addr) //nolint } return } @@ -207,7 +207,7 @@ func (w *Writer) keepalive(ctx context.Context) { func (w *Writer) checkError(err error) { if err != nil && err != common.ErrConnecting { - log.Errorf("[writer] Sending log failed %s", err) + log.Error(nil, err, "[writer] Sending log failed") //nolint w.withLock(func() { if w.enc != nil { w.enc.Close() diff --git a/logs/writer_test.go b/logs/writer_test.go index 2916858..25c4b18 100644 --- a/logs/writer_test.go +++ b/logs/writer_test.go @@ -6,6 +6,7 @@ import ( "testing" "time" + "github.com/projecteru2/agent/common" "github.com/projecteru2/agent/types" "github.com/stretchr/testify/assert" @@ -39,7 +40,7 @@ func TestNewWriterWithJournal(t *testing.T) { defer cancel() addr := "journal://system" enc, err := CreateJournalEncoder() - if err == errJournalDisabled { + if err == common.ErrJournalDisable { return } assert.NoError(t, err) diff --git a/manager/node/heartbeat.go b/manager/node/heartbeat.go index 933cf75..90ced38 100644 --- a/manager/node/heartbeat.go +++ b/manager/node/heartbeat.go @@ -6,7 +6,7 @@ import ( "github.com/projecteru2/agent/utils" - log "github.com/sirupsen/logrus" + "github.com/projecteru2/core/log" ) // heartbeat creates a new goroutine to report status every HeartbeatInterval seconds @@ -35,11 +35,11 @@ func (m *Manager) heartbeat(ctx context.Context) { // which means if a node is not available, subcriber will notice this after at least 360s. // HealthCheck.Timeout is used as timeout of requesting core Profile func (m *Manager) nodeStatusReport(ctx context.Context) { - log.Debug("[nodeStatusReport] report begins") - defer log.Debug("[nodeStatusReport] report ends") + log.Debug(ctx, "[nodeStatusReport] report begins") + defer log.Debug(ctx, "[nodeStatusReport] report ends") if !m.runtimeClient.IsDaemonRunning(ctx) { - log.Error("[nodeStatusReport] cannot connect to runtime daemon") + log.Warn(ctx, "[nodeStatusReport] cannot connect to runtime daemon") return } @@ -48,11 +48,11 @@ func (m *Manager) nodeStatusReport(ctx context.Context) { if err := utils.BackoffRetry(ctx, 3, func() (err error) { utils.WithTimeout(ctx, m.config.GlobalConnectionTimeout, func(ctx context.Context) { if err = m.store.SetNodeStatus(ctx, ttl); err != nil { - log.Errorf("[nodeStatusReport] failed to set node status of %v, err %v", m.config.HostName, err) + log.Errorf(ctx, err, "[nodeStatusReport] failed to set node status of %v", m.config.HostName) } }) return err }); err != nil { - log.Errorf("[nodeStatusReport] failed to set node status of %v for 3 times", m.config.HostName) + log.Errorf(ctx, err, "[nodeStatusReport] failed to set node status of %v for 3 times", m.config.HostName) } } diff --git a/manager/node/manager.go b/manager/node/manager.go index eca967c..017e6d9 100644 --- a/manager/node/manager.go +++ b/manager/node/manager.go @@ -14,7 +14,7 @@ import ( "github.com/projecteru2/agent/types" "github.com/projecteru2/agent/utils" - log "github.com/sirupsen/logrus" + "github.com/projecteru2/core/log" ) // Manager manages node status @@ -42,7 +42,7 @@ func NewManager(ctx context.Context, config *types.Config) (*Manager, error) { node, err := m.store.GetNode(ctx, config.HostName) if err != nil { - log.Errorf("[NewManager] failed to get node %s, err: %s", config.HostName, err) + log.Errorf(ctx, err, "[NewManager] failed to get node %s", config.HostName) return nil, err } @@ -75,17 +75,17 @@ func NewManager(ctx context.Context, config *types.Config) (*Manager, error) { // Run runs a node manager func (m *Manager) Run(ctx context.Context) error { - log.Info("[NodeManager] start node status heartbeat") + log.Info(ctx, "[NodeManager] start node status heartbeat") _ = utils.Pool.Submit(func() { m.heartbeat(ctx) }) <-ctx.Done() - log.Info("[NodeManager] exiting") + log.Info(ctx, "[NodeManager] exiting") return nil } // Exit . func (m *Manager) Exit() error { - log.Infof("[NodeManager] remove node status of %s", m.config.HostName) + log.Infof(nil, "[NodeManager] remove node status of %s", m.config.HostName) //nolint // ctx is now canceled. use a new context. var err error @@ -94,7 +94,7 @@ func (m *Manager) Exit() error { err = m.store.SetNodeStatus(ctx, -1) }) if err != nil { - log.Errorf("[NodeManager] failed to remove node status of %v, err: %s", m.config.HostName, err) + log.Errorf(nil, err, "[NodeManager] failed to remove node status of %v", m.config.HostName) //nolint return err } return nil diff --git a/manager/workload/attach.go b/manager/workload/attach.go index fe7d73d..e84547f 100644 --- a/manager/workload/attach.go +++ b/manager/workload/attach.go @@ -13,11 +13,11 @@ import ( "github.com/projecteru2/agent/types" "github.com/projecteru2/agent/utils" - log "github.com/sirupsen/logrus" + "github.com/projecteru2/core/log" ) func (m *Manager) attach(ctx context.Context, ID string) { - log.Debugf("[attach] attaching workload %v", ID) + log.Debugf(ctx, "[attach] attaching workload %v", ID) ctx, cancel := context.WithCancel(ctx) defer cancel() @@ -27,7 +27,7 @@ func (m *Manager) attach(ctx context.Context, ID string) { } writer, err := logs.NewWriter(ctx, transfer, m.config.Log.Stdout) if err != nil { - log.Errorf("[attach] Create log forward %s failed %s", transfer, err) + log.Errorf(ctx, err, "[attach] Create log forward %s failed", transfer) return } @@ -35,47 +35,47 @@ func (m *Manager) attach(ctx context.Context, ID string) { workloadName, err := m.runtimeClient.GetWorkloadName(ctx, ID) if err != nil { if err != common.ErrNotImplemented { - log.Errorf("[attach] failed to get workload name, id: %v, err: %v", ID, err) + log.Errorf(ctx, err, "[attach] failed to get workload name, id: %v", ID) } else { - log.Debug("[attach] should ignore this workload") + log.Debug(ctx, "[attach] should ignore this workload") } return } name, entryPoint, ident, err := utils.GetAppInfo(workloadName) if err != nil { - log.Errorf("[attach] invalid workload name %s, err: %v", workloadName, err) + log.Errorf(ctx, err, "[attach] invalid workload name %s", workloadName) return } // attach workload outr, errr, err := m.runtimeClient.AttachWorkload(ctx, ID) if err != nil { - log.Errorf("[attach] failed to attach workload %s, err: %v", workloadName, err) + log.Errorf(ctx, err, "[attach] failed to attach workload %s", workloadName) return } - log.Infof("[attach] attach %s workload %s success", workloadName, ID) + log.Infof(ctx, "[attach] attach %s workload %s success", workloadName, ID) // attach metrics _ = utils.Pool.Submit(func() { m.runtimeClient.CollectWorkloadMetrics(ctx, ID) }) extra, err := m.runtimeClient.LogFieldsExtra(ctx, ID) if err != nil { - log.Errorf("[attach] failed to get log fields extra, err: %v", err) + log.Error(ctx, err, "[attach] failed to get log fields extra") } wg := &sync.WaitGroup{} pump := func(typ string, source io.Reader) { defer wg.Done() - log.Debugf("[attach] attach pump %s %s %s start", workloadName, ID, typ) - defer log.Debugf("[attach] attach pump %s %s %s finished", workloadName, ID, typ) + log.Debugf(ctx, "[attach] attach pump %s %s %s start", workloadName, ID, typ) + defer log.Debugf(ctx, "[attach] attach pump %s %s %s finished", workloadName, ID, typ) buf := bufio.NewReader(source) for { data, err := buf.ReadString('\n') if err != nil { if err != io.EOF { - log.Errorf("[attach] attach pump %s %s %s failed, err: %v", workloadName, ID, typ, err) + log.Errorf(ctx, err, "[attach] attach pump %s %s %s failed, err: %v", workloadName, ID, typ, err) } return } @@ -95,7 +95,7 @@ func (m *Manager) attach(ctx context.Context, ID string) { m.logBroadcaster.logC <- l } if err := writer.Write(l); err != nil && !(entryPoint == "agent" && utils.IsDockerized()) { - log.Errorf("[attach] %s workload %s_%s write failed %v", workloadName, entryPoint, ID, err) + log.Errorf(ctx, err, "[attach] %s workload %s_%s write failed", workloadName, entryPoint, ID) } } } diff --git a/manager/workload/event.go b/manager/workload/event.go index 91c5f54..f5382d1 100644 --- a/manager/workload/event.go +++ b/manager/workload/event.go @@ -7,7 +7,7 @@ import ( "github.com/projecteru2/agent/types" "github.com/projecteru2/agent/utils" - log "github.com/sirupsen/logrus" + "github.com/projecteru2/core/log" ) // EventHandler define event handler @@ -34,17 +34,17 @@ func (e *EventHandler) Watch(ctx context.Context, c <-chan *types.WorkloadEventM select { case ev, ok := <-c: if !ok { - log.Info("[Watch] event chan closed") + log.Info(ctx, "[Watch] event chan closed") return } - log.Infof("[Watch] Monitor: workload id %s action %s", ev.ID, ev.Action) + log.Infof(ctx, "[Watch] Monitor: workload id %s action %s", ev.ID, ev.Action) e.Lock() if h := e.handlers[ev.Action]; h != nil { _ = utils.Pool.Submit(func() { h(ctx, ev) }) } e.Unlock() case <-ctx.Done(): - log.Info("[Watch] context canceled, stop watching") + log.Info(ctx, "[Watch] context canceled, stop watching") return } } diff --git a/manager/workload/health_check.go b/manager/workload/health_check.go index 2c620f3..8af764a 100644 --- a/manager/workload/health_check.go +++ b/manager/workload/health_check.go @@ -7,7 +7,7 @@ import ( "github.com/projecteru2/agent/types" "github.com/projecteru2/agent/utils" - log "github.com/sirupsen/logrus" + "github.com/projecteru2/core/log" ) func (m *Manager) healthCheck(ctx context.Context) { @@ -29,10 +29,10 @@ func (m *Manager) healthCheck(ctx context.Context) { // 但是这时候 health check 刚返回 true 回来并写入 core // 为了保证最终数据一致性这里也要检测 func (m *Manager) checkAllWorkloads(ctx context.Context) { - log.Debug("[checkAllWorkloads] health check begin") + log.Debug(ctx, "[checkAllWorkloads] health check begin") workloadIDs, err := m.runtimeClient.ListWorkloadIDs(ctx, m.getBaseFilter()) if err != nil { - log.Errorf("[checkAllWorkloads] Error when list all workloads with label \"ERU=1\": %v", err) + log.Error(ctx, err, "[checkAllWorkloads] Error when list all workloads with label \"ERU=1\"") return } @@ -47,12 +47,12 @@ func (m *Manager) checkAllWorkloads(ctx context.Context) { func (m *Manager) checkOneWorkload(ctx context.Context, ID string) bool { workloadStatus, err := m.runtimeClient.GetStatus(ctx, ID, true) if err != nil { - log.Errorf("[checkOneWorkload] failed to get status of workload %s, err: %v", ID, err) + log.Errorf(ctx, err, "[checkOneWorkload] failed to get status of workload %s", ID) return false } if err = m.setWorkloadStatus(ctx, workloadStatus); err != nil { - log.Errorf("[checkOneWorkload] update workload status for %v failed, err: %v", ID, err) + log.Errorf(ctx, err, "[checkOneWorkload] update workload status for %v failed", ID) } return workloadStatus.Healthy } diff --git a/manager/workload/load.go b/manager/workload/load.go index 8090059..34a8a81 100644 --- a/manager/workload/load.go +++ b/manager/workload/load.go @@ -6,7 +6,7 @@ import ( "time" "github.com/projecteru2/agent/utils" - log "github.com/sirupsen/logrus" + "github.com/projecteru2/core/log" ) func (m *Manager) listWorkloadIDsWithRetry(ctx context.Context, filter map[string]string) ([]string, error) { @@ -21,7 +21,7 @@ func (m *Manager) listWorkloadIDsWithRetry(ctx context.Context, filter map[strin case <-ticker.C: workloadIDs, err = m.runtimeClient.ListWorkloadIDs(ctx, filter) if err != nil { - log.Errorf("[initWorkloadStatus] Failed to load workloads: %v, will retry", err) + log.Error(ctx, err, "[initWorkloadStatus] Failed to load workloads, will retry") continue } return workloadIDs, nil @@ -30,33 +30,33 @@ func (m *Manager) listWorkloadIDsWithRetry(ctx context.Context, filter map[strin } func (m *Manager) initWorkloadStatus(ctx context.Context) error { - log.Info("[initWorkloadStatus] Load workloads") + log.Info(ctx, "[initWorkloadStatus] Load workloads") workloadIDs, err := m.listWorkloadIDsWithRetry(ctx, m.getBaseFilter()) if err != nil { - log.Errorf("[initWorkloadStatus] Failed to load workloads: %v", err) + log.Error(ctx, err, "[initWorkloadStatus] Failed to load workloads") return err } wg := &sync.WaitGroup{} for _, workloadID := range workloadIDs { - log.Debugf("[initWorkloadStatus] detect workload %s", workloadID) + log.Debugf(ctx, "[initWorkloadStatus] detect workload %s", workloadID) wg.Add(1) ID := workloadID _ = utils.Pool.Submit(func() { defer wg.Done() workloadStatus, err := m.runtimeClient.GetStatus(ctx, ID, true) if err != nil { - log.Errorf("[initWorkloadStatus] get workload %v status failed %v", ID, err) + log.Errorf(ctx, err, "[initWorkloadStatus] get workload %v status failed", ID) return } if workloadStatus.Running { - log.Debugf("[initWorkloadStatus] workload %s is running", workloadStatus.ID) + log.Debugf(ctx, "[initWorkloadStatus] workload %s is running", workloadStatus.ID) _ = utils.Pool.Submit(func() { m.attach(ctx, ID) }) } if err := m.setWorkloadStatus(ctx, workloadStatus); err != nil { - log.Errorf("[initWorkloadStatus] update workload %v status failed %v", ID, err) + log.Errorf(ctx, err, "[initWorkloadStatus] update workload %v status failed", ID) } }) } diff --git a/manager/workload/log.go b/manager/workload/log.go index 6704378..e3a0f8d 100644 --- a/manager/workload/log.go +++ b/manager/workload/log.go @@ -10,9 +10,8 @@ import ( "github.com/alphadose/haxmap" "github.com/projecteru2/agent/types" "github.com/projecteru2/agent/utils" + corelog "github.com/projecteru2/core/log" coreutils "github.com/projecteru2/core/utils" - - "github.com/sirupsen/logrus" ) type subscriber struct { @@ -75,7 +74,7 @@ func (l *logBroadcaster) subscribe(ctx context.Context, app string, buf *bufio.R errChan: errChan, } - logrus.Infof("%s %s log subscribed", app, ID) + corelog.Infof(ctx, "%s %s log subscribed", app, ID) return ID, errChan, func() { cancel() _ = utils.Pool.Submit(func() { l.unsubscribe(app, ID) }) @@ -93,7 +92,7 @@ func (l *logBroadcaster) unsubscribe(app string, ID string) { } delete(subscribers, ID) - logrus.Infof("%s %s detached", app, ID) + corelog.Infof(nil, "%s %s detached", app, ID) //nolint // if no subscribers for this app, remove the key if len(subscribers) == 0 { @@ -111,7 +110,7 @@ func (l *logBroadcaster) broadcast(log *types.Log) { } data, err := json.Marshal(log) if err != nil { - logrus.Error(err) + corelog.Error(nil, err) //nolint return } line := fmt.Sprintf("%X\r\n%s\r\n\r\n", len(data)+2, string(data)) @@ -128,7 +127,7 @@ func (l *logBroadcaster) broadcast(log *types.Log) { return } if _, err := sub.buf.Write([]byte(line)); err != nil { - logrus.Debugf("[broadcast] failed to write into %v, err: %v", ID, err) + corelog.Debugf(nil, "[broadcast] failed to write into %v, err: %v", ID, err) //nolint sub.cancel() sub.errChan <- err return @@ -143,7 +142,7 @@ func (l *logBroadcaster) run(ctx context.Context) { for { select { case <-ctx.Done(): - logrus.Info("[logBroadcaster] stops") + corelog.Info(ctx, "[logBroadcaster] stops") return case log := <-l.logC: l.broadcast(log) diff --git a/manager/workload/log_test.go b/manager/workload/log_test.go index e36a6c9..77b705e 100644 --- a/manager/workload/log_test.go +++ b/manager/workload/log_test.go @@ -10,13 +10,12 @@ import ( "github.com/projecteru2/agent/types" "github.com/bmizerany/pat" - "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" ) func TestLogBroadcaster(t *testing.T) { manager := newMockWorkloadManager(t) - logrus.SetLevel(logrus.DebugLevel) + // log.SetupLog("debug") logCtx, logCancel := context.WithCancel(context.Background()) defer logCancel() @@ -65,14 +64,14 @@ func TestLogBroadcaster(t *testing.T) { } }() - ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) + ctx, cancel := context.WithTimeout(context.Background(), 7*time.Second) defer cancel() go manager.logBroadcaster.run(ctx) // wait for http server to start time.Sleep(time.Second) - reqCtx, reqCancel := context.WithTimeout(ctx, 10*time.Second) + reqCtx, reqCancel := context.WithTimeout(ctx, 3*time.Second) defer reqCancel() req, err := http.NewRequestWithContext(reqCtx, "GET", "http://127.0.0.1:12310/log/?app=nerv", nil) diff --git a/manager/workload/manager.go b/manager/workload/manager.go index ee3a94c..dc0bc4e 100644 --- a/manager/workload/manager.go +++ b/manager/workload/manager.go @@ -18,7 +18,7 @@ import ( "github.com/projecteru2/agent/types" "github.com/projecteru2/agent/utils" - log "github.com/sirupsen/logrus" + "github.com/projecteru2/core/log" ) // Manager . @@ -48,9 +48,11 @@ func NewManager(ctx context.Context, config *types.Config) (*Manager, error) { switch config.Store { case common.GRPCStore: corestore.Init(ctx, config) - if m.store = corestore.Get(); m.store == nil { + store := corestore.Get() + if store == nil { return nil, common.ErrGetStoreFailed } + m.store = store case common.MocksStore: m.store = storemocks.NewFakeStore() default: @@ -59,7 +61,7 @@ func NewManager(ctx context.Context, config *types.Config) (*Manager, error) { node, err := m.store.GetNode(ctx, config.HostName) if err != nil { - log.Errorf("[NewManager] failed to get node %s, err: %s", config.HostName, err) + log.Errorf(ctx, err, "[NewManager] failed to get node %s", config.HostName) return nil, err } @@ -117,7 +119,7 @@ func (m *Manager) Run(ctx context.Context) error { // wait for signal <-ctx.Done() - log.Info("[WorkloadManager] exiting") + log.Info(ctx, "[WorkloadManager] exiting") return nil } @@ -132,7 +134,7 @@ func (m *Manager) PullLog(ctx context.Context, app string, buf *bufio.ReadWriter return case err := <-errChan: if err != io.EOF { - log.Errorf("[PullLog] %v failed to pull log, err: %v", ID, err) + log.Errorf(ctx, err, "[PullLog] %v failed to pull log", ID) } return } diff --git a/manager/workload/monitor.go b/manager/workload/monitor.go index d55c73b..35ae4ee 100644 --- a/manager/workload/monitor.go +++ b/manager/workload/monitor.go @@ -8,7 +8,7 @@ import ( "github.com/projecteru2/agent/types" "github.com/projecteru2/agent/utils" - log "github.com/sirupsen/logrus" + "github.com/projecteru2/core/log" ) var eventHandler = NewEventHandler() @@ -22,7 +22,7 @@ func (m *Manager) initMonitor(ctx context.Context) (<-chan *types.WorkloadEventM } func (m *Manager) watchEvent(ctx context.Context, eventChan <-chan *types.WorkloadEventMessage) { - log.Info("[watchEvent] Status watch start") + log.Info(ctx, "[watchEvent] Status watch start") eventHandler.Watch(ctx, eventChan) } @@ -33,10 +33,10 @@ func (m *Manager) monitor(ctx context.Context) { _ = utils.Pool.Submit(func() { m.watchEvent(ctx, eventChan) }) select { case <-ctx.Done(): - log.Info("[monitor] context canceled, stop monitoring") + log.Info(ctx, "[monitor] context canceled, stop monitoring") return case err := <-errChan: - log.Errorf("[monitor] received an err: %v, will retry", err) + log.Error(ctx, err, "[monitor] received an err, will retry") time.Sleep(m.config.GlobalConnectionTimeout) } } @@ -44,7 +44,7 @@ func (m *Manager) monitor(ctx context.Context) { // 检查一个workload,允许重试 func (m *Manager) checkOneWorkloadWithBackoffRetry(ctx context.Context, ID string) { - log.Debugf("[checkOneWorkloadWithBackoffRetry] check workload %s", ID) + log.Debugf(ctx, "[checkOneWorkloadWithBackoffRetry] check workload %s", ID) m.checkWorkloadMutex.Lock() defer m.checkWorkloadMutex.Unlock() @@ -63,16 +63,16 @@ func (m *Manager) checkOneWorkloadWithBackoffRetry(ctx context.Context, ID strin m.startingWorkloads.Set(ID, retryTask) _ = utils.Pool.Submit(func() { if err := retryTask.Run(); err != nil { - log.Debugf("[checkOneWorkloadWithBackoffRetry] workload %s still not healthy", ID) + log.Debugf(ctx, "[checkOneWorkloadWithBackoffRetry] workload %s still not healthy", ID) } }) } func (m *Manager) handleWorkloadStart(ctx context.Context, event *types.WorkloadEventMessage) { - log.Debugf("[handleWorkloadStart] workload %s start", event.ID) + log.Debugf(ctx, "[handleWorkloadStart] workload %s start", event.ID) workloadStatus, err := m.runtimeClient.GetStatus(ctx, event.ID, true) if err != nil { - log.Errorf("[handleWorkloadStart] faild to get workload %v status, err: %v", event.ID, err) + log.Errorf(ctx, err, "[handleWorkloadStart] faild to get workload %v status", event.ID) return } @@ -82,7 +82,7 @@ func (m *Manager) handleWorkloadStart(ctx context.Context, event *types.Workload if workloadStatus.Healthy { if err := m.store.SetWorkloadStatus(ctx, workloadStatus, m.config.GetHealthCheckStatusTTL()); err != nil { - log.Errorf("[handleWorkloadStart] update deploy status failed %v", err) + log.Error(ctx, err, "[handleWorkloadStart] update deploy status failed") } } else { m.checkOneWorkloadWithBackoffRetry(ctx, event.ID) @@ -90,14 +90,14 @@ func (m *Manager) handleWorkloadStart(ctx context.Context, event *types.Workload } func (m *Manager) handleWorkloadDie(ctx context.Context, event *types.WorkloadEventMessage) { - log.Debugf("[handleWorkloadDie] container %s die", event.ID) + log.Debugf(ctx, "[handleWorkloadDie] container %s die", event.ID) workloadStatus, err := m.runtimeClient.GetStatus(ctx, event.ID, true) if err != nil { - log.Errorf("[handleWorkloadDie] faild to get workload %v status, err: %v", event.ID, err) + log.Errorf(ctx, err, "[handleWorkloadDie] faild to get workload %v status", event.ID) return } if err := m.store.SetWorkloadStatus(ctx, workloadStatus, m.config.GetHealthCheckStatusTTL()); err != nil { - log.Errorf("[handleWorkloadDie] update deploy status failed %v", err) + log.Error(ctx, err, "[handleWorkloadDie] update deploy status failed") } } diff --git a/runtime/docker/client.go b/runtime/docker/client.go index 9826492..d9ae13e 100644 --- a/runtime/docker/client.go +++ b/runtime/docker/client.go @@ -5,7 +5,7 @@ import ( "github.com/projecteru2/agent/types" - log "github.com/sirupsen/logrus" + "github.com/projecteru2/core/log" ) var ( @@ -19,7 +19,7 @@ func InitClient(config *types.Config, nodeIP string) { var err error client, err = New(config, nodeIP) if err != nil { - log.Errorf("[GetDockerClient] failed to make docker client, err: %s", err) + log.Error(nil, err, "[GetDockerClient] failed to make docker client") //nolint } }) } diff --git a/runtime/docker/docker.go b/runtime/docker/docker.go index 5f2e88b..33e4bd7 100644 --- a/runtime/docker/docker.go +++ b/runtime/docker/docker.go @@ -24,9 +24,9 @@ import ( engineapi "github.com/docker/docker/client" "github.com/docker/docker/pkg/stdcopy" "github.com/docker/go-units" + "github.com/projecteru2/core/log" "github.com/shirou/gopsutil/cpu" "github.com/shirou/gopsutil/mem" - log "github.com/sirupsen/logrus" ) // Docker . @@ -56,10 +56,10 @@ func New(config *types.Config, nodeIP string) (*Docker, error) { transfers: utils.NewHashBackends(config.Metrics.Transfers), } - log.Infof("[NewDocker] Host IP %s", d.nodeIP) + log.Infof(nil, "[NewDocker] Host IP %s", d.nodeIP) //nolint var err error if d.client, err = utils.MakeDockerClient(config); err != nil { - log.Errorf("[NewDocker] failed to make docker client, err: %v", err) + log.Error(nil, err, "[NewDocker] failed to make docker client") //nolint return nil, err } @@ -71,13 +71,13 @@ func New(config *types.Config, nodeIP string) (*Docker, error) { if err != nil { return nil, err } - log.Infof("[NewDocker] Host has %d cpus", len(cpus)) + log.Infof(nil, "[NewDocker] Host has %d cpus", len(cpus)) //nolint memory, err := mem.VirtualMemory() if err != nil { return nil, err } - log.Infof("[NewDocker] Host has %d memory", memory.Total) + log.Infof(nil, "[NewDocker] Host has %d memory", memory.Total) //nolint d.cpuCore = float64(len(cpus)) d.memory = int64(memory.Total) @@ -105,7 +105,7 @@ func (d *Docker) ListWorkloadIDs(ctx context.Context, filters map[string]string) containers, err = d.client.ContainerList(ctx, opts) }) if err != nil { - log.Errorf("[ListWorkloadIDs] failed to list workloads, err: %v", err) + log.Error(ctx, err, "[ListWorkloadIDs] failed to list workloads") return nil, err } @@ -125,7 +125,7 @@ func (d *Docker) AttachWorkload(ctx context.Context, ID string) (io.Reader, io.R Stderr: true, }) if err != nil && err != httputil.ErrPersistEOF { //nolint - log.Errorf("[AttachWorkload] failed to attach workload %v, err: %v", ID, err) + log.Errorf(ctx, err, "[AttachWorkload] failed to attach workload %v", ID) return nil, nil, err } @@ -140,13 +140,13 @@ func (d *Docker) AttachWorkload(ctx context.Context, ID string) (io.Reader, io.R errw.Close() outr.Close() errr.Close() - log.Debugf("[attach] %v buf pipes closed", ID) + log.Debugf(ctx, "[attach] %v buf pipes closed", ID) }() if _, err = stdcopy.StdCopy(outw, errw, resp.Reader); err != nil { - log.Errorf("[attach] attach get stream failed %s", err) + log.Error(ctx, err, "[attach] attach get stream failed") } - log.Infof("[attach] attach workload %s finished", ID) + log.Infof(ctx, "[attach] attach workload %s finished", ID) }) return outr, errr, nil @@ -181,12 +181,12 @@ func (d *Docker) detectWorkload(ctx context.Context, ID string) (*Container, err label := c.Config.Labels if _, ok := label[cluster.ERUMark]; !ok { - return nil, fmt.Errorf("not a eru container %s", ID) + return nil, common.ErrInvaildContainer } // TODO should be removed in the future if d.config.CheckOnlyMine && !utils.UseLabelAsFilter() && !d.checkHostname(c.Config.Env) { - return nil, fmt.Errorf("should ignore this container") + return nil, common.ErrInvaildContainer } // 生成基准 meta @@ -260,13 +260,13 @@ func (d *Docker) Events(ctx context.Context, filters map[string]string) (<-chan func (d *Docker) GetStatus(ctx context.Context, ID string, checkHealth bool) (*types.WorkloadStatus, error) { container, err := d.detectWorkload(ctx, ID) if err != nil { - log.Errorf("[GetStatus] failed to detect workload %v, err: %v", ID, err) + log.Errorf(ctx, err, "[GetStatus] failed to detect workload %v", ID) return nil, err } bytes, err := json.Marshal(container.Labels) if err != nil { - log.Errorf("[GetStatus] failed to marshal labels, err: %v", err) + log.Error(ctx, err, "[GetStatus] failed to marshal labels") return nil, err } @@ -285,7 +285,7 @@ func (d *Docker) GetStatus(ctx context.Context, ID string, checkHealth bool) (*t if checkHealth && container.Running { free, acquired := d.cas.Acquire(container.ID) if !acquired { - return nil, fmt.Errorf("[GetStatus] failed to get the lock") + return nil, common.ErrGetLockFailed } defer free() status.Healthy = container.CheckHealth(ctx, time.Duration(d.config.HealthCheck.Timeout)*time.Second) @@ -302,7 +302,7 @@ func (d *Docker) GetWorkloadName(ctx context.Context, ID string) (string, error) containerJSON, err = d.client.ContainerInspect(ctx, ID) }) if err != nil { - log.Errorf("[GetWorkloadName] failed to get container by id %v, err: %v", ID, err) + log.Errorf(ctx, err, "[GetWorkloadName] failed to get container by id %v", ID) return "", err } @@ -313,7 +313,7 @@ func (d *Docker) GetWorkloadName(ctx context.Context, ID string) (string, error) func (d *Docker) LogFieldsExtra(ctx context.Context, ID string) (map[string]string, error) { container, err := d.detectWorkload(ctx, ID) if err != nil { - log.Errorf("[LogFieldsExtra] failed to detect container %v, err: %v", ID, err) + log.Errorf(ctx, err, "[LogFieldsExtra] failed to detect container %v", ID) return nil, err } @@ -331,12 +331,12 @@ func (d *Docker) LogFieldsExtra(ctx context.Context, ID string) (map[string]stri func (d *Docker) getContainerStats(ctx context.Context, ID string) (*enginetypes.StatsJSON, error) { rawStat, err := d.client.ContainerStatsOneShot(ctx, ID) if err != nil { - log.Errorf("[getContainerStats] failed to get container %s stats, err: %v", ID, err) + log.Errorf(ctx, err, "[getContainerStats] failed to get container %s stats", ID) return nil, err } b, err := io.ReadAll(rawStat.Body) if err != nil { - log.Errorf("[getContainerStats] failed to read container %s stats, err: %v", ID, err) + log.Errorf(ctx, err, "[getContainerStats] failed to read container %s stats", ID) return nil, err } stats := &enginetypes.StatsJSON{} @@ -358,7 +358,7 @@ func (d *Docker) IsDaemonRunning(ctx context.Context) bool { _, err = d.client.Ping(ctx) }) if err != nil { - log.Errorf("[IsDaemonRunning] connect to docker daemon failed, err: %v", err) + log.Error(ctx, err, "[IsDaemonRunning] connect to docker daemon failed") return false } return true diff --git a/runtime/docker/helpers.go b/runtime/docker/helpers.go index dbff25f..91becbf 100644 --- a/runtime/docker/helpers.go +++ b/runtime/docker/helpers.go @@ -8,7 +8,7 @@ import ( coreutils "github.com/projecteru2/core/utils" enginetypes "github.com/docker/docker/api/types" - log "github.com/sirupsen/logrus" + "github.com/projecteru2/core/log" ) func normalizeEnv(env []string) map[string]string { @@ -56,7 +56,7 @@ func generateContainerMeta(c enginetypes.ContainerJSON, meta *coretypes.LabelMet container.Healthy = !(meta.HealthCheck != nil) } - log.Debugf("[generateContainerMeta] Generate container meta %v %v", container.Name, container.EntryPoint) + log.Debugf(nil, "[generateContainerMeta] Generate container meta %v %v", container.Name, container.EntryPoint) //nolint return container, nil } diff --git a/runtime/docker/metrics.go b/runtime/docker/metrics.go index d330187..9691107 100644 --- a/runtime/docker/metrics.go +++ b/runtime/docker/metrics.go @@ -9,8 +9,8 @@ import ( coreutils "github.com/projecteru2/core/utils" statsdlib "github.com/CMGS/statsd" + "github.com/projecteru2/core/log" "github.com/prometheus/client_golang/prometheus" - log "github.com/sirupsen/logrus" ) var clients *haxmap.Map[string, *MetricsClient] @@ -479,9 +479,9 @@ func (m *MetricsClient) checkConn() error { // We should add an `errorCount` to reconnect when implementing TCP protocol var err error if m.statsdClient, err = statsdlib.New(m.statsd, statsdlib.WithErrorHandler(func(err error) { - log.Errorf("[statsd] Sending statsd failed: %v", err) + log.Error(nil, err, "[statsd] Sending statsd failed") //nolint })); err != nil { - log.Errorf("[statsd] Connect statsd failed: %v", err) + log.Error(nil, err, "[statsd] Connect statsd failed") //nolint return err } return nil diff --git a/runtime/docker/stat.go b/runtime/docker/stat.go index 83b5c88..37db40a 100644 --- a/runtime/docker/stat.go +++ b/runtime/docker/stat.go @@ -7,8 +7,8 @@ import ( "github.com/projecteru2/agent/utils" + "github.com/projecteru2/core/log" "github.com/shirou/gopsutil/net" - log "github.com/sirupsen/logrus" ) // CollectWorkloadMetrics . @@ -22,23 +22,23 @@ func (d *Docker) CollectWorkloadMetrics(ctx context.Context, ID string) { //noli container, err := d.detectWorkload(ctx, ID) if err != nil { - log.Errorf("[CollectWorkloadMetrics] failed to detect container %v, err: %v", ID, err) + log.Errorf(ctx, err, "[CollectWorkloadMetrics] failed to detect container %v", ID) } // init stats containerCPUStats, systemCPUStats, containerNetStats, err := getStats(ctx, container.ID, container.Pid, proc) if err != nil { - log.Errorf("[stat] get %s stats failed %v", container.ID, err) + log.Errorf(ctx, err, "[stat] get %s stats failed", container.ID) return } rawBlkioStats, err := d.getBlkioStats(ctx, container.ID) if err != nil { - log.Errorf("[stat] get %s diskio stats failed %v", container.ID, err) + log.Errorf(ctx, err, "[stat] get %s diskio stats failed", container.ID) return } blkioStats, err := fromEngineBlkioStats(rawBlkioStats) if err != nil { - log.Errorf("[stat] get %s diskio stats failed %v", container.ID, err) + log.Errorf(ctx, err, "[stat] get %s diskio stats failed", container.ID) return } delta := float64(d.config.Metrics.Step) @@ -55,13 +55,13 @@ func (d *Docker) CollectWorkloadMetrics(ctx context.Context, ID string) { //noli hostCPUCount := d.cpuCore * period mClient := NewMetricsClient(addr, hostname, container) - defer log.Infof("[stat] container %s %s metric report stop", container.Name, container.ID) - log.Infof("[stat] container %s %s metric report start", container.Name, container.ID) + defer log.Infof(ctx, "[stat] container %s %s metric report stop", container.Name, container.ID) + log.Infof(ctx, "[stat] container %s %s metric report start", container.Name, container.ID) updateMetrics := func() { newContainer, err := d.detectWorkload(ctx, container.ID) if err != nil { - log.Errorf("[stat] can not refresh container meta %s", container.ID) + log.Errorf(ctx, err, "[stat] can not refresh container meta %s", container.ID) return } containerCPUCount := newContainer.CPUNum * period @@ -69,12 +69,12 @@ func (d *Docker) CollectWorkloadMetrics(ctx context.Context, ID string) { //noli defer cancel() newContainerCPUStats, newSystemCPUStats, newContainerNetStats, err := getStats(timeoutCtx, newContainer.ID, newContainer.Pid, proc) if err != nil { - log.Errorf("[stat] get %s stats failed %v", newContainer.ID, err) + log.Errorf(ctx, err, "[stat] get %s stats failed", newContainer.ID) return } containerMemStats, err := getMemStats(timeoutCtx, newContainer.ID) if err != nil { - log.Errorf("[stat] get %s mem stats failed %v", newContainer.ID, err) + log.Errorf(ctx, err, "[stat] get %s mem stats failed", newContainer.ID) return } @@ -137,15 +137,15 @@ func (d *Docker) CollectWorkloadMetrics(ctx context.Context, ID string) { //noli mClient.DropIn(nic.Name, float64(nic.Dropin-oldNICStats.Dropin)/delta) mClient.DropOut(nic.Name, float64(nic.Dropout-oldNICStats.Dropout)/delta) } - log.Debugf("[stat] start to get blkio stats for %s", container.ID) + log.Debugf(ctx, "[stat] start to get blkio stats for %s", container.ID) newRawBlkioStats, err := d.getBlkioStats(ctx, container.ID) if err != nil { - log.Errorf("[stat] get %s diskio stats failed %v", container.ID, err) + log.Errorf(ctx, err, "[stat] get %s diskio stats failed", container.ID) return } newBlkioStats, err := fromEngineBlkioStats(newRawBlkioStats) if err != nil { - log.Errorf("[stat] get %s diskio stats failed %v", container.ID, err) + log.Errorf(ctx, err, "[stat] get %s diskio stats failed", container.ID) return } for _, entry := range newBlkioStats.IOServiceBytesReadRecursive { @@ -177,7 +177,7 @@ func (d *Docker) CollectWorkloadMetrics(ctx context.Context, ID string) { //noli rawBlkioStats, blkioStats = newRawBlkioStats, newBlkioStats containerCPUStats, systemCPUStats, containerNetStats = newContainerCPUStats, newSystemCPUStats, newContainerNetStats if err := mClient.Send(); err != nil { - log.Errorf("[stat] Send metrics failed %v", err) + log.Error(ctx, err, "[stat] Send metrics failed") } } for { diff --git a/runtime/yavirt/client.go b/runtime/yavirt/client.go index bc0a709..4377442 100644 --- a/runtime/yavirt/client.go +++ b/runtime/yavirt/client.go @@ -5,7 +5,7 @@ import ( "github.com/projecteru2/agent/types" - log "github.com/sirupsen/logrus" + "github.com/projecteru2/core/log" ) var ( @@ -19,7 +19,7 @@ func InitClient(config *types.Config) { var err error yavirt, err = New(config) if err != nil { - log.Errorf("[InitClient] failed to create yavirt client, err: %v", err) + log.Error(nil, err, "[InitClient] failed to create yavirt client") //nolint } }) } diff --git a/runtime/yavirt/guest.go b/runtime/yavirt/guest.go index 6c8f20d..793d00d 100644 --- a/runtime/yavirt/guest.go +++ b/runtime/yavirt/guest.go @@ -9,7 +9,7 @@ import ( "github.com/projecteru2/agent/utils" - log "github.com/sirupsen/logrus" + "github.com/projecteru2/core/log" ) // LabelMeta . @@ -60,7 +60,7 @@ func (g *Guest) CheckHealth(ctx context.Context, timeout time.Duration) bool { hcm := &healthCheckMeta{} err := json.Unmarshal([]byte(meta), hcm) if err != nil { - log.Errorf("[CheckHealth] invalid json format, guest %v, meta %v, err %v", g.ID, meta, err) + log.Error(ctx, err, "[CheckHealth] invalid json format, guest %v, meta %v", g.ID, meta) return } g.HealthCheck = hcm.HealthCheck diff --git a/runtime/yavirt/yavirt.go b/runtime/yavirt/yavirt.go index 08c2236..cdd434a 100644 --- a/runtime/yavirt/yavirt.go +++ b/runtime/yavirt/yavirt.go @@ -3,7 +3,6 @@ package yavirt import ( "context" "encoding/json" - "fmt" "io" "regexp" "strings" @@ -17,7 +16,7 @@ import ( "github.com/projecteru2/libyavirt/client" yavirttypes "github.com/projecteru2/libyavirt/types" - log "github.com/sirupsen/logrus" + "github.com/projecteru2/core/log" ) // Yavirt . @@ -43,7 +42,7 @@ func New(config *types.Config) (*Yavirt, error) { for _, expr := range y.config.Yavirt.SkipGuestReportRegexps { reg, err := regexp.Compile(expr) if err != nil { - log.Errorf("[NewYavirt] failed to compile regexp %v, err: %v", expr, err) + log.Errorf(nil, err, "[NewYavirt] failed to compile regexp %v", expr) //nolint return nil, err } y.skipRegexp = append(y.skipRegexp, reg) @@ -65,7 +64,7 @@ func (y *Yavirt) needSkip(ID string) bool { // detectWorkload detects a workload by ID func (y *Yavirt) detectWorkload(ctx context.Context, ID string) (*Guest, error) { if y.needSkip(ID) { - return nil, fmt.Errorf("should skip this vm") + return nil, common.ErrInvaildVM } var guest yavirttypes.Guest @@ -76,17 +75,17 @@ func (y *Yavirt) detectWorkload(ctx context.Context, ID string) (*Guest, error) }) if err != nil { - log.Errorf("[detectWorkload] failed to detect workload %v, err: %v", ID, err) + log.Errorf(ctx, err, "[detectWorkload] failed to detect workload %v", ID) return nil, err } if _, ok := guest.Labels[cluster.ERUMark]; !ok { - return nil, fmt.Errorf("not a eru vm %s", ID) + return nil, common.ErrInvaildVM } if y.config.CheckOnlyMine && y.config.HostName != guest.Hostname { - log.Debugf("[detectWorkload] guest's hostname is %s instead of %s", guest.Hostname, y.config.HostName) - return nil, fmt.Errorf("should ignore this vm") + log.Debugf(ctx, "[detectWorkload] guest's hostname is %s instead of %s", guest.Hostname, y.config.HostName) + return nil, common.ErrInvaildVM } return &Guest{ @@ -125,7 +124,7 @@ func (y *Yavirt) ListWorkloadIDs(ctx context.Context, filters map[string]string) ids, err = y.client.GetGuestIDList(ctx, yavirttypes.GetGuestIDListReq{Filters: filters}) }) if err != nil && !strings.Contains(err.Error(), "key not exists") { - log.Errorf("[ListWorkloadIDs] failed to get workload ids, err: %v", err) + log.Error(ctx, err, "[ListWorkloadIDs] failed to get workload ids") return nil, err } return ids, nil @@ -166,13 +165,13 @@ func (y *Yavirt) Events(ctx context.Context, filters map[string]string) (<-chan func (y *Yavirt) GetStatus(ctx context.Context, ID string, checkHealth bool) (*types.WorkloadStatus, error) { guest, err := y.detectWorkload(ctx, ID) if err != nil { - log.Errorf("[GetStatus] failed to get guest %v status, err: %v", ID, err) + log.Errorf(ctx, err, "[GetStatus] failed to get guest %v status", ID) return nil, err } bytes, err := json.Marshal(guest.Labels) if err != nil { - log.Errorf("[GetStatus] failed to marshal labels, err: %v", err) + log.Error(ctx, err, "[GetStatus] failed to marshal labels") return nil, err } @@ -188,7 +187,7 @@ func (y *Yavirt) GetStatus(ctx context.Context, ID string, checkHealth bool) (*t if checkHealth && guest.Running { free, acquired := y.cas.Acquire(guest.ID) if !acquired { - return nil, fmt.Errorf("[GetStatus] failed to get the lock") + return nil, common.ErrGetLockFailed } defer free() status.Healthy = guest.CheckHealth(ctx, time.Duration(y.config.HealthCheck.Timeout)*time.Second) @@ -214,7 +213,7 @@ func (y *Yavirt) IsDaemonRunning(ctx context.Context) bool { _, err = y.client.Info(ctx) }) if err != nil { - log.Debugf("[IsDaemonRunning] connect to yavirt daemon failed, err: %v", err) + log.Debugf(ctx, "[IsDaemonRunning] connect to yavirt daemon failed, err: %v", err) return false } return true diff --git a/store/core/client.go b/store/core/client.go index 2a62501..5fbb34c 100644 --- a/store/core/client.go +++ b/store/core/client.go @@ -10,7 +10,7 @@ import ( pb "github.com/projecteru2/core/rpc/gen" "github.com/patrickmn/go-cache" - log "github.com/sirupsen/logrus" + "github.com/projecteru2/core/log" ) // Store use core to store meta @@ -49,7 +49,7 @@ func Init(ctx context.Context, config *types.Config) { var err error coreStore, err = New(ctx, config) if err != nil { - log.Errorf("[Init] failed to create core store, err: %v", err) + log.Error(ctx, err, "[Init] failed to create core store") return } }) diff --git a/store/core/node.go b/store/core/node.go index a36268e..7728238 100644 --- a/store/core/node.go +++ b/store/core/node.go @@ -7,8 +7,8 @@ import ( "github.com/projecteru2/agent/types" "github.com/projecteru2/agent/utils" + "github.com/projecteru2/core/log" pb "github.com/projecteru2/core/rpc/gen" - log "github.com/sirupsen/logrus" ) // GetNode return a node by core @@ -150,7 +150,7 @@ func (c *Store) listPodeNodes(ctx context.Context, opt *pb.ListNodesOptions) (ch node, err := stream.Recv() if err != nil { if err != io.EOF { //nolint:nolintlint - log.Errorf("[listPodeNodes] get node stream failed %v", err) + log.Error(ctx, err, "[listPodeNodes] get node stream failed") } return } diff --git a/store/mocks/fake.go b/store/mocks/fake.go index 8f0d9c9..5fd70bf 100644 --- a/store/mocks/fake.go +++ b/store/mocks/fake.go @@ -5,7 +5,7 @@ import ( "sync" "github.com/alphadose/haxmap" - log "github.com/sirupsen/logrus" + "github.com/projecteru2/core/log" "github.com/stretchr/testify/mock" "github.com/projecteru2/agent/common" @@ -59,7 +59,7 @@ func NewFakeStore() store.Store { } }, nil) m.On("SetNodeStatus", mock.Anything, mock.Anything).Return(func(ctx context.Context, ttl int64) error { - log.Infof("[MockStore] set node status\n") + log.Infof(ctx, "[MockStore] set node status\n") nodename := "fake" m.Lock() defer m.Unlock() @@ -88,7 +88,7 @@ func NewFakeStore() store.Store { } }, nil) m.On("SetWorkloadStatus", mock.Anything, mock.Anything, mock.Anything).Return(func(ctx context.Context, status *types.WorkloadStatus, ttl int64) error { - log.Infof("[MockStore] set workload status: %+v\n", status) + log.Infof(ctx, "[MockStore] set workload status: %+v\n", status) m.workloadStatus.Set(status.ID, status) return nil }) diff --git a/types/config.go b/types/config.go index ccefa82..521706e 100644 --- a/types/config.go +++ b/types/config.go @@ -3,12 +3,13 @@ package types import ( "bufio" "bytes" + "fmt" "os" "time" coretypes "github.com/projecteru2/core/types" - log "github.com/sirupsen/logrus" + "github.com/projecteru2/core/log" cli "github.com/urfave/cli/v2" "gopkg.in/yaml.v2" ) @@ -85,7 +86,7 @@ func (config *Config) Prepare(c *cli.Context) { } else { hostname, err := os.Hostname() if err != nil { - log.Fatal(err) + log.Fatalf(c.Context, err, "Get hostname failed %v", err) } config.HostName = hostname } @@ -143,10 +144,10 @@ func (config *Config) Prepare(c *cli.Context) { } // validate if config.PidFile == "" { - log.Fatal("need to set pidfile") + config.PidFile = "./agent.pid" } if config.HealthCheck.Interval == 0 { - log.Fatal("healthcheck.interval == 0, this is not allowed") + config.HealthCheck.Interval = 60 } if config.HealthCheck.Timeout == 0 { config.HealthCheck.Timeout = 10 @@ -160,13 +161,13 @@ func (config *Config) Prepare(c *cli.Context) { func (config *Config) Print() { bs, err := yaml.Marshal(config) if err != nil { - log.Fatalf("[config] print config failed %v", err) + log.Fatalf(nil, err, "[config] print config failed %v", err) //nolint } - log.Info("---- current config ----") + fmt.Println("---- current config ----") scanner := bufio.NewScanner(bytes.NewBuffer(bs)) for scanner.Scan() { - log.Info(scanner.Text()) + fmt.Println(scanner.Text()) } - log.Info("------------------------") + fmt.Println("------------------------") } diff --git a/utils/check.go b/utils/check.go index 6fafcbc..b390d97 100644 --- a/utils/check.go +++ b/utils/check.go @@ -6,16 +6,16 @@ import ( "net/http" "time" - log "github.com/sirupsen/logrus" + "github.com/projecteru2/core/log" ) // CheckHTTP 检查一个workload的所有URL // CheckHTTP 事实上一般也就一个 func CheckHTTP(ctx context.Context, ID string, backends []string, code int, timeout time.Duration) bool { for _, backend := range backends { - log.Debugf("[checkHTTP] Check health via http: workload %s, url %s, expect code %d", ID, backend, code) + log.Debugf(ctx, "[checkHTTP] Check health via http: workload %s, url %s, expect code %d", ID, backend, code) if !checkOneURL(ctx, backend, code, timeout) { - log.Infof("[checkHTTP] Check health failed via http: workload %s, url %s, expect code %d", ID, backend, code) + log.Infof(ctx, "[checkHTTP] Check health failed via http: workload %s, url %s, expect code %d", ID, backend, code) return false } } @@ -26,10 +26,10 @@ func CheckHTTP(ctx context.Context, ID string, backends []string, code int, time // 这里不支持ctx? func CheckTCP(ID string, backends []string, timeout time.Duration) bool { for _, backend := range backends { - log.Debugf("[checkTCP] Check health via tcp: workload %s, backend %s", ID, backend) + log.Debugf(nil, "[checkTCP] Check health via tcp: workload %s, backend %s", ID, backend) //nolint conn, err := net.DialTimeout("tcp", backend, timeout) if err != nil { - log.Debugf("[checkTCP] Check health failed via tcp: workload %s, backend %s", ID, backend) + log.Debugf(nil, "[checkTCP] Check health failed via tcp: workload %s, backend %s", ID, backend) //nolint return false } conn.Close() @@ -68,7 +68,7 @@ func checkOneURL(ctx context.Context, url string, expectedCode int, timeout time resp, err = get(ctx, nil, url) //nolint }) if err != nil { - log.Warnf("[checkOneURL] Error when checking %s, %s", url, err.Error()) + log.Warnf(ctx, "[checkOneURL] Error when checking %s, %s", url, err.Error()) return false } defer resp.Body.Close() @@ -76,7 +76,7 @@ func checkOneURL(ctx context.Context, url string, expectedCode int, timeout time return resp.StatusCode < 500 && resp.StatusCode >= 200 } if resp.StatusCode != expectedCode { - log.Infof("[checkOneURL] Error when checking %s, expect %d, got %d", url, expectedCode, resp.StatusCode) + log.Infof(ctx, "[checkOneURL] Error when checking %s, expect %d, got %d", url, expectedCode, resp.StatusCode) } return resp.StatusCode == expectedCode } diff --git a/utils/retry.go b/utils/retry.go index 669c0a7..6626618 100644 --- a/utils/retry.go +++ b/utils/retry.go @@ -4,7 +4,7 @@ import ( "context" "time" - log "github.com/sirupsen/logrus" + "github.com/projecteru2/core/log" ) // RetryTask . @@ -32,7 +32,7 @@ func NewRetryTask(ctx context.Context, maxAttempts int, f func() error) *RetryTa // Run start running retry task func (r *RetryTask) Run() error { - log.Debug("[RetryTask] start") + log.Debug(nil, "[RetryTask] start") //nolint defer r.Stop() var err error @@ -43,14 +43,14 @@ func (r *RetryTask) Run() error { for i := 0; i < r.MaxAttempts; i++ { select { case <-r.ctx.Done(): - log.Debug("[RetryTask] abort") + log.Debug(nil, "[RetryTask] abort") //nolint return r.ctx.Err() case <-timer.C: err = r.Func() if err == nil { return nil } - log.Debugf("[RetryTask] will retry after %v seconds", interval) + log.Debugf(nil, "[RetryTask] will retry after %v seconds", interval) //nolint timer.Reset(time.Duration(interval) * time.Second) interval *= 2 } @@ -60,7 +60,7 @@ func (r *RetryTask) Run() error { // Stop stops running task func (r *RetryTask) Stop() { - log.Debug("[RetryTask] stop") + log.Debug(nil, "[RetryTask] stop") //nolint r.cancel() } diff --git a/utils/utils.go b/utils/utils.go index 6af4112..6efc189 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -20,7 +20,7 @@ import ( yavirtclient "github.com/projecteru2/libyavirt/client" engineapi "github.com/docker/docker/client" - log "github.com/sirupsen/logrus" + "github.com/projecteru2/core/log" ) var dockerized bool @@ -40,7 +40,7 @@ func MakeYavirtClient(config *types.Config) (yavirtclient.Client, error) { // WritePid write pid func WritePid(path string) { if err := os.WriteFile(path, []byte(strconv.Itoa(os.Getpid())), 0600); err != nil { - log.Panicf("Save pid file failed %s", err) + log.Fatalf(nil, err, "Save pid file failed %s", err) //nolint } }