Skip to content
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

Use chained interceptors in the gRPC server #3744

Merged
merged 1 commit into from
Jul 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions config/configgrpc/configgrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,9 @@ func (gss *GRPCServerSettings) ToServerOption(ext map[config.ComponentID]compone
}
}

uInterceptors := []grpc.UnaryServerInterceptor{}
sInterceptors := []grpc.StreamServerInterceptor{}

if gss.Auth != nil {
componentID, cperr := config.NewIDFromString(gss.Auth.AuthenticatorName)
if cperr != nil {
Expand All @@ -336,24 +339,22 @@ func (gss *GRPCServerSettings) ToServerOption(ext map[config.ComponentID]compone
return nil, err
}

opts = append(opts,
grpc.UnaryInterceptor(authenticator.GRPCUnaryServerInterceptor),
grpc.StreamInterceptor(authenticator.GRPCStreamServerInterceptor),
)
uInterceptors = append(uInterceptors, authenticator.GRPCUnaryServerInterceptor)
sInterceptors = append(sInterceptors, authenticator.GRPCStreamServerInterceptor)
}

// Enable OpenTelemetry observability plugin.
// TODO: Pass construct settings to have access to Tracer.
opts = append(opts, grpc.UnaryInterceptor(
otelgrpc.UnaryServerInterceptor(
otelgrpc.WithTracerProvider(otel.GetTracerProvider()),
otelgrpc.WithPropagators(otel.GetTextMapPropagator()),
)))
opts = append(opts, grpc.StreamInterceptor(
otelgrpc.StreamServerInterceptor(
otelgrpc.WithTracerProvider(otel.GetTracerProvider()),
otelgrpc.WithPropagators(otel.GetTextMapPropagator()),
)))
uInterceptors = append(uInterceptors, otelgrpc.UnaryServerInterceptor(
otelgrpc.WithTracerProvider(otel.GetTracerProvider()),
otelgrpc.WithPropagators(otel.GetTextMapPropagator()),
))
sInterceptors = append(sInterceptors, otelgrpc.StreamServerInterceptor(
otelgrpc.WithTracerProvider(otel.GetTracerProvider()),
otelgrpc.WithPropagators(otel.GetTextMapPropagator()),
))

opts = append(opts, grpc.ChainUnaryInterceptor(uInterceptors...), grpc.ChainStreamInterceptor(sInterceptors...))

return opts, nil
}
Expand Down
9 changes: 8 additions & 1 deletion config/configgrpc/configgrpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ func TestAllGrpcClientSettings(t *testing.T) {
func TestDefaultGrpcServerSettings(t *testing.T) {
gss := &GRPCServerSettings{}
opts, err := gss.ToServerOption(map[config.ComponentID]component.Extension{})
_ = grpc.NewServer(opts...)

assert.NoError(t, err)
assert.Len(t, opts, 2)
}
Expand Down Expand Up @@ -114,6 +116,8 @@ func TestAllGrpcServerSettingsExceptAuth(t *testing.T) {
},
}
opts, err := gss.ToServerOption(map[config.ComponentID]component.Extension{})
_ = grpc.NewServer(opts...)

assert.NoError(t, err)
assert.Len(t, opts, 9)
}
Expand All @@ -133,6 +137,7 @@ func TestGrpcServerAuthSettings(t *testing.T) {
config.NewID("mock"): &configauth.MockAuthenticator{},
}
opts, err := gss.ToServerOption(ext)
_ = grpc.NewServer(opts...)

// verify
assert.NoError(t, err)
Expand Down Expand Up @@ -297,7 +302,9 @@ func TestGRPCServerSettingsError(t *testing.T) {
}
for _, test := range tests {
t.Run(test.err, func(t *testing.T) {
_, err := test.settings.ToServerOption(map[config.ComponentID]component.Extension{})
opts, err := test.settings.ToServerOption(map[config.ComponentID]component.Extension{})
_ = grpc.NewServer(opts...)

assert.Regexp(t, test.err, err)
})
}
Expand Down