grpc.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. "github.com/moby/buildkit/util/tracing/detect"
  9. "github.com/sirupsen/logrus"
  10. "go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
  11. "go.opentelemetry.io/otel/propagation"
  12. "go.opentelemetry.io/otel/trace"
  13. "golang.org/x/net/http2"
  14. "google.golang.org/grpc"
  15. )
  16. func init() {
  17. // enable in memory recording for grpc traces
  18. detect.Recorder = detect.NewTraceRecorder()
  19. }
  20. type grpcRouter struct {
  21. routes []router.Route
  22. grpcServer *grpc.Server
  23. h2Server *http2.Server
  24. }
  25. var propagators = propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{})
  26. // NewRouter initializes a new grpc http router
  27. func NewRouter(backends ...Backend) router.Router {
  28. tp, err := detect.TracerProvider()
  29. if err != nil {
  30. logrus.WithError(err).Error("failed to detect trace provider")
  31. }
  32. opts := []grpc.ServerOption{grpc.UnaryInterceptor(grpcerrors.UnaryServerInterceptor), grpc.StreamInterceptor(grpcerrors.StreamServerInterceptor)}
  33. if tp != nil {
  34. streamTracer := otelgrpc.StreamServerInterceptor(otelgrpc.WithTracerProvider(tp), otelgrpc.WithPropagators(propagators))
  35. unary := grpc_middleware.ChainUnaryServer(unaryInterceptor(tp), grpcerrors.UnaryServerInterceptor)
  36. stream := grpc_middleware.ChainStreamServer(streamTracer, grpcerrors.StreamServerInterceptor)
  37. opts = []grpc.ServerOption{grpc.UnaryInterceptor(unary), grpc.StreamInterceptor(stream)}
  38. }
  39. r := &grpcRouter{
  40. h2Server: &http2.Server{},
  41. grpcServer: grpc.NewServer(opts...),
  42. }
  43. for _, b := range backends {
  44. b.RegisterGRPC(r.grpcServer)
  45. }
  46. r.initRoutes()
  47. return r
  48. }
  49. // Routes returns the available routers to the session controller
  50. func (gr *grpcRouter) Routes() []router.Route {
  51. return gr.routes
  52. }
  53. func (gr *grpcRouter) initRoutes() {
  54. gr.routes = []router.Route{
  55. router.NewPostRoute("/grpc", gr.serveGRPC),
  56. }
  57. }
  58. func unaryInterceptor(tp trace.TracerProvider) grpc.UnaryServerInterceptor {
  59. withTrace := otelgrpc.UnaryServerInterceptor(otelgrpc.WithTracerProvider(tp), otelgrpc.WithPropagators(propagators))
  60. return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
  61. // This method is used by the clients to send their traces to buildkit so they can be included
  62. // in the daemon trace and stored in the build history record. This method can not be traced because
  63. // it would cause an infinite loop.
  64. if strings.HasSuffix(info.FullMethod, "opentelemetry.proto.collector.trace.v1.TraceService/Export") {
  65. return handler(ctx, req)
  66. }
  67. return withTrace(ctx, req, info, handler)
  68. }
  69. }