-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
zrouter: Add logging middleware and request-id (#66)
* Add logging middleware * add request-id middleware * fix applyMiddlewares and add request-id * fix go mod
- Loading branch information
1 parent
6f6f7af
commit df4bdff
Showing
7 changed files
with
83 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,32 @@ | ||
package zmiddlewares | ||
|
||
import ( | ||
"github.com/go-chi/chi/v5/middleware" | ||
"github.com/google/uuid" | ||
"net/http" | ||
) | ||
|
||
const ( | ||
RequestIDHeader = "X-Request-ID" | ||
) | ||
|
||
func RequestID() Middleware { | ||
return middleware.RequestID | ||
return requestIDMiddleware | ||
} | ||
|
||
func requestIDMiddleware(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
requestID := r.Header.Get(RequestIDHeader) | ||
if requestID == "" { | ||
requestID = uuid.New().String() | ||
r.Header.Set(RequestIDHeader, requestID) | ||
} | ||
|
||
w.Header().Set(RequestIDHeader, requestID) | ||
rw := &responseWriter{ResponseWriter: w} | ||
next.ServeHTTP(rw, r) | ||
}) | ||
} | ||
|
||
func Logger() Middleware { | ||
return middleware.Logger | ||
return LoggingMiddleware | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package zmiddlewares | ||
|
||
import ( | ||
"bytes" | ||
"go.uber.org/zap" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
func LoggingMiddleware(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
buffer := &bytes.Buffer{} | ||
|
||
rw := &responseWriter{ | ||
ResponseWriter: w, | ||
body: buffer, | ||
} | ||
|
||
start := time.Now() | ||
next.ServeHTTP(rw, r) | ||
duration := time.Since(start) | ||
requestID := r.Header.Get(RequestIDHeader) | ||
|
||
zap.S().Debugf("request_id: %s - Method: %s - URL: %s | Status: %d - Duration: %s - Response Body: %s", | ||
requestID, r.Method, r.URL.String(), rw.status, duration, rw.Body()) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters