Skip to content

Commit be7919c

Browse files
authored
transport: Pass Header metadata to tap handle. (grpc#6652)
1 parent e3f1514 commit be7919c

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

internal/transport/http2_server.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ func (t *http2Server) operateHeaders(frame *http2.MetaHeadersFrame, handle func(
566566
}
567567
if t.inTapHandle != nil {
568568
var err error
569-
if s.ctx, err = t.inTapHandle(s.ctx, &tap.Info{FullMethodName: s.method}); err != nil {
569+
if s.ctx, err = t.inTapHandle(s.ctx, &tap.Info{FullMethodName: s.method, Header: mdata}); err != nil {
570570
t.mu.Unlock()
571571
if t.logger.V(logLevel) {
572572
t.logger.Infof("Aborting the stream early due to InTapHandle failure: %v", err)

tap/tap.go

+6
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,19 @@ package tap
2727

2828
import (
2929
"context"
30+
31+
"google.golang.org/grpc/metadata"
3032
)
3133

3234
// Info defines the relevant information needed by the handles.
3335
type Info struct {
3436
// FullMethodName is the string of grpc method (in the format of
3537
// /package.service/method).
3638
FullMethodName string
39+
40+
// Header contains the header metadata received.
41+
Header metadata.MD
42+
3743
// TODO: More to be added.
3844
}
3945

test/end2end_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -2099,6 +2099,10 @@ func (t *myTap) handle(ctx context.Context, info *tap.Info) (context.Context, er
20992099
switch info.FullMethodName {
21002100
case "/grpc.testing.TestService/EmptyCall":
21012101
t.cnt++
2102+
2103+
if vals := info.Header.Get("return-error"); len(vals) > 0 && vals[0] == "true" {
2104+
return nil, status.Errorf(codes.Unknown, "tap error")
2105+
}
21022106
case "/grpc.testing.TestService/UnaryCall":
21032107
return nil, fmt.Errorf("tap error")
21042108
case "/grpc.testing.TestService/FullDuplexCall":
@@ -2120,13 +2124,28 @@ func testTap(t *testing.T, e env) {
21202124
tc := testgrpc.NewTestServiceClient(cc)
21212125
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
21222126
defer cancel()
2127+
21232128
if _, err := tc.EmptyCall(ctx, &testpb.Empty{}); err != nil {
21242129
t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want _, <nil>", err)
21252130
}
21262131
if ttap.cnt != 1 {
21272132
t.Fatalf("Get the count in ttap %d, want 1", ttap.cnt)
21282133
}
21292134

2135+
if _, err := tc.EmptyCall(metadata.AppendToOutgoingContext(ctx, "return-error", "false"), &testpb.Empty{}); err != nil {
2136+
t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want _, <nil>", err)
2137+
}
2138+
if ttap.cnt != 2 {
2139+
t.Fatalf("Get the count in ttap %d, want 2", ttap.cnt)
2140+
}
2141+
2142+
if _, err := tc.EmptyCall(metadata.AppendToOutgoingContext(ctx, "return-error", "true"), &testpb.Empty{}); status.Code(err) != codes.Unknown {
2143+
t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want _, %s", err, codes.Unknown)
2144+
}
2145+
if ttap.cnt != 3 {
2146+
t.Fatalf("Get the count in ttap %d, want 3", ttap.cnt)
2147+
}
2148+
21302149
payload, err := newPayload(testpb.PayloadType_COMPRESSABLE, 31)
21312150
if err != nil {
21322151
t.Fatal(err)

0 commit comments

Comments
 (0)