Implementation:Bentoml BentoML GRPC Access Interceptor
| Knowledge Sources | |
|---|---|
| Domains | gRPC, Logging, Observability |
| Last Updated | 2026-02-13 15:00 GMT |
Overview
Implements an asyncio gRPC server interceptor that logs access information for each unary-unary RPC call, including request/response metadata and latency.
Description
The AccessLogServerInterceptor class extends aio.ServerInterceptor to provide structured access logging for BentoML's gRPC server. For each non-streaming RPC call, it wraps the handler behavior to capture timing information, request size, response size, HTTP status (converted from gRPC status), and gRPC status code. Logs are emitted via the bentoml.access logger at INFO level with a format that includes the peer address, request details (scheme, path, content-type, size), response details (HTTP status, gRPC status, content-type, size), and latency in milliseconds. The interceptor skips streaming RPCs (both request and response streaming). It uses the wrap_rpc_handler utility to apply the wrapper function to the appropriate handler method. Error handling catches all exceptions, sets the gRPC context code to INTERNAL, and still logs the access entry in the finally block.
Usage
Use this interceptor when you need access logging for BentoML gRPC services. It should be added to the server after the OpenTelemetry and Prometheus interceptors to ensure proper context code availability.
Code Reference
Source Location
- Repository: Bentoml_BentoML
- File: src/bentoml/grpc/interceptors/access.py
- Lines: 1-100
Signature
class AccessLogServerInterceptor(aio.ServerInterceptor):
async def intercept_service(
self,
continuation: t.Callable[[HandlerCallDetails], t.Awaitable[RpcMethodHandler]],
handler_call_details: HandlerCallDetails,
) -> RpcMethodHandler: ...
Import
from bentoml.grpc.interceptors.access import AccessLogServerInterceptor
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| continuation | Callable | Yes | Callback to obtain the next RPC method handler in the chain |
| handler_call_details | HandlerCallDetails | Yes | Details of the incoming RPC call including method name |
Outputs
| Name | Type | Description |
|---|---|---|
| RpcMethodHandler | RpcMethodHandler | The wrapped handler that logs access information before/after the RPC |
| (side effect) | Log entry | Access log written to bentoml.access logger with peer, request, response, and latency |
Usage Examples
from bentoml.grpc.interceptors.access import AccessLogServerInterceptor
from grpc import aio
# Create a gRPC server with the access log interceptor
server = aio.server(
interceptors=[
# OpenTelemetry interceptor first
# Prometheus interceptor second
AccessLogServerInterceptor(), # Access log last
]
)