-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d6fc251
commit 3ffed9e
Showing
18 changed files
with
254 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
"github.com/google/uuid" | ||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
// Key to use when setting the request ID. | ||
type ctxKeyRequestID int | ||
|
||
// requestIDKey is the key that holds the unique request ID in a request context. | ||
const requestIDKey ctxKeyRequestID = 0 | ||
|
||
// requestIDHeader is the name of the HTTP Header that contains the request id. | ||
var requestIDHeader = "X-Request-Id" | ||
|
||
// Creates an ID for the request. This will be stored in the forwarded context | ||
// and is added as a context field to the logger. | ||
func createRequestIdMiddleware(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
id := uuid.New().String() | ||
w.Header().Add(requestIDHeader, id) | ||
|
||
logger := log.With().Str("requestId", id).Logger() | ||
|
||
ctx := r.Context() | ||
ctx = context.WithValue(ctx, requestIDKey, id) | ||
ctx = logger.WithContext(ctx) | ||
|
||
next.ServeHTTP(w, r.WithContext(ctx)) | ||
}) | ||
} | ||
|
||
// Gets the ID for the current request or empty if there is none. | ||
func GetRequestId(ctx context.Context) string { | ||
id, _ := ctx.Value(requestIDKey).(string) | ||
return id | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package api | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/rs/zerolog" | ||
"github.com/rs/zerolog/log" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestAddsHeader(t *testing.T) { | ||
require := require.New(t) | ||
nextCalled := false | ||
nextHandler := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { | ||
nextCalled = true | ||
require.NotEmpty(GetRequestId(r.Context())) | ||
}) | ||
|
||
handler := createRequestIdMiddleware(nextHandler) | ||
|
||
recorder := httptest.NewRecorder() | ||
handler.ServeHTTP(recorder, httptest.NewRequest("GET", "http://abc", nil)) | ||
require.True(nextCalled) | ||
require.NotEmpty(recorder.Result().Header.Get(requestIDHeader)) | ||
} | ||
|
||
func TestAddsRequestIdToLogEntries(t *testing.T) { | ||
require := require.New(t) | ||
existingLogger := log.Logger | ||
defer func() { log.Logger = existingLogger }() | ||
|
||
buf := bytes.Buffer{} | ||
log.Logger = zerolog.New(&buf) | ||
|
||
nextHandler := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { | ||
log.Ctx(r.Context()).Info().Msg("hello") | ||
}) | ||
|
||
handler := createRequestIdMiddleware(nextHandler) | ||
handler.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest("GET", "http://abc", nil)) | ||
|
||
require.NotEmpty(buf.String()) | ||
loggedMap := make(map[string]string) | ||
require.Nil(json.Unmarshal(buf.Bytes(), &loggedMap)) | ||
require.NotEmpty(loggedMap["requestId"]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ import ( | |
"io" | ||
"time" | ||
|
||
"github.com/gofrs/uuid" | ||
"github.com/google/uuid" | ||
) | ||
|
||
var ( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.