Add func to debug requests
- logging middleware - response writer with status code Signed-off-by: Evgeniy Kulikov <kim@nspcc.ru>
This commit is contained in:
parent
5df041f7d7
commit
447a255d18
1 changed files with 37 additions and 2 deletions
|
@ -79,6 +79,11 @@ type (
|
||||||
|
|
||||||
// mimeType represents various MIME type used API responses.
|
// mimeType represents various MIME type used API responses.
|
||||||
mimeType string
|
mimeType string
|
||||||
|
|
||||||
|
logResponseWriter struct {
|
||||||
|
http.ResponseWriter
|
||||||
|
statusCode int
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -93,6 +98,13 @@ const (
|
||||||
mimeXML mimeType = "application/xml"
|
mimeXML mimeType = "application/xml"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var _ = logErrorResponse
|
||||||
|
|
||||||
|
func (lrw *logResponseWriter) WriteHeader(code int) {
|
||||||
|
lrw.statusCode = code
|
||||||
|
lrw.ResponseWriter.WriteHeader(code)
|
||||||
|
}
|
||||||
|
|
||||||
func setRequestID(h http.Handler) http.Handler {
|
func setRequestID(h http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
// generate random UUIDv4
|
// generate random UUIDv4
|
||||||
|
@ -114,6 +126,24 @@ func setRequestID(h http.Handler) http.Handler {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func logErrorResponse(l *zap.Logger) mux.MiddlewareFunc {
|
||||||
|
return func(h http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
lw := &logResponseWriter{ResponseWriter: w}
|
||||||
|
|
||||||
|
// pass execution:
|
||||||
|
h.ServeHTTP(lw, r)
|
||||||
|
|
||||||
|
// Ignore <300 status codes
|
||||||
|
if lw.statusCode >= http.StatusMultipleChoices {
|
||||||
|
l.Error("something went wrong",
|
||||||
|
zap.Int("status", lw.statusCode),
|
||||||
|
zap.String("method", mux.CurrentRoute(r).GetName()))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func GetRequestID(v interface{}) string {
|
func GetRequestID(v interface{}) string {
|
||||||
switch t := v.(type) {
|
switch t := v.(type) {
|
||||||
case context.Context:
|
case context.Context:
|
||||||
|
@ -128,8 +158,13 @@ func GetRequestID(v interface{}) string {
|
||||||
func Attach(r *mux.Router, m MaxClients, h Handler, center *auth.Center, log *zap.Logger) {
|
func Attach(r *mux.Router, m MaxClients, h Handler, center *auth.Center, log *zap.Logger) {
|
||||||
api := r.PathPrefix(SlashSeparator).Subrouter()
|
api := r.PathPrefix(SlashSeparator).Subrouter()
|
||||||
|
|
||||||
// Attach behaviors: RequestID, ...
|
api.Use(
|
||||||
api.Use(setRequestID)
|
// -- prepare request
|
||||||
|
setRequestID,
|
||||||
|
|
||||||
|
// -- logging error requests
|
||||||
|
// logErrorResponse(log),
|
||||||
|
)
|
||||||
|
|
||||||
// Attach user authentication for all S3 routes.
|
// Attach user authentication for all S3 routes.
|
||||||
AttachUserAuth(api, center, log)
|
AttachUserAuth(api, center, log)
|
||||||
|
|
Loading…
Reference in a new issue