Skip to content

Commit

Permalink
logger, tracking: use X-Tracking-Id header and add tracking_id log field
Browse files Browse the repository at this point in the history
* use X-Tracking-Id as default tracking header, falls back to X-Request_Id
* add both tracking_id and request_id to logger context
  • Loading branch information
bernhardbarker authored Oct 1, 2020
1 parent c979f4c commit 8cb2fae
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 34 deletions.
1 change: 1 addition & 0 deletions logger/internal/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ const (
HeaderForwardedFor = "X-Forwarded-For"
HeaderRequestID = "X-Request-Id"
HeaderRequestDepth = "X-Request-Depth"
HeaderTrackingID = "X-Tracking-Id"
HeaderTreePath = "X-Tree-Path"
)
7 changes: 4 additions & 3 deletions logger/middleware/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@ func HTTPRequestLogger(skipRoutes []string) func(http.Handler) http.Handler {
ctx := r.Context()

log := *logger.FromContext(ctx)
trackingID := tracking.IDFromContext(ctx)
logFields := map[string]interface{}{
internal.FieldEntryPoint: isEntryPoint(r),
// TODO double check if they are a must and make them optional if not
internal.FieldRequestDepth: tracking.RequestDepthFromCtx(ctx),
internal.FieldTrackingID: tracking.IDFromContext(ctx),
internal.FieldRequestID: tracking.IDFromContext(ctx),
internal.FieldTrackingID: trackingID,
internal.FieldRequestID: trackingID,
internal.FieldTreePath: tracking.TreePathFromCtx(ctx),
internal.FieldRoute: tracking.RequestRouteFromCtx(ctx),
internal.FieldParams: r.URL.RawQuery,
Expand Down Expand Up @@ -99,7 +100,7 @@ func ipAddress(r *http.Request) string {
}

func isEntryPoint(r *http.Request) bool {
return len(r.Header.Get(internal.HeaderRequestID)) == 0
return r.Header.Get(internal.HeaderRequestID) == "" && r.Header.Get(internal.HeaderTrackingID) == ""
}

type responseWriter struct {
Expand Down
1 change: 1 addition & 0 deletions tracking/internal/constants/headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ const (
HeaderForwardedFor = "X-Forwarded-For"
HeaderRequestID = "X-Request-Id"
HeaderRequestDepth = "X-Request-Depth"
HeaderTrackingID = "X-Tracking-Id"
HeaderTreePath = "X-Tree-Path"
)
15 changes: 9 additions & 6 deletions tracking/middleware/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@ import (
// if it does it sets it on the context, if it does not it generates a new one to set on the context
func EventsAddTrackingID(next events.Handler) events.Handler {
return events.HandlerFunc(func(ctx context.Context, e events.Event) error {
trackingID := e.Headers[constants.HeaderRequestID]
trackingID := e.Headers[constants.HeaderTrackingID]
if trackingID == "" {
uuid, err := uuid.NewUUID()
if err != nil {
logger.FromContext(ctx).Err(err).Msg("could not generate uuid not setting trackingID in context")
return next.Handle(ctx, e)
trackingID = e.Headers[constants.HeaderRequestID]
if trackingID == "" {
uuid, err := uuid.NewUUID()
if err != nil {
logger.FromContext(ctx).Err(err).Msg("could not generate uuid, not setting trackingID in context")
return next.Handle(ctx, e)
}
trackingID = uuid.String()
}
trackingID = uuid.String()
}
ctx = tracking.SetContextID(ctx, trackingID)
return next.Handle(ctx, e)
Expand Down
37 changes: 19 additions & 18 deletions tracking/middleware/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,29 @@ func TestEventsAddTrackingIDCreatesIDWhenEventHeaderEmpty(t *testing.T) {

err := testHandler.Handle(context.Background(), e)
if err != nil {
t.Errorf("could not succesfully handle: %v", err)
t.Errorf("could not successfully handle: %v", err)
}
}

func TestEventsAddTrackingIDDoesNotChangeTrackingIDIfAlreadyPresent(t *testing.T) {
testId := "goodid"

e := events.Event{
Headers: events.Header(map[string]string{constants.HeaderRequestID: testId}),
}

testHandler := EventsAddTrackingID(events.Handler(
events.HandlerFunc(func(ctx context.Context, e events.Event) error {
got := tracking.IDFromContext(ctx)
if !cmp.Equal(got, testId) {
t.Errorf("want: %v, got: %v", testId, got)
}
return nil
})))

err := testHandler.Handle(context.Background(), e)
if err != nil {
t.Errorf("could not succesfully handle: %v", err)
for _, headerName := range []string{constants.HeaderRequestID, constants.HeaderTrackingID} {
e := events.Event{
Headers: events.Header(map[string]string{headerName: testId}),
}

testHandler := EventsAddTrackingID(events.Handler(
events.HandlerFunc(func(ctx context.Context, e events.Event) error {
got := tracking.IDFromContext(ctx)
if !cmp.Equal(got, testId) {
t.Errorf("field name: %s, want: %v, got: %v", headerName, testId, got)
}
return nil
})))

err := testHandler.Handle(context.Background(), e)
if err != nil {
t.Errorf("field name: %s, could not successfully handle: %v", headerName, err)
}
}
}
18 changes: 11 additions & 7 deletions tracking/middleware/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,27 @@ import (
"github.com/blacklane/go-libs/tracking/internal/constants"
)

func RequestID(next http.Handler) http.Handler {
func TrackingID(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requestID := ExtractRequestID(r)
trackingID := ExtractTrackingID(r)

ctx := tracking.SetContextID(r.Context(), requestID)
ctx := tracking.SetContextID(r.Context(), trackingID)
rr := r.WithContext(ctx)

next.ServeHTTP(w, rr)
})
}

func ExtractRequestID(r *http.Request) string {
func ExtractTrackingID(r *http.Request) string {
trackingID := r.Header.Get(constants.HeaderTrackingID)
if trackingID != "" {
return trackingID
}
requestID := r.Header.Get(constants.HeaderRequestID)
if requestID == "" {
requestID = uuid.New().String()
if requestID != "" {
return requestID
}
return requestID
return uuid.New().String()
}

// ExtractRequestDepth returns the request depth extracted from the header added of 1 or
Expand Down

0 comments on commit 8cb2fae

Please sign in to comment.