diff options
author | Wolfy-J <[email protected]> | 2018-09-26 22:30:53 +0300 |
---|---|---|
committer | Wolfy-J <[email protected]> | 2018-09-26 22:30:53 +0300 |
commit | a206fc3270ea6e469b1704f15f2f15a9f6a14bbd (patch) | |
tree | 68bc61c49f84db861976659abed3784bb7e304a9 /cmd | |
parent | d24c43aaeff897394eca140478c73dd146f8710a (diff) |
improved debug handlers
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/rr/cmd/root.go | 2 | ||||
-rw-r--r-- | cmd/rr/debug/debugger.go | 120 | ||||
-rw-r--r-- | cmd/rr/http/debug.go | 84 | ||||
-rw-r--r-- | cmd/rr/http/reset.go | 12 | ||||
-rw-r--r-- | cmd/rr/http/workers.go | 12 | ||||
-rw-r--r-- | cmd/util/client.go | 18 | ||||
-rw-r--r-- | cmd/util/cprint.go (renamed from cmd/rr/util/cprint.go) | 0 | ||||
-rw-r--r-- | cmd/util/debug.go | 57 | ||||
-rw-r--r-- | cmd/util/table.go (renamed from cmd/rr/util/list.go) | 4 |
9 files changed, 159 insertions, 150 deletions
diff --git a/cmd/rr/cmd/root.go b/cmd/rr/cmd/root.go index aabfcdea..e71ce0c4 100644 --- a/cmd/rr/cmd/root.go +++ b/cmd/rr/cmd/root.go @@ -24,7 +24,7 @@ import ( "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/spf13/viper" - "github.com/spiral/roadrunner/cmd/rr/util" + "github.com/spiral/roadrunner/cmd/util" "github.com/spiral/roadrunner/service" "os" "path/filepath" diff --git a/cmd/rr/debug/debugger.go b/cmd/rr/debug/debugger.go deleted file mode 100644 index 5b1d586b..00000000 --- a/cmd/rr/debug/debugger.go +++ /dev/null @@ -1,120 +0,0 @@ -package debug - -import ( - "fmt" - "github.com/sirupsen/logrus" - "github.com/spiral/roadrunner" - "github.com/spiral/roadrunner/cmd/rr/util" - rrhttp "github.com/spiral/roadrunner/service/http" - "net/http" - "strings" -) - -// Listener creates new debug listener. -func Listener(logger *logrus.Logger) func(event int, ctx interface{}) { - return (&debugger{logger}).listener -} - -// listener provide debug callback for system events. With colors! -type debugger struct{ logger *logrus.Logger } - -// listener listens to http events and generates nice looking output. -func (s *debugger) listener(event int, ctx interface{}) { - // http events - switch event { - case rrhttp.EventResponse: - e := ctx.(*rrhttp.ResponseEvent) - s.logger.Info(util.Sprintf( - "<cyan+h>%s</reset> %s <white+hb>%s</reset> %s", - e.Request.RemoteAddr, - statusColor(e.Response.Status), - e.Request.Method, - e.Request.URI, - )) - case rrhttp.EventError: - e := ctx.(*rrhttp.ErrorEvent) - - if _, ok := e.Error.(roadrunner.JobError); ok { - s.logger.Info(util.Sprintf( - "%s <white+hb>%s</reset> %s", - statusColor(500), - e.Request.Method, - uri(e.Request), - )) - } else { - s.logger.Info(util.Sprintf( - "%s <white+hb>%s</reset> %s <red>%s</reset>", - statusColor(500), - e.Request.Method, - uri(e.Request), - e.Error, - )) - } - } - - switch event { - case roadrunner.EventWorkerKill: - w := ctx.(*roadrunner.Worker) - s.logger.Warning(util.Sprintf( - "<white+hb>worker.%v</reset> <yellow>killed</red>", - *w.Pid, - )) - case roadrunner.EventWorkerError: - err := ctx.(roadrunner.WorkerError) - s.logger.Error(util.Sprintf( - "<white+hb>worker.%v</reset> <red>%s</reset>", - *err.Worker.Pid, - err.Caused, - )) - } - - // outputs - switch event { - case roadrunner.EventStderrOutput: - s.logger.Warning(util.Sprintf( - "<yellow>%s</reset>", - strings.Trim(string(ctx.([]byte)), "\r\n"), - )) - } - - // rr server events - switch event { - case roadrunner.EventServerFailure: - s.logger.Error(util.Sprintf("<red>server is dead</reset>")) - } - - // pool events - switch event { - case roadrunner.EventPoolConstruct: - s.logger.Debug(util.Sprintf("<cyan>new worker pool</reset>")) - case roadrunner.EventPoolError: - s.logger.Error(util.Sprintf("<red>%s</reset>", ctx)) - } - - //s.logger.Warning(event, ctx) -} - -func statusColor(status int) string { - if status < 300 { - return util.Sprintf("<green>%v</reset>", status) - } - - if status < 400 { - return util.Sprintf("<cyan>%v</reset>", status) - } - - if status < 500 { - return util.Sprintf("<yellow>%v</reset>", status) - } - - return util.Sprintf("<red>%v</reset>", status) -} - -// uri fetches full uri from request in a form of string (including https scheme if TLS connection is enabled). -func uri(r *http.Request) string { - if r.TLS != nil { - return fmt.Sprintf("https://%s%s", r.Host, r.URL.String()) - } - - return fmt.Sprintf("http://%s%s", r.Host, r.URL.String()) -} diff --git a/cmd/rr/http/debug.go b/cmd/rr/http/debug.go index f69e10a8..53980303 100644 --- a/cmd/rr/http/debug.go +++ b/cmd/rr/http/debug.go @@ -1,20 +1,90 @@ package http import ( - rr "github.com/spiral/roadrunner/cmd/rr/cmd" - + "fmt" + "github.com/sirupsen/logrus" "github.com/spf13/cobra" - "github.com/spiral/roadrunner/cmd/rr/debug" - "github.com/spiral/roadrunner/service/http" + "github.com/spiral/roadrunner" + rr "github.com/spiral/roadrunner/cmd/rr/cmd" + "github.com/spiral/roadrunner/cmd/util" + rrhttp "github.com/spiral/roadrunner/service/http" + "net/http" ) func init() { cobra.OnInitialize(func() { if rr.Debug { - svc, _ := rr.Container.Get(http.ID) - if svc, ok := svc.(*http.Service); ok { - svc.AddListener(debug.Listener(rr.Logger)) + svc, _ := rr.Container.Get(rrhttp.ID) + if svc, ok := svc.(*rrhttp.Service); ok { + svc.AddListener((&debugger{logger: rr.Logger}).listener) } } }) } + +// listener provide debug callback for system events. With colors! +type debugger struct{ logger *logrus.Logger } + +// listener listens to http events and generates nice looking output. +func (s *debugger) listener(event int, ctx interface{}) { + if util.LogEvent(s.logger, event, ctx) { + // handler by default debug package + return + } + + // http events + switch event { + case rrhttp.EventResponse: + e := ctx.(*rrhttp.ResponseEvent) + s.logger.Info(util.Sprintf( + "<cyan+h>%s</reset> %s <white+hb>%s</reset> %s", + e.Request.RemoteAddr, + statusColor(e.Response.Status), + e.Request.Method, + e.Request.URI, + )) + case rrhttp.EventError: + e := ctx.(*rrhttp.ErrorEvent) + + if _, ok := e.Error.(roadrunner.JobError); ok { + s.logger.Info(util.Sprintf( + "%s <white+hb>%s</reset> %s", + statusColor(500), + e.Request.Method, + uri(e.Request), + )) + } else { + s.logger.Info(util.Sprintf( + "%s <white+hb>%s</reset> %s <red>%s</reset>", + statusColor(500), + e.Request.Method, + uri(e.Request), + e.Error, + )) + } + } +} + +func statusColor(status int) string { + if status < 300 { + return util.Sprintf("<green>%v</reset>", status) + } + + if status < 400 { + return util.Sprintf("<cyan>%v</reset>", status) + } + + if status < 500 { + return util.Sprintf("<yellow>%v</reset>", status) + } + + return util.Sprintf("<red>%v</reset>", status) +} + +func uri(r *http.Request) string { + if r.TLS != nil { + return fmt.Sprintf("https://%s%s", r.Host, r.URL.String()) + } + + return fmt.Sprintf("http://%s%s", r.Host, r.URL.String()) +} diff --git a/cmd/rr/http/reset.go b/cmd/rr/http/reset.go index f1129eef..3d84dbce 100644 --- a/cmd/rr/http/reset.go +++ b/cmd/rr/http/reset.go @@ -21,12 +21,9 @@ package http import ( - "errors" "github.com/spf13/cobra" rr "github.com/spiral/roadrunner/cmd/rr/cmd" - "github.com/spiral/roadrunner/cmd/rr/util" - "github.com/spiral/roadrunner/service" - "github.com/spiral/roadrunner/service/rpc" + "github.com/spiral/roadrunner/cmd/util" ) func init() { @@ -38,12 +35,7 @@ func init() { } func reloadHandler(cmd *cobra.Command, args []string) error { - svc, st := rr.Container.Get(rpc.ID) - if st < service.StatusOK { - return errors.New("RPC service is not configured") - } - - client, err := svc.(*rpc.Service).Client() + client, err := util.RPCClient(rr.Container) if err != nil { return err } diff --git a/cmd/rr/http/workers.go b/cmd/rr/http/workers.go index 3c3e9987..4444b87f 100644 --- a/cmd/rr/http/workers.go +++ b/cmd/rr/http/workers.go @@ -21,14 +21,11 @@ package http import ( - "errors" tm "github.com/buger/goterm" "github.com/spf13/cobra" rr "github.com/spiral/roadrunner/cmd/rr/cmd" - "github.com/spiral/roadrunner/cmd/rr/util" - "github.com/spiral/roadrunner/service" + "github.com/spiral/roadrunner/cmd/util" "github.com/spiral/roadrunner/service/http" - rrpc "github.com/spiral/roadrunner/service/rpc" "net/rpc" "os" "os/signal" @@ -69,12 +66,7 @@ func workersHandler(cmd *cobra.Command, args []string) (err error) { } }() - svc, st := rr.Container.Get(rrpc.ID) - if st < service.StatusOK { - return errors.New("RPC service is not configured") - } - - client, err := svc.(*rrpc.Service).Client() + client, err := util.RPCClient(rr.Container) if err != nil { return err } diff --git a/cmd/util/client.go b/cmd/util/client.go new file mode 100644 index 00000000..ee3414a6 --- /dev/null +++ b/cmd/util/client.go @@ -0,0 +1,18 @@ +package util + +import ( + "errors" + "github.com/spiral/roadrunner/service" + rrpc "github.com/spiral/roadrunner/service/rpc" + "net/rpc" +) + +// RPCClient returns RPC client associated with given roadrunner service container. +func RPCClient(container service.Container) (*rpc.Client, error) { + svc, st := container.Get(rrpc.ID) + if st < service.StatusOK { + return nil, errors.New("RPC service is not configured") + } + + return svc.(*rrpc.Service).Client() +} diff --git a/cmd/rr/util/cprint.go b/cmd/util/cprint.go index 0985de62..0985de62 100644 --- a/cmd/rr/util/cprint.go +++ b/cmd/util/cprint.go diff --git a/cmd/util/debug.go b/cmd/util/debug.go new file mode 100644 index 00000000..7be258ec --- /dev/null +++ b/cmd/util/debug.go @@ -0,0 +1,57 @@ +package util + +import ( + "github.com/sirupsen/logrus" + "github.com/spiral/roadrunner" + "strings" +) + +// LogEvent outputs rr event into given logger and return false if event was not handled. +func LogEvent(logger *logrus.Logger, event int, ctx interface{}) bool { + switch event { + case roadrunner.EventWorkerKill: + w := ctx.(*roadrunner.Worker) + logger.Warning(Sprintf( + "<white+hb>worker.%v</reset> <yellow>killed</red>", + *w.Pid, + )) + return true + case roadrunner.EventWorkerError: + err := ctx.(roadrunner.WorkerError) + logger.Error(Sprintf( + "<white+hb>worker.%v</reset> <red>%s</reset>", + *err.Worker.Pid, + err.Caused, + )) + return true + } + + // outputs + switch event { + case roadrunner.EventStderrOutput: + logger.Warning(Sprintf( + "<yellow>%s</reset>", + strings.Trim(string(ctx.([]byte)), "\r\n"), + )) + return true + } + + // rr server events + switch event { + case roadrunner.EventServerFailure: + logger.Error(Sprintf("<red>server is dead</reset>")) + return true + } + + // pool events + switch event { + case roadrunner.EventPoolConstruct: + logger.Debug(Sprintf("<cyan>new worker pool</reset>")) + return true + case roadrunner.EventPoolError: + logger.Error(Sprintf("<red>%s</reset>", ctx)) + return true + } + + return false +} diff --git a/cmd/rr/util/list.go b/cmd/util/table.go index 4094ce44..565c0679 100644 --- a/cmd/rr/util/list.go +++ b/cmd/util/table.go @@ -1,11 +1,11 @@ package util import ( - rrutil "github.com/spiral/roadrunner/util" + "github.com/dustin/go-humanize" "github.com/olekukonko/tablewriter" + rrutil "github.com/spiral/roadrunner/util" "os" "strconv" - "github.com/dustin/go-humanize" "time" ) |