gRPC Go Middleware: interceptors, helpers, utilities.
NOTE: V2 is under development. If you want to be up to date, or better (!) help us improve go-grpc-middleware please follow on grpc-ecosystem#275.
gRPC Go recently acquired support for Interceptors, i.e. middleware that is executed either on the gRPC Server before the request is passed onto the user's application logic, or on the gRPC client either around the user call. It is a perfect way to implement common patterns: auth, logging, message, validation, retries or monitoring.
These are generic building blocks that make it easy to build multiple microservices easily. The purpose of this repository is to act as a go-to point for such reusable functionality. It contains some of them itself, but also will link to useful external repos.
middleware
itself provides support for chaining interceptors, here's an example:
import "github.com/grpc-ecosystem/go-grpc-middleware/v2"
myServer := grpc.NewServer(
grpc.ChainStreamInterceptor(
opentracing.StreamServerInterceptor(),
prometheus.StreamServerInterceptor,
zap.StreamServerInterceptor(zapLogger),
auth.StreamServerInterceptor(myAuthFunction),
recovery.StreamServerInterceptor(),
),
grpc.ChainUnaryInterceptor(
opentracing.UnaryServerInterceptor(),
prometheus.UnaryServerInterceptor,
zap.UnaryServerInterceptor(zapLogger),
auth.UnaryServerInterceptor(myAuthFunction),
recovery.UnaryServerInterceptor(),
),
)
Please send a PR to add new interceptors or middleware to this list
auth
- a customizable (viaAuthFunc
) piece of auth middleware
zap
- integration of zap logging library into gRPC handlers.logrus
- integration of logrus logging library into gRPC handlers.kit
- integration of go-kit/log logging library into gRPC handlers.zerolog
- integration of zerolog logging Library into gRPC handlers.phuslog
- integration of phuslog logging Library into gRPC handlers.logr
- integration of logr logging Library into gRPC handlers.
grpc_prometheus
⚡ - Prometheus client-side and server-side monitoring middlewareopentracing
- OpenTracing client-side and server-side interceptors with support for streaming and handler-returned tags
retry
- a generic gRPC response code retry mechanism, client-side middlewaretimeout
- a generic gRPC request timeout, client-side middleware
validator
- codegen inbound message validation from.proto
optionsrecovery
- turn panics into gRPC errorsratelimit
- grpc rate limiting by your own limiter
skip
- allow users to skip interceptors in certain condition.
This code has been running in production since May 2016 as the basis of the gRPC micro services stack at Improbable.
Additional tooling will be added, and contributions are welcome.
go-grpc-middleware
is released under the Apache 2.0 license. See the LICENSE file for details.