-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for gRPC Server Response Headers and Trailers, adding ClientAfter handler #479
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e9965a6
adds gRPC ClientAfter handler and multiple calls to Client Before/Aft…
basvanbeek 2677e49
added unit test for gRPC header and trailer request/response propagation
basvanbeek 01974e5
panic on codec error is better as it's a coding error
basvanbeek 550ed03
refactored ServerResponseFunc to be more symmetrical with ClientRespo…
basvanbeek 78ef6c8
moved protobuf package for test inside the grpc test folder
basvanbeek 4079d84
Improved gRPC Request / Response Funcs
basvanbeek 8864ce8
updates to reflect change in ServerRequestFunc
basvanbeek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package test | ||
|
||
import ( | ||
"context" | ||
|
||
"google.golang.org/grpc" | ||
|
||
"github.com/go-kit/kit/endpoint" | ||
grpctransport "github.com/go-kit/kit/transport/grpc" | ||
"github.com/go-kit/kit/transport/grpc/_grpc_test/pb" | ||
) | ||
|
||
type clientBinding struct { | ||
test endpoint.Endpoint | ||
} | ||
|
||
func (c *clientBinding) Test(ctx context.Context, a string, b int64) (context.Context, string, error) { | ||
response, err := c.test(ctx, TestRequest{A: a, B: b}) | ||
if err != nil { | ||
return nil, "", err | ||
} | ||
r := response.(*TestResponse) | ||
return r.Ctx, r.V, nil | ||
} | ||
|
||
func NewClient(cc *grpc.ClientConn) Service { | ||
return &clientBinding{ | ||
test: grpctransport.NewClient( | ||
cc, | ||
"pb.Test", | ||
"Test", | ||
encodeRequest, | ||
decodeResponse, | ||
&pb.TestResponse{}, | ||
grpctransport.ClientBefore( | ||
injectCorrelationID, | ||
), | ||
grpctransport.ClientBefore( | ||
displayClientRequestHeaders, | ||
), | ||
grpctransport.ClientAfter( | ||
displayClientResponseHeaders, | ||
displayClientResponseTrailers, | ||
), | ||
grpctransport.ClientAfter( | ||
extractConsumedCorrelationID, | ||
), | ||
).Endpoint(), | ||
} | ||
} |
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,136 @@ | ||
package test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"google.golang.org/grpc/metadata" | ||
) | ||
|
||
type metaContext string | ||
|
||
const ( | ||
correlationID metaContext = "correlation-id" | ||
responseHDR metaContext = "my-response-header" | ||
responseTRLR metaContext = "my-response-trailer" | ||
correlationIDTRLR metaContext = "correlation-id-consumed" | ||
) | ||
|
||
/* client before functions */ | ||
|
||
func injectCorrelationID(ctx context.Context, md *metadata.MD) context.Context { | ||
if hdr, ok := ctx.Value(correlationID).(string); ok { | ||
fmt.Printf("\tClient found correlationID %q in context, set metadata header\n", hdr) | ||
(*md)[string(correlationID)] = append((*md)[string(correlationID)], hdr) | ||
} | ||
return ctx | ||
} | ||
|
||
func displayClientRequestHeaders(ctx context.Context, md *metadata.MD) context.Context { | ||
if len(*md) > 0 { | ||
fmt.Println("\tClient >> Request Headers:") | ||
for key, val := range *md { | ||
fmt.Printf("\t\t%s: %s\n", key, val[len(val)-1]) | ||
} | ||
} | ||
return ctx | ||
} | ||
|
||
/* server before functions */ | ||
|
||
func extractCorrelationID(ctx context.Context, md *metadata.MD) context.Context { | ||
if hdr, ok := (*md)[string(correlationID)]; ok { | ||
cID := hdr[len(hdr)-1] | ||
ctx = context.WithValue(ctx, correlationID, cID) | ||
fmt.Printf("\tServer received correlationID %q in metadata header, set context\n", cID) | ||
} | ||
return ctx | ||
} | ||
|
||
func displayServerRequestHeaders(ctx context.Context, md *metadata.MD) context.Context { | ||
if len(*md) > 0 { | ||
fmt.Println("\tServer << Request Headers:") | ||
for key, val := range *md { | ||
fmt.Printf("\t\t%s: %s\n", key, val[len(val)-1]) | ||
} | ||
} | ||
return ctx | ||
} | ||
|
||
/* server after functions */ | ||
|
||
func injectResponseHeader(ctx context.Context, md *metadata.MD, _ *metadata.MD) { | ||
*md = metadata.Join(*md, metadata.Pairs(string(responseHDR), "has-a-value")) | ||
} | ||
|
||
func displayServerResponseHeaders(ctx context.Context, md *metadata.MD, _ *metadata.MD) { | ||
if len(*md) > 0 { | ||
fmt.Println("\tServer >> Response Headers:") | ||
for key, val := range *md { | ||
fmt.Printf("\t\t%s: %s\n", key, val[len(val)-1]) | ||
} | ||
} | ||
} | ||
|
||
func injectResponseTrailer(ctx context.Context, _ *metadata.MD, md *metadata.MD) { | ||
*md = metadata.Join(*md, metadata.Pairs(string(responseTRLR), "has-a-value-too")) | ||
} | ||
|
||
func injectConsumedCorrelationID(ctx context.Context, _ *metadata.MD, md *metadata.MD) { | ||
if hdr, ok := ctx.Value(correlationID).(string); ok { | ||
fmt.Printf("\tServer found correlationID %q in context, set consumed trailer\n", hdr) | ||
*md = metadata.Join(*md, metadata.Pairs(string(correlationIDTRLR), hdr)) | ||
} | ||
} | ||
|
||
func displayServerResponseTrailers(ctx context.Context, _ *metadata.MD, md *metadata.MD) { | ||
if len(*md) > 0 { | ||
fmt.Println("\tServer >> Response Trailers:") | ||
for key, val := range *md { | ||
fmt.Printf("\t\t%s: %s\n", key, val[len(val)-1]) | ||
} | ||
} | ||
} | ||
|
||
/* client after functions */ | ||
|
||
func displayClientResponseHeaders(ctx context.Context, md metadata.MD, _ metadata.MD) context.Context { | ||
if len(md) > 0 { | ||
fmt.Println("\tClient << Response Headers:") | ||
for key, val := range md { | ||
fmt.Printf("\t\t%s: %s\n", key, val[len(val)-1]) | ||
} | ||
} | ||
return ctx | ||
} | ||
|
||
func displayClientResponseTrailers(ctx context.Context, _ metadata.MD, md metadata.MD) context.Context { | ||
if len(md) > 0 { | ||
fmt.Println("\tClient << Response Trailers:") | ||
for key, val := range md { | ||
fmt.Printf("\t\t%s: %s\n", key, val[len(val)-1]) | ||
} | ||
} | ||
return ctx | ||
} | ||
|
||
func extractConsumedCorrelationID(ctx context.Context, _ metadata.MD, md metadata.MD) context.Context { | ||
if hdr, ok := md[string(correlationIDTRLR)]; ok { | ||
fmt.Printf("\tClient received consumed correlationID %q in metadata trailer, set context\n", hdr[len(hdr)-1]) | ||
ctx = context.WithValue(ctx, correlationIDTRLR, hdr[len(hdr)-1]) | ||
} | ||
return ctx | ||
} | ||
|
||
/* CorrelationID context handlers */ | ||
|
||
func SetCorrelationID(ctx context.Context, v string) context.Context { | ||
return context.WithValue(ctx, correlationID, v) | ||
} | ||
|
||
func GetConsumedCorrelationID(ctx context.Context) string { | ||
if trlr, ok := ctx.Value(correlationIDTRLR).(string); ok { | ||
return trlr | ||
} | ||
return "" | ||
} |
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,3 @@ | ||
package pb | ||
|
||
//go:generate protoc test.proto --go_out=plugins=grpc:. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1