Skip to content

Commit

Permalink
fix(#94): fix request id logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Jumpy-Squirrel committed Nov 9, 2024
1 parent 88b0177 commit 940f297
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
4 changes: 1 addition & 3 deletions internal/application/common/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,12 @@ type AllClaims struct {
CustomClaims
}

const RequestIDKey = "RequestIDKey"

// GetRequestID extracts the request ID from the context.
//
// It originally comes from a header with the request, or is rolled while processing
// the request.
func GetRequestID(ctx context.Context) string {
if reqID, ok := ctx.Value(RequestIDKey).(string); ok {
if reqID, ok := ctx.Value(CtxKeyRequestID{}).(string); ok {
return reqID
}

Expand Down
3 changes: 3 additions & 0 deletions internal/application/middleware/reqid.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package middleware

import (
"context"
aulogging "github.com/StephanHCB/go-autumn-logging"
"github.com/eurofurence/reg-room-service/internal/application/common"
"net/http"
"regexp"
Expand All @@ -18,6 +19,8 @@ var ValidRequestIdRegex = regexp.MustCompile("^[0-9a-f]{8}$")
// It also adds it to the response under the same header.
// This automatically also leads to all logging using this context to log the request id.
func RequestIdMiddleware(next http.Handler) http.Handler {
aulogging.RequestIdRetriever = common.GetRequestID

handlerFunc := func(w http.ResponseWriter, r *http.Request) {
reqUuidStr := r.Header.Get(RequestIDHeader)
if !ValidRequestIdRegex.MatchString(reqUuidStr) {
Expand Down

0 comments on commit 940f297

Please sign in to comment.