grpc.go 2.2 KB

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