Request logging additions (request id, forwarded user) (#618)
* Add request-id logging * Missing test file * Add option to log forwarded user information if available via X-Forwarded-Header * Format
This commit is contained in:
parent
e0a748812d
commit
afe431c94d
@ -8,6 +8,8 @@ import (
|
|||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
|
||||||
|
"github.com/sosedoff/pgweb/pkg/command"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -29,6 +31,7 @@ func SetLogger(l *logrus.Logger) {
|
|||||||
|
|
||||||
func RequestLogger(logger *logrus.Logger) gin.HandlerFunc {
|
func RequestLogger(logger *logrus.Logger) gin.HandlerFunc {
|
||||||
debug := logger.Level > logrus.InfoLevel
|
debug := logger.Level > logrus.InfoLevel
|
||||||
|
logForwardedUser := command.Opts.LogForwardedUser
|
||||||
|
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
@ -58,6 +61,19 @@ func RequestLogger(logger *logrus.Logger) gin.HandlerFunc {
|
|||||||
"path": path,
|
"path": path,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if reqID := getRequestID(c); reqID != "" {
|
||||||
|
fields["id"] = reqID
|
||||||
|
}
|
||||||
|
|
||||||
|
if logForwardedUser {
|
||||||
|
if forwardedUser := c.GetHeader("X-Forwarded-User"); forwardedUser != "" {
|
||||||
|
fields["forwarded_user"] = forwardedUser
|
||||||
|
}
|
||||||
|
if forwardedEmail := c.GetHeader("X-Forwarded-Email"); forwardedEmail != "" {
|
||||||
|
fields["forwarded_email"] = forwardedEmail
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if err := c.Errors.Last(); err != nil {
|
if err := c.Errors.Last(); err != nil {
|
||||||
fields["error"] = err.Error()
|
fields["error"] = err.Error()
|
||||||
}
|
}
|
||||||
@ -88,3 +104,11 @@ func RequestLogger(logger *logrus.Logger) gin.HandlerFunc {
|
|||||||
func sanitizeLogPath(str string) string {
|
func sanitizeLogPath(str string) string {
|
||||||
return reConnectToken.ReplaceAllString(str, "/connect/REDACTED")
|
return reConnectToken.ReplaceAllString(str, "/connect/REDACTED")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getRequestID(c *gin.Context) string {
|
||||||
|
id := c.GetHeader("x-request-id")
|
||||||
|
if id == "" {
|
||||||
|
id = c.GetHeader("x-amzn-trace-id")
|
||||||
|
}
|
||||||
|
return id
|
||||||
|
}
|
||||||
|
31
pkg/api/logger_test.go
Normal file
31
pkg/api/logger_test.go
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_getRequestID(t *testing.T) {
|
||||||
|
examples := []struct {
|
||||||
|
headers map[string]string
|
||||||
|
result string
|
||||||
|
}{
|
||||||
|
{map[string]string{}, ""},
|
||||||
|
{map[string]string{"X-Request-ID": "foo"}, "foo"},
|
||||||
|
{map[string]string{"x-request-id": "foo"}, "foo"},
|
||||||
|
{map[string]string{"x-request-id": "foo"}, "foo"},
|
||||||
|
{map[string]string{"x-request-id": "foo", "x-amzn-trace-id": "amz"}, "foo"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, ex := range examples {
|
||||||
|
req := &http.Request{Header: http.Header{}}
|
||||||
|
for k, v := range ex.headers {
|
||||||
|
req.Header.Set(k, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(t, ex.result, getRequestID(&gin.Context{Request: req}))
|
||||||
|
}
|
||||||
|
}
|
@ -23,6 +23,7 @@ type Options struct {
|
|||||||
Debug bool `short:"d" long:"debug" description:"Enable debugging mode"`
|
Debug bool `short:"d" long:"debug" description:"Enable debugging mode"`
|
||||||
LogLevel string `long:"log-level" description:"Logging level" default:"info"`
|
LogLevel string `long:"log-level" description:"Logging level" default:"info"`
|
||||||
LogFormat string `long:"log-format" description:"Logging output format" default:"text"`
|
LogFormat string `long:"log-format" description:"Logging output format" default:"text"`
|
||||||
|
LogForwardedUser bool `long:"log-forwarded-user" description:"Log user information available in X-Forwarded-User/Email headers"`
|
||||||
URL string `long:"url" description:"Database connection string"`
|
URL string `long:"url" description:"Database connection string"`
|
||||||
Host string `long:"host" description:"Server hostname or IP" default:"localhost"`
|
Host string `long:"host" description:"Server hostname or IP" default:"localhost"`
|
||||||
Port int `long:"port" description:"Server port" default:"5432"`
|
Port int `long:"port" description:"Server port" default:"5432"`
|
||||||
|
Loading…
x
Reference in New Issue
Block a user