-
Notifications
You must be signed in to change notification settings - Fork 405
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding a way to inject a request ID (#1046)
* Adding a way to inject a request ID It is very useful to associate a request ID to each incoming request, this change allows to provide a function to do that via Server Option. The change comes with a default function which will generate a new request ID. The request ID is put in the request context along with a common logger which always logs the request-id We add gRPC interceptors to the server so it can get the request ID out of the gRPC metadata and put it in the common logger stored in the context so as all the log lines using the common logger from the context will have the request ID logged
- Loading branch information
Showing
133 changed files
with
10,665 additions
and
14 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -81,3 +81,4 @@ ignored = ["github.com/fnproject/fn/cli", | |
[[constraint]] | ||
name = "github.com/dchest/siphash" | ||
version = "1.1.0" | ||
|
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,21 @@ | ||
package common | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/fnproject/fn/api/id" | ||
) | ||
|
||
// FnRequestID returns the passed value if that is not empty otherwise it generates a new unique ID | ||
func FnRequestID(ridFound string) string { | ||
if ridFound == "" { | ||
return id.New().String() | ||
} | ||
return ridFound | ||
} | ||
|
||
//RequestIDFromContext extract the request id from the context | ||
func RequestIDFromContext(ctx context.Context) string { | ||
rid, _ := ctx.Value(contextKey(RequestIDContextKey)).(string) | ||
return rid | ||
} |
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,33 @@ | ||
package grpcutil | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/fnproject/fn/api/common" | ||
"google.golang.org/grpc/metadata" | ||
) | ||
|
||
func TestRIDFoundInMetadata(t *testing.T) { | ||
expected := "request-id-test" | ||
ctx := context.Background() | ||
m := make(map[string]string) | ||
m[common.RequestIDContextKey] = expected | ||
md := metadata.New(m) | ||
incomingCtx := metadata.NewIncomingContext(ctx, md) | ||
actual := ridFromMetadata(incomingCtx) | ||
if actual != expected { | ||
t.Fatalf("Wrong request ID expected '%s' got '%s'", expected, actual) | ||
} | ||
} | ||
|
||
func TestRIDNotFoundInMetadata(t *testing.T) { | ||
ctx := context.Background() | ||
m := make(map[string]string) | ||
md := metadata.New(m) | ||
incomingCtx := metadata.NewIncomingContext(ctx, md) | ||
actual := ridFromMetadata(incomingCtx) | ||
if actual != "" { | ||
t.Fatalf("Expected empty request ID got '%s'", actual) | ||
} | ||
} |
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.