Middleware For RPC
An interceptor is a hook that runs around every gRPC call, on the client side, the server side, or both. It is the gRPC equivalent of HTTP middleware.
What They Wrap
An interceptor sits between the stub and the real handler.
- It runs before the call, where it can read or add metadata.
- It calls the next handler in the chain.
- It runs after the call, where it can inspect the result or status.
Common Uses
Interceptors centralize concerns that would otherwise be copied into every method.
- Authentication by checking a token in the metadata.
- Logging and metrics for latency and error rates.
- Retries for failed calls on the client side.
- Tracing by propagating a trace context.
Interceptors can be chained, so order matters. An authentication interceptor should usually run before a logging one so that rejected calls are still recorded. Keeping these concerns in interceptors keeps the actual service methods focused on business logic.
Key idea
gRPC interceptors wrap every call with reusable logic for auth, logging, retries, and tracing, keeping cross cutting concerns out of the service methods while order in the chain still matters.