add metrics, tracing, validation interceptors, add options: WithInterceptors, With{Client,Server}Interceptors

This commit is contained in:
2021-11-21 14:58:49 +01:00
parent 9ae20eab1e
commit dc78a2c688
15 changed files with 401 additions and 41 deletions

View File

@ -17,6 +17,7 @@ import (
"google.golang.org/grpc"
"go.linka.cloud/grpc/certs"
"go.linka.cloud/grpc/interceptors"
"go.linka.cloud/grpc/registry"
"go.linka.cloud/grpc/transport"
"go.linka.cloud/grpc/utils/addr"
@ -228,16 +229,45 @@ func WithAfterStop(fn ...func() error) Option {
}
}
func WithInterceptors(i ...interceptors.Interceptors) Option {
return func(o *options) {
for _, v := range i {
o.unaryServerInterceptors = append(o.unaryServerInterceptors, v.UnaryServerInterceptor())
o.streamServerInterceptors = append(o.streamServerInterceptors, v.StreamServerInterceptor())
o.unaryClientInterceptors = append(o.unaryClientInterceptors, v.UnaryClientInterceptor())
o.streamClientInterceptors = append(o.streamClientInterceptors, v.StreamClientInterceptor())
}
}
}
func WithServerInterceptors(i ...interceptors.ServerInterceptors) Option {
return func(o *options) {
for _, v := range i {
o.unaryServerInterceptors = append(o.unaryServerInterceptors, v.UnaryServerInterceptor())
o.streamServerInterceptors = append(o.streamServerInterceptors, v.StreamServerInterceptor())
}
}
}
func WithClientInterceptors(i ...interceptors.ClientInterceptors) Option {
return func(o *options) {
for _, v := range i {
o.unaryClientInterceptors = append(o.unaryClientInterceptors, v.UnaryClientInterceptor())
o.streamClientInterceptors = append(o.streamClientInterceptors, v.StreamClientInterceptor())
}
}
}
func WithUnaryClientInterceptor(i ...grpc.UnaryClientInterceptor) Option {
return func(o *options) {
o.clientInterceptors = append(o.clientInterceptors, i...)
o.unaryClientInterceptors = append(o.unaryClientInterceptors, i...)
}
}
// WithUnaryServerInterceptor adds unary Wrapper interceptors to the options passed into the server
func WithUnaryServerInterceptor(i ...grpc.UnaryServerInterceptor) Option {
return func(o *options) {
o.serverInterceptors = append(o.serverInterceptors, i...)
o.unaryServerInterceptors = append(o.unaryServerInterceptors, i...)
}
}
@ -340,10 +370,11 @@ type options struct {
serverOpts []grpc.ServerOption
serverInterceptors []grpc.UnaryServerInterceptor
unaryServerInterceptors []grpc.UnaryServerInterceptor
streamServerInterceptors []grpc.StreamServerInterceptor
clientInterceptors []grpc.UnaryClientInterceptor
unaryClientInterceptors []grpc.UnaryClientInterceptor
streamClientInterceptors []grpc.StreamClientInterceptor
mux ServeMux
@ -428,7 +459,7 @@ func (o *options) ServerOpts() []grpc.ServerOption {
}
func (o *options) ServerInterceptors() []grpc.UnaryServerInterceptor {
return o.serverInterceptors
return o.unaryServerInterceptors
}
func (o *options) StreamServerInterceptors() []grpc.StreamServerInterceptor {
@ -436,7 +467,7 @@ func (o *options) StreamServerInterceptors() []grpc.StreamServerInterceptor {
}
func (o *options) ClientInterceptors() []grpc.UnaryClientInterceptor {
return o.clientInterceptors
return o.unaryClientInterceptors
}
func (o *options) StreamClientInterceptors() []grpc.StreamClientInterceptor {

View File

@ -107,10 +107,10 @@ func newService(opts ...Option) (*service, error) {
}
return s.run()
}
ui := grpcmiddleware.ChainUnaryServer(s.opts.serverInterceptors...)
ui := grpcmiddleware.ChainUnaryServer(s.opts.unaryServerInterceptors...)
s.inproc = s.inproc.WithServerUnaryInterceptor(ui)
si := grpcmiddleware.ChainStreamServer(/*TODO(adphi): add to options*/)
si := grpcmiddleware.ChainStreamServer( /*TODO(adphi): add to options*/ )
s.inproc = s.inproc.WithServerStreamInterceptor(si)
gopts := []grpc.ServerOption{
@ -181,8 +181,8 @@ func (s *service) run() error {
if reflect.DeepEqual(s.opts.cors, cors.Options{}) {
s.opts.cors = cors.Options{
AllowedHeaders: []string{"*"},
AllowedMethods: []string{
AllowedHeaders: []string{"*"},
AllowedMethods: []string{
http.MethodGet,
http.MethodPost,
http.MethodPut,
@ -235,7 +235,7 @@ func (s *service) run() error {
logrus.Warnf("received %v", sig)
return s.Close()
case err := <-errs:
if err != nil && !ignoreMuxError(err){
if err != nil && !ignoreMuxError(err) {
logrus.Error(err)
return err
}