Go gRPC Middleware V2
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 https://github.com/grpc-ecosystem/go-grpc-middleware/issues/275.
Middleware
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(
tags.StreamServerInterceptor(),
opentracing.StreamServerInterceptor(),
prometheus.StreamServerInterceptor,
zap.StreamServerInterceptor(zapLogger),
auth.StreamServerInterceptor(myAuthFunction),
recovery.StreamServerInterceptor(),
),
grpc.ChainUnaryInterceptor(
tags.UnaryServerInterceptor(),
opentracing.UnaryServerInterceptor(),
prometheus.UnaryServerInterceptor,
zap.UnaryServerInterceptor(zapLogger),
auth.UnaryServerInterceptor(myAuthFunction),
recovery.UnaryServerInterceptor(),
),
)
Interceptors
Please send a PR to add new interceptors or middleware to this list
Auth
auth
- a customizable (via AuthFunc
) piece of auth middleware
Logging
tags
- a library that adds a Tag
map to context, with data populated from request bodyzap
- integration of zap logging library into gRPC handlers.logrus
- integration of logrus logging library into gRPC handlers.kit
- integration of go-kit logging library into gRPC handlers.zerolog
- integration of zerolog logging Library into gRPC handlers.
Monitoring
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
Client
retry
- a generic gRPC response code retry mechanism, client-side middlewaretimeout
- a generic gRPC request timeout, client-side middleware
Server
validator
- codegen inbound message validation from .proto
optionsrecovery
- turn panics into gRPC errorsratelimit
- grpc rate limiting by your own limiter
Utility
skip
- allow users to skip interceptors in certain condition.
Status
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.
License
go-grpc-middleware
is released under the Apache 2.0 license. See the LICENSE file for details.