middleware

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 4, 2026 License: MIT Imports: 33 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

go/grpc/middleware/errors.go

Index

Constants

This section is empty.

Variables

View Source
var (
	AllButHealth = selector.MatchFunc(func(ctx context.Context, callMeta interceptors.CallMeta) bool {
		return grpc_health_v1.Health_ServiceDesc.ServiceName != callMeta.Service
	})
)

Functions

func AppendToOutgoingContextWithPropagation

func AppendToOutgoingContextWithPropagation(ctx context.Context, key, value string, opts ...PropagateOption) context.Context

func AppendTrailer

func AppendTrailer(ctx context.Context, key string, values ...string) bool

Inject a trailer into the context. Returns false if the context tag is not found.

func AppendTrailerWithPropagation

func AppendTrailerWithPropagation(ctx context.Context, key string, values ...string) bool

Use this method if you want to add a trailer to be propagated up the chain of calls.

func GetFields

func GetFields(ctx context.Context) ([]any, bool)

func InjectLogFields

func InjectLogFields(ctx context.Context, args ...any) bool

Inject fields onto the log context tag.

func MethodDescriptorFromContext

func MethodDescriptorFromContext(ctx context.Context) (protoreflect.MethodDescriptor, bool)

func MustParseFullMethod

func MustParseFullMethod(fullMethod string) (service, method string)

MustParseFullMethod splits a gRPC full method name "/package.Service/Method" into its service and method parts, panicking if it is malformed.

Use this for names supplied by your own code — typically the generated *_FullMethodName constants — so a bad value fails at startup. For names that arrive off the wire, use splitFullMethod instead.

func RegisterHookHandler

func RegisterHookHandler[T proto.Message](handler HookHandler[T], opts ...HookOption) error

RegisterHookHandler registers a typed hook handler for the proto message type T. The lookup key is derived from T's proto descriptor full name. At least one of WithOnRequest or WithOnResponse must be provided to specify when the hook is invoked. The generic type parameter T ensures type safety: the handler will only be called with messages of the correct type. This function is typically called during init().

func StreamClientRetry

func StreamClientRetry() grpc.StreamClientInterceptor

StreamClientRetry returns a grpc retry interceptor.

func StreamClientTrailerPropagation

func StreamClientTrailerPropagation() grpc.StreamClientInterceptor

StreamClientTrailerPropagation automatically captures trailers and sets them onto to 'context' tag if they're propagatable.

func StreamServerAIPLogging

func StreamServerAIPLogging() grpc.StreamServerInterceptor

func StreamServerCanonicalize

func StreamServerCanonicalize() grpc.StreamServerInterceptor

func StreamServerDebugInfoScrubber

func StreamServerDebugInfoScrubber() grpc.StreamServerInterceptor

func StreamServerErrorInfoInjector

func StreamServerErrorInfoInjector() grpc.StreamServerInterceptor

func StreamServerFieldMask

func StreamServerFieldMask() grpc.StreamServerInterceptor

func StreamServerHeaderPropagation

func StreamServerHeaderPropagation() grpc.StreamServerInterceptor

func StreamServerHook

func StreamServerHook() grpc.StreamServerInterceptor

StreamServerHook returns a stream server interceptor that invokes hooks on streamed messages. Request hooks are invoked on received messages; response hooks are invoked on sent messages. If no registrations match the stream's method, the stream is passed through unwrapped.

func StreamServerLogContextTagInitializer

func StreamServerLogContextTagInitializer() grpc.StreamServerInterceptor

StreamServerContextTagsInterceptor initializes context tags.

func StreamServerLogging

func StreamServerLogging(logger *slog.Logger) grpc.StreamServerInterceptor

func StreamServerMethodDescriptor

func StreamServerMethodDescriptor() grpc.StreamServerInterceptor

func StreamServerTrailerPropagation

func StreamServerTrailerPropagation() grpc.StreamServerInterceptor

StreamServerTrailer sets a trailers tag onto the context and sends any trailers back to the client.

func StreamServerValidate

func StreamServerValidate(validator protovalidate.Validator) grpc.StreamServerInterceptor

func UnaryClientRetry

func UnaryClientRetry() grpc.UnaryClientInterceptor

UnaryClientRetry returns a gRPC DialOption that adds a default retrying interceptor to all unary RPC calls. Only retries on ResourceExhausted and Unavailable errors.

func UnaryClientTimeout

func UnaryClientTimeout() grpc.UnaryClientInterceptor

func UnaryClientTrailerPropagation

func UnaryClientTrailerPropagation() grpc.UnaryClientInterceptor

UnaryClientTrailerPropagation automatically captures trailers and sets them onto to 'context' tag if they're propagatable. This is only done on 'ctx' that have a trailer tag set. GRPC Servers that use the Trailer Interceptor will have that set.

func UnaryClientValidate

func UnaryClientValidate(validator protovalidate.Validator) grpc.UnaryClientInterceptor

unaryClientValidateInterceptor returns a new unary client interceptor that validates outgoing messages. Invalid messages will be rejected with `InvalidArgument` before sending the request to server.

func UnaryServerAIPLogging

func UnaryServerAIPLogging() grpc.UnaryServerInterceptor

func UnaryServerCanonicalize

func UnaryServerCanonicalize() grpc.UnaryServerInterceptor

func UnaryServerDebugInfoScrubber

func UnaryServerDebugInfoScrubber() grpc.UnaryServerInterceptor

func UnaryServerErrorInfoInjector

func UnaryServerErrorInfoInjector() grpc.UnaryServerInterceptor

func UnaryServerFieldMask

func UnaryServerFieldMask() grpc.UnaryServerInterceptor

func UnaryServerHeaderPropagation

func UnaryServerHeaderPropagation() grpc.UnaryServerInterceptor

func UnaryServerHook

func UnaryServerHook() grpc.UnaryServerInterceptor

UnaryServerHook returns a unary server interceptor that invokes hooks on request and response messages. For requests, hooks registered with WithOnRequest are invoked before the handler. For responses, hooks registered with WithOnResponse are invoked after the handler succeeds. Matchers are evaluated once per RPC; if no registrations match for a direction, the message tree walk is skipped entirely for that direction.

func UnaryServerLogContextTagInitializer

func UnaryServerLogContextTagInitializer() grpc.UnaryServerInterceptor

UnaryServerContextTagsInterceptor initializes context tags.

func UnaryServerLogging

func UnaryServerLogging(logger *slog.Logger) grpc.UnaryServerInterceptor

func UnaryServerMethodDescriptor

func UnaryServerMethodDescriptor() grpc.UnaryServerInterceptor

func UnaryServerTrailerPropagation

func UnaryServerTrailerPropagation() grpc.UnaryServerInterceptor

UnaryServerTrailerPropagation sets a trailers tag onto the context and sends any trailers back to the client.

func UnaryServerValidate

func UnaryServerValidate(validator protovalidate.Validator) grpc.UnaryServerInterceptor

func WithFieldMask

func WithFieldMask(ctx context.Context, paths string) context.Context

func WithFieldMaskStrict

func WithFieldMaskStrict(ctx context.Context, paths string) context.Context

func WithReadMask

func WithReadMask(ctx context.Context, paths string) context.Context

func WithReadMaskStrict

func WithReadMaskStrict(ctx context.Context, paths string) context.Context

Types

type HookHandler

type HookHandler[T proto.Message] func(ctx context.Context, message T) error

HookHandler is a typed function that processes a proto message when a hook is triggered. The handler receives the context and the specific proto message type it was registered for.

type HookMatcher

type HookMatcher func(ctx context.Context, methodFullName string) bool

HookMatcher determines whether a hook should be invoked for a given RPC. It receives the context and the gRPC full method name (e.g. "/package.Service/Method"), which is exactly the value of the generated *_FullMethodName constants. Returning true means the hook will be invoked; false means it will be skipped.

func MatchFullMethods

func MatchFullMethods(fullMethods ...string) HookMatcher

MatchFullMethods returns a HookMatcher that matches if the gRPC full method name is in the given set.

Pass the generated *_FullMethodName constants (e.g. pb.UserService_GetUser_FullMethodName) so a renamed or removed method is a compile error rather than a matcher that silently stops firing.

func MatchMethods

func MatchMethods(methods ...string) HookMatcher

MatchMethods returns a HookMatcher that matches if the RPC method is in the given set.

Methods are bare names, e.g. "GetUser".

func MatchServices

func MatchServices(services ...string) HookMatcher

MatchServices returns a HookMatcher that matches if the RPC service is in the given set.

Services are dotted full names, e.g. "package.Service".

type HookOption

type HookOption func(*hookRegistration)

HookOption configures optional behavior for a hook registration.

func WithHookMatchers

func WithHookMatchers(matchers ...HookMatcher) HookOption

WithHookMatchers sets matcher functions on a hook registration. Can be called multiple times to add additional matchers. All matchers must return true for the hook to be invoked (AND semantics). If no matchers are set, the hook is invoked for all RPCs that contain the registered message type.

func WithHookOnRequest

func WithHookOnRequest() HookOption

WithHookOnRequest configures the hook to be invoked when the registered message type appears anywhere in the request message tree of an RPC.

func WithHookOnResponse

func WithHookOnResponse() HookOption

WithHookOnResponse configures the hook to be invoked when the registered message type appears anywhere in the response message tree of an RPC.

type PropagateOption

type PropagateOption func(*propagateOptions)

func WithHops

func WithHops(n int) PropagateOption

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL