gRPC Interceptors and Metadata
Auth, logging middleware and gRPC metadata
What are interceptors?
gRPC interceptors (middleware) run before or after RPC handlers. They add cross-cutting concerns: logging, auth token validation, metrics, panic recovery, and tracing.
Unary server interceptor
A unary server interceptor wraps the handler with additional logic:
func LoggingInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
log.Printf("RPC: %s", info.FullMethod)
resp, err := handler(ctx, req)
log.Printf("done: %v", err)
return resp, err
}
grpcSrv := grpc.NewServer(grpc.UnaryInterceptor(LoggingInterceptor))All lessons in this course
- Protocol Buffers and .proto Files
- Implementing a Unary gRPC Service
- Server and Client Streaming
- gRPC Interceptors and Metadata