Skip to content

Commit

Permalink
transport: Pass Header metadata to tap handle.
Browse files Browse the repository at this point in the history
  • Loading branch information
pstibrany committed Oct 3, 2023
1 parent 130bc42 commit 9f727c4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
3 changes: 2 additions & 1 deletion internal/transport/http2_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/golang/protobuf/proto"
"golang.org/x/net/http2"
"golang.org/x/net/http2/hpack"

"google.golang.org/grpc/internal/grpclog"
"google.golang.org/grpc/internal/grpcutil"
"google.golang.org/grpc/internal/pretty"
Expand Down Expand Up @@ -566,7 +567,7 @@ func (t *http2Server) operateHeaders(frame *http2.MetaHeadersFrame, handle func(
}
if t.inTapHandle != nil {
var err error
if s.ctx, err = t.inTapHandle(s.ctx, &tap.Info{FullMethodName: s.method}); err != nil {
if s.ctx, err = t.inTapHandle(s.ctx, &tap.Info{FullMethodName: s.method, Header: mdata}); err != nil {
t.mu.Unlock()
if t.logger.V(logLevel) {
t.logger.Infof("Aborting the stream early due to InTapHandle failure: %v", err)
Expand Down
6 changes: 6 additions & 0 deletions tap/tap.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,19 @@ package tap

import (
"context"

"google.golang.org/grpc/metadata"
)

// Info defines the relevant information needed by the handles.
type Info struct {
// FullMethodName is the string of grpc method (in the format of
// /package.service/method).
FullMethodName string

// Header contains the header metadata received.
Header metadata.MD

// TODO: More to be added.
}

Expand Down
21 changes: 21 additions & 0 deletions test/end2end_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import (
"github.com/golang/protobuf/proto"
"golang.org/x/net/http2"
"golang.org/x/net/http2/hpack"

"google.golang.org/grpc"
"google.golang.org/grpc/balancer"
"google.golang.org/grpc/balancer/roundrobin"
Expand Down Expand Up @@ -74,6 +75,7 @@ import (

anypb "github.com/golang/protobuf/ptypes/any"
spb "google.golang.org/genproto/googleapis/rpc/status"

healthgrpc "google.golang.org/grpc/health/grpc_health_v1"
healthpb "google.golang.org/grpc/health/grpc_health_v1"
testgrpc "google.golang.org/grpc/interop/grpc_testing"
Expand Down Expand Up @@ -2099,6 +2101,10 @@ func (t *myTap) handle(ctx context.Context, info *tap.Info) (context.Context, er
switch info.FullMethodName {
case "/grpc.testing.TestService/EmptyCall":
t.cnt++

if vals := info.Header.Get("return-error"); len(vals) > 0 && vals[0] == "true" {
return nil, status.Errorf(codes.Unknown, "tap error")
}
case "/grpc.testing.TestService/UnaryCall":
return nil, fmt.Errorf("tap error")
case "/grpc.testing.TestService/FullDuplexCall":
Expand All @@ -2120,13 +2126,28 @@ func testTap(t *testing.T, e env) {
tc := testgrpc.NewTestServiceClient(cc)
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()

if _, err := tc.EmptyCall(ctx, &testpb.Empty{}); err != nil {
t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want _, <nil>", err)
}
if ttap.cnt != 1 {
t.Fatalf("Get the count in ttap %d, want 1", ttap.cnt)
}

if _, err := tc.EmptyCall(metadata.AppendToOutgoingContext(ctx, "return-error", "false"), &testpb.Empty{}); err != nil {
t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want _, <nil>", err)
}
if ttap.cnt != 2 {
t.Fatalf("Get the count in ttap %d, want 2", ttap.cnt)
}

if _, err := tc.EmptyCall(metadata.AppendToOutgoingContext(ctx, "return-error", "true"), &testpb.Empty{}); status.Code(err) != codes.Unknown {
t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want _, %s", err, codes.Unknown)
}
if ttap.cnt != 3 {
t.Fatalf("Get the count in ttap %d, want 3", ttap.cnt)
}

payload, err := newPayload(testpb.PayloadType_COMPRESSABLE, 31)
if err != nil {
t.Fatal(err)
Expand Down

0 comments on commit 9f727c4

Please sign in to comment.