mirror of
https://github.com/linka-cloud/grpc.git
synced 2025-06-22 09:12:28 +00:00
interceptors: migrate to otel and add logging interceptor
Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
This commit is contained in:
@ -18,7 +18,7 @@ func (s *service) gateway(opts ...runtime.ServeMuxOption) error {
|
||||
return nil
|
||||
}
|
||||
mux := runtime.NewServeMux(append(defaultGatewayOptions, opts...)...)
|
||||
if err := s.opts.gateway(s.opts.ctx, mux, s.inproc); err != nil {
|
||||
if err := s.opts.gateway(s.opts.ctx, mux, s.wrapCC()); err != nil {
|
||||
return err
|
||||
}
|
||||
if s.opts.gatewayPrefix != "" {
|
||||
|
@ -1,6 +1,14 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/fullstorydev/grpchan/inprocgrpc"
|
||||
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
|
||||
"google.golang.org/grpc"
|
||||
insecure2 "google.golang.org/grpc/credentials/insecure"
|
||||
|
||||
"go.linka.cloud/grpc-toolkit/interceptors"
|
||||
"go.linka.cloud/grpc-toolkit/interceptors/metadata"
|
||||
)
|
||||
@ -18,3 +26,47 @@ func md(opts *options) interceptors.Interceptors {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *service) wrapCC() grpc.ClientConnInterface {
|
||||
c, err := grpc.NewClient("internal", grpc.WithTransportCredentials(insecure2.NewCredentials()))
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("failed to create fake grpc client: %v", err))
|
||||
}
|
||||
w := &client{ch: s.inproc, c: c}
|
||||
if len(s.opts.unaryClientInterceptors) != 0 {
|
||||
w.ui = grpc_middleware.ChainUnaryClient(s.opts.unaryClientInterceptors...)
|
||||
}
|
||||
if len(s.opts.streamClientInterceptors) != 0 {
|
||||
w.si = grpc_middleware.ChainStreamClient(s.opts.streamClientInterceptors...)
|
||||
}
|
||||
return w
|
||||
}
|
||||
|
||||
type client struct {
|
||||
ui grpc.UnaryClientInterceptor
|
||||
si grpc.StreamClientInterceptor
|
||||
ch *inprocgrpc.Channel
|
||||
c *grpc.ClientConn
|
||||
}
|
||||
|
||||
func (c *client) Invoke(ctx context.Context, method string, args any, reply any, opts ...grpc.CallOption) error {
|
||||
if c.ui != nil {
|
||||
return c.ui(ctx, method, args, reply, c.c, c.invoke, opts...)
|
||||
}
|
||||
return c.ch.Invoke(ctx, method, args, reply, opts...)
|
||||
}
|
||||
|
||||
func (c *client) NewStream(ctx context.Context, desc *grpc.StreamDesc, method string, opts ...grpc.CallOption) (grpc.ClientStream, error) {
|
||||
if c.si != nil {
|
||||
return c.si(ctx, desc, c.c, method, c.stream, opts...)
|
||||
}
|
||||
return c.ch.NewStream(ctx, desc, method, opts...)
|
||||
}
|
||||
|
||||
func (c *client) invoke(ctx context.Context, method string, req, reply any, _ *grpc.ClientConn, opts ...grpc.CallOption) error {
|
||||
return c.ch.Invoke(ctx, method, req, reply, opts...)
|
||||
}
|
||||
|
||||
func (c *client) stream(ctx context.Context, desc *grpc.StreamDesc, _ *grpc.ClientConn, method string, opts ...grpc.CallOption) (grpc.ClientStream, error) {
|
||||
return c.ch.NewStream(ctx, desc, method, opts...)
|
||||
}
|
||||
|
@ -91,7 +91,6 @@ func (o *options) Default() {
|
||||
if o.transport == nil {
|
||||
o.transport = &grpc.Server{}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
type Option func(*options)
|
||||
|
@ -30,6 +30,7 @@ import (
|
||||
"google.golang.org/grpc/health/grpc_health_v1"
|
||||
greflect "google.golang.org/grpc/reflection"
|
||||
|
||||
"go.linka.cloud/grpc-toolkit/internal/injectlogger"
|
||||
"go.linka.cloud/grpc-toolkit/logger"
|
||||
"go.linka.cloud/grpc-toolkit/registry"
|
||||
"go.linka.cloud/grpc-toolkit/registry/noop"
|
||||
@ -81,19 +82,35 @@ func newService(opts ...Option) (*service, error) {
|
||||
for _, f := range opts {
|
||||
f(s.opts)
|
||||
}
|
||||
s.opts.ctx, s.cancel = context.WithCancel(s.opts.ctx)
|
||||
|
||||
md := md(s.opts)
|
||||
if md != nil {
|
||||
s.opts.unaryServerInterceptors = append([]grpc.UnaryServerInterceptor{md.UnaryServerInterceptor()}, s.opts.unaryServerInterceptors...)
|
||||
s.opts.unaryClientInterceptors = append([]grpc.UnaryClientInterceptor{md.UnaryClientInterceptor()}, s.opts.unaryClientInterceptors...)
|
||||
s.opts.streamServerInterceptors = append([]grpc.StreamServerInterceptor{md.StreamServerInterceptor()}, s.opts.streamServerInterceptors...)
|
||||
s.opts.streamClientInterceptors = append([]grpc.StreamClientInterceptor{md.StreamClientInterceptor()}, s.opts.streamClientInterceptors...)
|
||||
s.opts.unaryServerInterceptors = append([]grpc.UnaryServerInterceptor{
|
||||
md.UnaryServerInterceptor(),
|
||||
injectlogger.New(s.opts.ctx).UnaryServerInterceptor()},
|
||||
s.opts.unaryServerInterceptors...,
|
||||
)
|
||||
s.opts.unaryClientInterceptors = append([]grpc.UnaryClientInterceptor{
|
||||
md.UnaryClientInterceptor(),
|
||||
injectlogger.New(s.opts.ctx).UnaryClientInterceptor()},
|
||||
s.opts.unaryClientInterceptors...,
|
||||
)
|
||||
s.opts.streamServerInterceptors = append([]grpc.StreamServerInterceptor{
|
||||
md.StreamServerInterceptor(),
|
||||
injectlogger.New(s.opts.ctx).StreamServerInterceptor()},
|
||||
s.opts.streamServerInterceptors...,
|
||||
)
|
||||
s.opts.streamClientInterceptors = append([]grpc.StreamClientInterceptor{
|
||||
md.StreamClientInterceptor(),
|
||||
injectlogger.New(s.opts.ctx).StreamClientInterceptor()},
|
||||
s.opts.streamClientInterceptors...,
|
||||
)
|
||||
}
|
||||
|
||||
if s.opts.error != nil {
|
||||
return nil, s.opts.error
|
||||
}
|
||||
s.opts.ctx, s.cancel = context.WithCancel(s.opts.ctx)
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
|
Reference in New Issue
Block a user