Go gRPC Middleware
This repository holds gRPC Go Middlewares: interceptors, helpers and utilities.
Middleware
gRPC Go has 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, tracing, metrics, validation, retries, rate limiting and more, which can be a great generic building blocks that make it easy to build multiple microservices easily.
Especially for observability signals (logging, tracing, metrics) interceptors offers semi-auto-instrumentation that improves consistency of your observability and allows great correlation techniques (e.g. exemplars and trace ID in logs). Demo-ed in examples.
This repository offers ready-to-use middlewares that implements gRPC interceptors with examples. In some cases dedicated projects offer great interceptors, so this repository skips those, and we link them in the interceptors list.
NOTE: Some middlewares are quite simple to write, so feel free to use this repo as template if you need. It's ok to copy some simpler interceptors if you need more flexibility. This repo can't support all the edge cases you might have.
Additional great feature of interceptors is the fact we can chain those. For example below you can find example server side chain of interceptors with full observabiliy correlation, auth and panic recovery:
grpcSrv := grpc.NewServer(
grpc.StatsHandler(otelgrpc.NewServerHandler()),
grpc.ChainUnaryInterceptor(
srvMetrics.UnaryServerInterceptor(
grpcprom.WithExemplarFromContext(exemplarFromContext),
grpcprom.WithLabelsFromContext(labelsFromContext),
),
logging.UnaryServerInterceptor(interceptorLogger(rpcLogger), logging.WithFieldsFromContext(logTraceID)),
selector.UnaryServerInterceptor(auth.UnaryServerInterceptor(authFn), selector.MatchFunc(allButHealthZ)),
recovery.UnaryServerInterceptor(recovery.WithRecoveryHandler(grpcPanicRecoveryHandler)),
),
grpc.ChainStreamInterceptor(
srvMetrics.StreamServerInterceptor(
grpcprom.WithExemplarFromContext(exemplarFromContext),
grpcprom.WithLabelsFromContext(labelsFromContext),
),
logging.StreamServerInterceptor(interceptorLogger(rpcLogger), logging.WithFieldsFromContext(logTraceID)),
selector.StreamServerInterceptor(auth.StreamServerInterceptor(authFn), selector.MatchFunc(allButHealthZ)),
recovery.StreamServerInterceptor(recovery.WithRecoveryHandler(grpcPanicRecoveryHandler)),
),
)
This pattern offers clean and explicit shared functionality for all your gRPC methods. Full, buildable examples can be found in examples directory.
Interceptors
This list covers known interceptors that users use for their Go microservices (both in this repo and external). Click on each to see extended examples in examples_test.go (also available in pkg.go.dev)
All paths should work with go get .
Auth
github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/auth- a customizable viaAuthFuncpiece of auth middleware.- (external)
google.golang.org/grpc/authz- more complex, customizable via auth polices (RBAC like), piece of auth middleware.
Observability
- Metrics:
github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus⚡ - Prometheus client-side and server-side monitoring middleware. Supports exemplars. Moved from deprecated nowgo-grpc-prometheus.- (external)
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc- official OpenTelemetry interceptors (metric and tracing).
- Logging with
github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging- a customizable logging middleware offering extended per request logging. It requires logging adapter, see examples ininterceptors/logging/examplesforgo-kit,log,logr,logrus,slog,zapandzerolog.- NOTE: Interceptors with context field injections need to be chained before the adapter function.
- Tracing:
- (external)
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc- official OpenTelemetry interceptors (metric and tracing) as used in example. - (external)
github.com/grpc-ecosystem/go-grpc-middleware/tracing/opentracing- deprecated OpenTracing client-side and server-side interceptors if you still need it!
- (external)
Client
github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/retry- a generic gRPC response code retry mechanism, client-side middleware.- NOTE: grpc-go has native retries too with advanced policies (https://github.com/grpc/grpc-go/blob/v1.54.0/examples/features/retry/client/main.go)
github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/timeout- a generic gRPC request timeout, client-side middleware.
Server
github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/validator- codegen inbound message validation from.protooptions.github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/recovery- turn panics into gRPC errors (make sure to use those as "last" interceptor, so panic does not skip other interceptors).github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/ratelimit- grpc rate limiting by your own limiter.github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/protovalidate- message validation from.protooptions via protovalidate-go
Filtering Interceptor
github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/selector- allow users to select given one or more interceptors in certain condition like matching service method.
Prerequisites
Structure of this repository
The main interceptors are available in the subdirectories of the interceptors directory e.g. interceptors/validator, interceptors/auth or interceptors/logging.
Some interceptors or utilities of interceptors requires opinionated code that depends on larger amount of dependencies. Those are places in providers directory as separate Go module, with separate versioning. For example providers/prometheus offer metrics middleware (there is no "interceptor/metrics" at the moment). The separate module, might be a little bit harder to discover and version in your go.mod, but it allows core interceptors to be ultra slim in terms of dependencies.
The interceptors directory also holds generic interceptors that accepts Reporter interface which allows creating your own middlewares with ease.
As you might notice this repository contains multiple modules with different versions (Go Module specifics). Refer to versions.yaml for current modules. We have main module of version 2.x.y and providers mo