diff options
-rw-r--r-- | cmd/rr/debug/debugger.go | 48 | ||||
-rw-r--r-- | service/http/handler.go | 29 |
2 files changed, 51 insertions, 26 deletions
diff --git a/cmd/rr/debug/debugger.go b/cmd/rr/debug/debugger.go index 533a5947..fdf3c1be 100644 --- a/cmd/rr/debug/debugger.go +++ b/cmd/rr/debug/debugger.go @@ -4,8 +4,10 @@ import ( "github.com/sirupsen/logrus" "github.com/spiral/roadrunner" "github.com/spiral/roadrunner/cmd/rr/utils" - "github.com/spiral/roadrunner/service/http" + rrhttp "github.com/spiral/roadrunner/service/http" "strings" + "fmt" + "net/http" ) // Listener creates new debug listener. @@ -20,21 +22,32 @@ type debugger struct{ logger *logrus.Logger } func (s *debugger) listener(event int, ctx interface{}) { // http events switch event { - case http.EventResponse: - log := ctx.(*http.Event) - s.logger.Info(utils.Sprintf("%s <white+hb>%s</reset> %s", statusColor(log.Status), log.Method, log.URI)) - case http.EventError: - log := ctx.(*http.Event) - - if _, ok := log.Error.(roadrunner.JobError); ok { - s.logger.Info(utils.Sprintf("%s <white+hb>%s</reset> %s", statusColor(log.Status), log.Method, log.URI)) + case rrhttp.EventResponse: + e := ctx.(*rrhttp.ResponseEvent) + s.logger.Info(utils.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(utils.Sprintf( + "%s <white+hb>%s</reset> %s", + statusColor(500), + e.Request.Method, + uri(e.Request), + )) } else { s.logger.Info(utils.Sprintf( "%s <white+hb>%s</reset> %s <red>%s</reset>", - statusColor(log.Status), - log.Method, - log.URI, - log.Error, + statusColor(500), + e.Request.Method, + uri(e.Request), + e.Error, )) } } @@ -93,3 +106,12 @@ func statusColor(status int) string { return utils.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()) +}
\ No newline at end of file diff --git a/service/http/handler.go b/service/http/handler.go index 6f2617b1..9e67e5b4 100644 --- a/service/http/handler.go +++ b/service/http/handler.go @@ -9,26 +9,29 @@ import ( ) const ( - // EventResponse thrown after the request been processed. See Event as payload. + // EventResponse thrown after the request been processed. See ErrorEvent as payload. EventResponse = iota + 500 // EventError thrown on any non job error provided by road runner server. EventError ) -// Event represents singular http response event. -type Event struct { - // Method of the request. - Method string +// ErrorEvent represents singular http error event. +type ErrorEvent struct { + // Request contains client request, must not be stored. + Request *http.Request - // URI requested by the client. - URI string + // Error - associated error, if any. + Error error +} - // Status is response status. - Status int +// ResponseEvent represents singular http response event. +type ResponseEvent struct { + // Request contains client request, must not be stored. + Request *Request - // Associated error, if any. - Error error + // Response contains service response. + Response *Response } // Handler serves http connections to underlying PHP application using PSR-7 protocol. Context will include request headers, @@ -99,7 +102,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { // handleError sends error. func (h *Handler) handleError(w http.ResponseWriter, r *http.Request, err error) { - h.throw(EventError, &Event{Method: r.Method, URI: uri(r), Status: 500, Error: err}) + h.throw(EventError, &ErrorEvent{Request: r, Error: err}) w.WriteHeader(500) w.Write([]byte(err.Error())) @@ -107,7 +110,7 @@ func (h *Handler) handleError(w http.ResponseWriter, r *http.Request, err error) // handleResponse triggers response event. func (h *Handler) handleResponse(req *Request, resp *Response) { - h.throw(EventResponse, &Event{Method: req.Method, URI: req.URI, Status: resp.Status}) + h.throw(EventResponse, &ResponseEvent{Request: req, Response: resp}) } // throw invokes event srv if any. |