Skip to content

Compose gRPC interceptors

transit's grpc package mirrors the HTTP package for gRPC: server interceptors you install on a grpc.Server, and client instrumentation you pass as dial options and client interceptors.

import transitgrpc "gitlab.com/phpboyscout/go/transit/grpc"

For every field, default and clamping rule behind the constructors below, see the gRPC reference.

Server interceptors

Server interceptors are grouped as an Interceptor{Unary, Stream} pair (either field may be nil). NewInterceptorChain collects them and ServerOptions() turns the chain into the grpc.ServerOption values that install it via ChainUnaryInterceptor / ChainStreamInterceptor.

Constructor Returns Purpose
LoggingInterceptor(log, opts…) Interceptor One structured record per RPC.
RateLimitInterceptor(log, cfg) Interceptor Token-bucket admission control.
RecoveryInterceptor(log, opts…) Interceptor Fails a panicking call with Internal instead of ending the process.
OTelStatsHandler(opts…) grpc.ServerOption OpenTelemetry server spans + rpc.server.* metrics.

OTelStatsHandler is a stats handler, not an interceptor (that is the shape the OTel gRPC contrib library ships), so pass it to grpc.NewServer alongside the chain's options rather than into NewInterceptorChain:

chain := transitgrpc.NewInterceptorChain(
    transitgrpc.LoggingInterceptor(log),
    transitgrpc.RateLimitInterceptor(log, transitgrpc.DefaultRateLimitConfig()),
)

opts := append(chain.ServerOptions(), transitgrpc.OTelStatsHandler())
srv := grpc.NewServer(opts...)

LoggingInterceptor takes WithGRPCLogLevel, WithGRPCPathFilter and WithoutGRPCLatency, mirroring the HTTP logger. It does not mirror its level policy: every code other than OK logs at ERROR, including NotFound and InvalidArgument, where the HTTP logger reserves ERROR for 5xx. On a service where "not found" is a normal answer, expect that to dominate your error rate.

Recover from a panicking handler

grpc-go does not recover a panic, so without this a panicking handler ends the process. Add RecoveryInterceptor as the first and the last entry of the chain:

recovery := transitgrpc.RecoveryInterceptor(log)

chain := transitgrpc.NewInterceptorChain(
    recovery,
    transitgrpc.LoggingInterceptor(log),
    transitgrpc.RateLimitInterceptor(log, transitgrpc.DefaultRateLimitConfig()),
    recovery,
)

Each end catches what the other cannot:

  • The last entry is next to the handler, so it catches a handler's panic and returns an ordinary Internal error. The interceptors before it see a failed call, so LoggingInterceptor still writes its "rpc completed" record, with code=Internal.
  • The first entry catches a panic in one of the interceptors themselves, which the last cannot see. LoggingInterceptor does not defer its record, so a panic it is unwound past leaves only the "rpc panicked" line.

A panic is caught by whichever end is nearer, so it is logged once. Both ends together cost a few nanoseconds per call.

To count panics or send them to an error tracker, pass the same WithOnPanic to both ends:

recovery := transitgrpc.RecoveryInterceptor(log, transitgrpc.WithOnPanic(
    func(ctx context.Context, r transitgrpc.PanicReport) {
        panicsTotal.WithLabelValues(r.Method).Inc()
    }))

Treat every "rpc panicked" line as a bug to fix. Its stack field names the line that panicked. Recovery keeps the server up while you fix it; it is not the fix.

Two panics stay out of reach: one on a goroutine the handler starts, which has to recover itself, and one in a raw grpc.UnaryInterceptor server option, which grpc-go runs outside the whole chain.

Client instrumentation

On the client, OpenTelemetry is a dial option and the circuit breaker is a standard gRPC client interceptor:

Constructor Returns Purpose
OTelClientHandler(opts…) grpc.DialOption Client spans + trace-context propagation.
CircuitBreakerInterceptor(log, cfg) grpc.UnaryClientInterceptor Fail fast on a consistently failing server.
CircuitBreakerStreamInterceptor(log, cfg) grpc.StreamClientInterceptor The streaming equivalent: successful establishment is the breaker verdict; classified per-message errors still count against it.
conn, err := grpc.NewClient(
    "dns:///orders.internal:443",
    grpc.WithTransportCredentials(creds),
    transitgrpc.OTelClientHandler(),
    grpc.WithUnaryInterceptor(
        transitgrpc.CircuitBreakerInterceptor(log, transitgrpc.DefaultCircuitBreakerConfig()),
    ),
)

OTelClientHandler injects the trace context into outgoing metadata using the global propagator, so a downstream gRPC server continues the same trace rather than starting a new one. Like the server handler, it reads the globally-installed providers and is a noop until those are set up (see go/observability).

The unary and stream breakers are two separate breakers

CircuitBreakerInterceptor and CircuitBreakerStreamInterceptor each build their own state machine. Installing both on one connection means unary failures never trip the stream breaker and vice versa. Each needs its own run of FailureThreshold consecutive failures.

There is no gRPC counterpart to the HTTP NewRetryTransport: gRPC implements retry in the SDK, as a service config policy set with grpc.WithDefaultServiceConfig. Because that retry happens beneath the interceptor chain, the HTTP rule about placing the breaker outside retry does not apply here: a retried call reaches the breaker as one verdict whatever the interceptor order.

Config merging

Both CircuitBreakerConfig and RateLimitConfig have Merge…Config helpers that apply only explicitly-flagged override fields onto a base, leaving function fields (custom failure predicates, key extractors) under your control. This is how a service layers file/env/flag config onto the defaults without a field-by-field copy.