Skip to content

Commit

Permalink
Add request_id in log
Browse files Browse the repository at this point in the history
  • Loading branch information
dynastymasra committed Mar 12, 2020
1 parent 8851c82 commit 48dcbb9
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions negroni/middleware/logger.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package middleware

import (
"fmt"
"context"
"net/http"
"time"

uuid "github.com/satori/go.uuid"

"github.com/dynastymasra/cookbook"

"github.com/sirupsen/logrus"
Expand All @@ -16,9 +18,12 @@ import (
func LogrusLog(name string) negroni.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
startTime := time.Now().UTC()
requestID := fmt.Sprint(r.Context().Value(RequestID))

next(w, r)
requestID := r.Header.Get(cookbook.XRequestID)
if len(requestID) < 1 {
requestID = uuid.NewV4().String()
}
next(w, r.WithContext(context.WithValue(r.Context(), cookbook.RequestID, requestID)))

responseTime := time.Now().UTC()
deltaTime := responseTime.Sub(startTime)
Expand Down

0 comments on commit 48dcbb9

Please sign in to comment.