mirror of
https://github.com/hoernschen/dendrite.git
synced 2025-08-01 13:52:46 +00:00
Update github.com/matrix-org/util for request context fix for logging
This commit is contained in:
parent
04f3c154b8
commit
042d636e34
2 changed files with 23 additions and 14 deletions
33
vendor/src/github.com/matrix-org/util/json.go
vendored
33
vendor/src/github.com/matrix-org/util/json.go
vendored
|
@ -93,23 +93,32 @@ func Protect(handler http.HandlerFunc) http.HandlerFunc {
|
|||
}
|
||||
}
|
||||
|
||||
// RequestWithLogging sets up standard logging for http.Requests.
|
||||
// http.Requests will have a logger (with a request ID/method/path logged) attached to the Context.
|
||||
// This can be accessed via GetLogger(Context).
|
||||
func RequestWithLogging(req *http.Request) *http.Request {
|
||||
reqID := RandomString(12)
|
||||
// Set a Logger and request ID on the context
|
||||
ctx := context.WithValue(req.Context(), ctxValueLogger, log.WithFields(log.Fields{
|
||||
"req.method": req.Method,
|
||||
"req.path": req.URL.Path,
|
||||
"req.id": reqID,
|
||||
}))
|
||||
ctx = context.WithValue(ctx, ctxValueRequestID, reqID)
|
||||
req = req.WithContext(ctx)
|
||||
|
||||
logger := GetLogger(req.Context())
|
||||
logger.Print("Incoming request")
|
||||
|
||||
return req
|
||||
}
|
||||
|
||||
// MakeJSONAPI creates an HTTP handler which always responds to incoming requests with JSON responses.
|
||||
// Incoming http.Requests will have a logger (with a request ID/method/path logged) attached to the Context.
|
||||
// This can be accessed via GetLogger(Context).
|
||||
func MakeJSONAPI(handler JSONRequestHandler) http.HandlerFunc {
|
||||
return Protect(func(w http.ResponseWriter, req *http.Request) {
|
||||
reqID := RandomString(12)
|
||||
// Set a Logger and request ID on the context
|
||||
ctx := context.WithValue(req.Context(), ctxValueLogger, log.WithFields(log.Fields{
|
||||
"req.method": req.Method,
|
||||
"req.path": req.URL.Path,
|
||||
"req.id": reqID,
|
||||
}))
|
||||
ctx = context.WithValue(ctx, ctxValueRequestID, reqID)
|
||||
req = req.WithContext(ctx)
|
||||
|
||||
logger := GetLogger(req.Context())
|
||||
logger.Print("Incoming request")
|
||||
req = RequestWithLogging(req)
|
||||
|
||||
if req.Method == "OPTIONS" {
|
||||
SetCORSHeaders(w)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue