Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

enhancement: add client IP address to logs (#511)

Guilherme Oenningc8de9683bb69
diff --git a/app/middlewares/setup.go b/app/middlewares/setup.go
index e529e7e6f..f08625e48 100644
--- a/app/middlewares/setup.go
+++ b/app/middlewares/setup.go
@@ -111,15 +111,16 @@ func WebSetup() web.MiddlewareFunc {
return func(next web.HandlerFunc) web.HandlerFunc {
return func(c web.Context) error {
start := time.Now()
- c.Logger().Debugf("@{HttpMethod:magenta} @{URL:magenta} started", log.Props{
+ c.Logger().Infof("@{HttpMethod:magenta} @{URL:magenta} started for @{ClientIP:magenta}", log.Props{
"HttpMethod": c.Request.Method,
"URL": c.Request.URL.String(),
+ "ClientIP": c.Request.ClientIP,
})
trx, err := c.Engine().Database().Begin()
if err != nil {
err = c.Failure(err)
- c.Logger().Debugf("@{HttpMethod:magenta} @{URL:magenta} finished in @{ElapsedMs:magenta}ms", log.Props{
+ c.Logger().Infof("@{HttpMethod:magenta} @{URL:magenta} finished in @{ElapsedMs:magenta}ms", log.Props{
"HttpMethod": c.Request.Method,
"URL": c.Request.URL.String(),
"ElapsedMs": time.Since(start).Nanoseconds() / int64(time.Millisecond),
@@ -152,7 +153,7 @@ func WebSetup() web.MiddlewareFunc {
}
c.Failure(err)
c.Rollback()
- c.Logger().Debugf("@{HttpMethod:magenta} @{URL:magenta} panicked in @{ElapsedMs:magenta}ms (rolled back)", log.Props{
+ c.Logger().Infof("@{HttpMethod:magenta} @{URL:magenta} panicked in @{ElapsedMs:magenta}ms (rolled back)", log.Props{
"HttpMethod": c.Request.Method,
"URL": c.Request.URL.String(),
"ElapsedMs": time.Since(start).Nanoseconds() / int64(time.Millisecond),
@@ -163,7 +164,7 @@ func WebSetup() web.MiddlewareFunc {
//Execute the chain
if err := next(c); err != nil {
c.Rollback()
- c.Logger().Debugf("@{HttpMethod:magenta} @{URL:magenta} finished in @{ElapsedMs:magenta}ms (rolled back)", log.Props{
+ c.Logger().Infof("@{HttpMethod:magenta} @{URL:magenta} finished in @{ElapsedMs:magenta}ms (rolled back)", log.Props{
"HttpMethod": c.Request.Method,
"URL": c.Request.URL.String(),
"ElapsedMs": time.Since(start).Nanoseconds() / int64(time.Millisecond),
@@ -176,7 +177,7 @@ func WebSetup() web.MiddlewareFunc {
c.Logger().Errorf("Failed to commit request with: @{Error}", log.Props{
"Error": err.Error(),
})
- c.Logger().Debugf("@{HttpMethod:magenta} @{URL:magenta} finished in @{ElapsedMs:magenta}ms (rolled back)", log.Props{
+ c.Logger().Infof("@{HttpMethod:magenta} @{URL:magenta} finished in @{ElapsedMs:magenta}ms (rolled back)", log.Props{
"HttpMethod": c.Request.Method,
"URL": c.Request.URL.String(),
"ElapsedMs": time.Since(start).Nanoseconds() / int64(time.Millisecond),
@@ -185,7 +186,7 @@ func WebSetup() web.MiddlewareFunc {
}
//Still no errors, everything is fine!
- c.Logger().Debugf("@{HttpMethod:magenta} @{URL:magenta} finished in @{ElapsedMs:magenta}ms (committed)", log.Props{
+ c.Logger().Infof("@{HttpMethod:magenta} @{URL:magenta} finished in @{ElapsedMs:magenta}ms (committed)", log.Props{
"HttpMethod": c.Request.Method,
"URL": c.Request.URL.String(),
"ElapsedMs": time.Since(start).Nanoseconds() / int64(time.Millisecond),
diff --git a/app/pkg/web/request.go b/app/pkg/web/request.go
index f0b002405..bd7b3b2a3 100644
--- a/app/pkg/web/request.go
+++ b/app/pkg/web/request.go
@@ -13,6 +13,7 @@ import (
type Request struct {
instance *http.Request
Method string
+ ClientIP string
ContentLength int64
Body string
IsSecure bool
@@ -45,9 +46,20 @@ func WrapRequest(request *http.Request) Request {
}
}
+ clientIP := request.Header.Get("X-Forwarded-For")
+ if clientIP == "" {
+ clientIP = strings.Split(request.RemoteAddr, ":")[0]
+ if clientIP == "" {
+ clientIP = "N/A"
+ }
+ } else {
+ clientIP = strings.Split(clientIP, ",")[0]
+ }
+
return Request{
instance: request,
Method: request.Method,
+ ClientIP: strings.TrimSpace(clientIP),
ContentLength: request.ContentLength,
Body: string(bodyBytes),
URL: u,
diff --git a/app/pkg/web/request_test.go b/app/pkg/web/request_test.go
index 07395b4f5..859771425 100644
--- a/app/pkg/web/request_test.go
+++ b/app/pkg/web/request_test.go
@@ -17,14 +17,16 @@ func TestRequest_Basic(t *testing.T) {
req := web.WrapRequest(
&http.Request{
- Method: "GET",
- Header: header,
- Host: "helloworld.com",
+ Method: "GET",
+ Header: header,
+ RemoteAddr: "172.10.10.10:5555",
+ Host: "helloworld.com",
},
)
Expect(req.Method).Equals("GET")
Expect(req.GetHeader("Content-Type")).Equals("application/json")
+ Expect(req.ClientIP).Equals("172.10.10.10")
Expect(req.URL.Hostname()).Equals("helloworld.com")
Expect(req.URL.Scheme).Equals("http")
Expect(req.URL.RequestURI()).Equals("/")
@@ -49,6 +51,7 @@ func TestRequest_WithPort(t *testing.T) {
Expect(req.Method).Equals("GET")
Expect(req.GetHeader("Content-Type")).Equals("application/json")
+ Expect(req.ClientIP).Equals("N/A")
Expect(req.URL.Hostname()).Equals("helloworld.com")
Expect(req.URL.Scheme).Equals("http")
Expect(req.URL.Port()).Equals("3000")
@@ -62,6 +65,7 @@ func TestRequest_BehindTLSTerminationProxy(t *testing.T) {
header := make(http.Header, 0)
header.Set("X-Forwarded-Host", "feedback.mycompany.com")
header.Set("X-Forwarded-Proto", "https")
+ header.Set("X-Forwarded-For", "127.5.5.5, 129.2.2.2, 121.2.2.5")
req := web.WrapRequest(
&http.Request{
@@ -74,6 +78,7 @@ func TestRequest_BehindTLSTerminationProxy(t *testing.T) {
Expect(req.Method).Equals("GET")
Expect(req.URL.Hostname()).Equals("feedback.mycompany.com")
Expect(req.URL.Scheme).Equals("https")
+ Expect(req.ClientIP).Equals("127.5.5.5")
Expect(req.IsSecure).Equals(true)
Expect(req.IsAPI()).IsFalse()
}