275531de84046287b3e75e84a7ffa03c5933d81c — Martin Angers 6 months ago 6f06050
add request id to logging fields
3 files changed, 6 insertions(+), 3 deletions(-)

M builder/builder.go
M builder/middleware.go
M builder/middleware_test.go
M builder/builder.go => builder/builder.go +1 -1
@@ 176,7 176,7 @@ var mw []middleware
  
  	if fn := s.Root.LoggingFunc; fn != nil {
- 		mw = append(mw, logging(fn))
+ 		mw = append(mw, logging(s.Root.RequestIDHeader, fn))
  	}
  	if fn := s.Root.PanicRecoveryFunc; fn != nil {
  		mw = append(mw, panicRecovery(fn))

M builder/middleware.go => builder/middleware.go +4 -1
@@ 171,7 171,7 @@   // logging calls the wrapped handler and collects relevant information
  // about the request, and then calls logFn with that information.
- func logging(logFn func(http.ResponseWriter, *http.Request, map[string]interface{})) func(http.Handler) http.Handler {
+ func logging(reqIDHeader string, logFn func(http.ResponseWriter, *http.Request, map[string]interface{})) func(http.Handler) http.Handler {
  	return func(h http.Handler) http.Handler {
  		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  			if _, ok := w.(interface {


@@ 201,6 201,9 @@ "user_agent":          r.UserAgent(),
  				"remote_addr":         r.RemoteAddr,
  			}
+ 			if reqIDHeader != "" {
+ 				m["request_id"] = w.Header().Get(reqIDHeader)
+ 			}
  			if w, ok := w.(interface {
  				Status() int
  				Size() int64

M builder/middleware_test.go => builder/middleware_test.go +1 -1
@@ 285,7 285,7 @@ logFn := func(w http.ResponseWriter, r *http.Request, info map[string]interface{}) {
  		logger.WithFields(logrus.Fields(info)).Info("logging")
  	}
- 	h := logging(logFn)(statusHandler(204))
+ 	h := logging("", logFn)(statusHandler(204))
  
  	w := httptest.NewRecorder()
  	r, _ := http.NewRequest("", "/", nil)