github.com/moby/docker@v26.1.3+incompatible/api/server/router/grpc/grpc.go (about) 1 package grpc // import "github.com/docker/docker/api/server/router/grpc" 2 3 import ( 4 "context" 5 "strings" 6 7 "github.com/docker/docker/api/server/router" 8 grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware" 9 "github.com/moby/buildkit/util/grpcerrors" 10 "go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc" 11 "golang.org/x/net/http2" 12 "google.golang.org/grpc" 13 ) 14 15 type grpcRouter struct { 16 routes []router.Route 17 grpcServer *grpc.Server 18 h2Server *http2.Server 19 } 20 21 // NewRouter initializes a new grpc http router 22 func NewRouter(backends ...Backend) router.Router { 23 unary := grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(unaryInterceptor(), grpcerrors.UnaryServerInterceptor)) 24 stream := grpc.StreamInterceptor(grpc_middleware.ChainStreamServer(otelgrpc.StreamServerInterceptor(), grpcerrors.StreamServerInterceptor)) //nolint:staticcheck // TODO(thaJeztah): ignore SA1019 for deprecated options: see https://github.com/moby/moby/issues/47437 25 26 r := &grpcRouter{ 27 h2Server: &http2.Server{}, 28 grpcServer: grpc.NewServer(unary, stream), 29 } 30 for _, b := range backends { 31 b.RegisterGRPC(r.grpcServer) 32 } 33 r.initRoutes() 34 return r 35 } 36 37 // Routes returns the available routers to the session controller 38 func (gr *grpcRouter) Routes() []router.Route { 39 return gr.routes 40 } 41 42 func (gr *grpcRouter) initRoutes() { 43 gr.routes = []router.Route{ 44 router.NewPostRoute("/grpc", gr.serveGRPC), 45 } 46 } 47 48 func unaryInterceptor() grpc.UnaryServerInterceptor { 49 withTrace := otelgrpc.UnaryServerInterceptor() //nolint:staticcheck // TODO(thaJeztah): ignore SA1019 for deprecated options: see https://github.com/moby/moby/issues/47437 50 51 return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) { 52 // This method is used by the clients to send their traces to buildkit so they can be included 53 // in the daemon trace and stored in the build history record. This method can not be traced because 54 // it would cause an infinite loop. 55 if strings.HasSuffix(info.FullMethod, "opentelemetry.proto.collector.trace.v1.TraceService/Export") { 56 return handler(ctx, req) 57 } 58 return withTrace(ctx, req, info, handler) 59 } 60 }