grpc.go 869 B

123456789101112131415161718192021222324252627282930
  1. package common
  2. import (
  3. "github.com/grpc-ecosystem/go-grpc-middleware"
  4. "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
  5. "github.com/grpc-ecosystem/go-grpc-middleware/tags"
  6. "github.com/grpc-ecosystem/go-grpc-prometheus"
  7. "go.uber.org/zap"
  8. "google.golang.org/grpc"
  9. )
  10. func NewInstrumentedGRPCServer(logger *zap.Logger) *grpc.Server {
  11. server := grpc.NewServer(
  12. grpc.StreamInterceptor(grpc_middleware.ChainStreamServer(
  13. grpc_ctxtags.StreamServerInterceptor(),
  14. grpc_prometheus.StreamServerInterceptor,
  15. grpc_zap.StreamServerInterceptor(logger),
  16. )),
  17. grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(
  18. grpc_ctxtags.UnaryServerInterceptor(),
  19. grpc_prometheus.UnaryServerInterceptor,
  20. grpc_zap.UnaryServerInterceptor(logger),
  21. )),
  22. )
  23. grpc_prometheus.EnableHandlingTimeHistogram()
  24. grpc_prometheus.Register(server)
  25. return server
  26. }