-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add request logging for service bindings (#1278)
* Add request logging for service bindings * workflow fix * Add message if the body is nil
- Loading branch information
1 parent
a40c8fb
commit 81c35e6
Showing
2 changed files
with
45 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package middleware | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func AddRequestLogging(logs logrus.FieldLogger) mux.MiddlewareFunc { | ||
return func(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { | ||
if strings.Contains(req.URL.Path, "service_bindings") { | ||
logs.WithFields(logrus.Fields{ | ||
"method": req.Method, | ||
"url": req.URL.String(), | ||
"query": req.URL.Query(), | ||
"headers": req.Header, | ||
"remote_addr": req.RemoteAddr, | ||
"host": req.Host, | ||
"user_agent": req.UserAgent(), | ||
}).Info("Request details") | ||
|
||
if req.Body == nil { | ||
logs.Info("Request body is nil") | ||
} else { | ||
bodyBytes, err := io.ReadAll(req.Body) | ||
if err != nil { | ||
logs.Error("Failed to read request body:", err) | ||
} else { | ||
logs.WithField("body", string(bodyBytes)).Info("Request body") | ||
|
||
req.Body = io.NopCloser(bytes.NewBuffer(bodyBytes)) | ||
} | ||
} | ||
} | ||
|
||
next.ServeHTTP(w, req) | ||
}) | ||
} | ||
} |