Middleware
- rpclog
- duration
- tags
- validator
- errors
- opentelemetry
- opentracing
- go-concurrency-limits
Get
go get github.com/xmlking/toolkit
Usage
Interceptors will be executed from left to right: e.g., logging, monitoring and auth.
grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(loggingUnary, monitoringUnary, authUnary),)
Add interceptors in following order
- Around interceptors - from outer to inner — e.g., duration, retry
- Before interceptors - rate-limit, auth, validation , tagging
- After interceptors - rpclog, translog, recovery
import (
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
grpc_validator "github.com/grpc-ecosystem/go-grpc-middleware/validator"
"github.com/xmlking/toolkit/middleware/rpclog"
)
server := grpc.NewServer(
grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(
// Execution is done in left-to-right order
grpc_validator.UnaryServerInterceptor(),
// keep it last in the interceptor chain
rpclog.UnaryServerInterceptor(rpclog.WithExcludeMethods("/grpc.health.v1.Health/Check", "/api.MyService/*")),
)),
grpc.StreamInterceptor(grpc_middleware.ChainStreamServer(
// keep it last in the interceptor chain
rpclog.StreamServerInterceptor()
)),
)
concurrency-limits
import (
gclGrpc "github.com/platnummonkey/go-concurrency-limits/grpc"
)
// setup grpc server with this option
serverOption := grpc.UnaryInterceptor(
gclGrpc.UnaryServerInterceptor(
gclGrpc.WithLimiter(...),
gclGrpc.WithServerResponseTypeClassifier(..),
),
)
// setup grpc client with this option
dialOption := grpc.WithUnaryInterceptor(
gclGrpc.UnaryClientInterceptor(
gclGrpc.WithLimiter(...),
gclGrpc.WithClientResponseTypeClassifier(...),
),
)