Help us shape the future of Go! Take the 2026 Go Developer Survey

interceptors

package module
v0.1.16 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2026 License: Apache-2.0 Imports: 25 Imported by: 1

README

CI Go Report Card GoDoc

interceptors

import "github.com/go-coldbrew/interceptors"

Package interceptors provides gRPC server and client interceptors for the ColdBrew framework.

Interceptor configuration functions (AddUnaryServerInterceptor, SetFilterFunc, etc.) must be called during program initialization, before the gRPC server starts. They are not safe for concurrent use.

Index

Constants

SupportPackageIsVersion1 is a compile-time assertion constant. Downstream packages (e.g. core) reference this constant to enforce version compatibility. When interceptors makes a breaking change, export a new constant and remove this one to force coordinated updates.

const SupportPackageIsVersion1 = true

Variables

var (
    //FilterMethods is the list of methods that are filtered by default
    FilterMethods = []string{"healthcheck", "readycheck", "serverreflectioninfo"}
)

func AddStreamClientInterceptor

func AddStreamClientInterceptor(ctx context.Context, i ...grpc.StreamClientInterceptor)

AddStreamClientInterceptor adds a client stream interceptor to default client stream interceptors. Must be called during initialization, before any RPCs are made. Not safe for concurrent use.

func AddStreamServerInterceptor

func AddStreamServerInterceptor(ctx context.Context, i ...grpc.StreamServerInterceptor)

AddStreamServerInterceptor adds a server interceptor to default server interceptors. Must be called during initialization, before the server starts. Not safe for concurrent use.

func AddUnaryClientInterceptor

func AddUnaryClientInterceptor(ctx context.Context, i ...grpc.UnaryClientInterceptor)

AddUnaryClientInterceptor adds a client interceptor to default client interceptors. Must be called during initialization, before any RPCs are made. Not safe for concurrent use.

func AddUnaryServerInterceptor

func AddUnaryServerInterceptor(ctx context.Context, i ...grpc.UnaryServerInterceptor)

AddUnaryServerInterceptor adds a server interceptor to default server interceptors. Must be called during initialization, before the server starts. Not safe for concurrent use.

func DebugLoggingInterceptor

func DebugLoggingInterceptor() grpc.UnaryServerInterceptor

DebugLoggingInterceptor is the interceptor that logs all request/response from a handler

func DefaultClientInterceptor

func DefaultClientInterceptor(defaultOpts ...interface{}) grpc.UnaryClientInterceptor

DefaultClientInterceptor are the set of default interceptors that should be applied to all client calls

func DefaultClientInterceptors

func DefaultClientInterceptors(defaultOpts ...interface{}) []grpc.UnaryClientInterceptor

DefaultClientInterceptors are the set of default interceptors that should be applied to all client calls

func DefaultClientStreamInterceptor

func DefaultClientStreamInterceptor(defaultOpts ...interface{}) grpc.StreamClientInterceptor

DefaultClientStreamInterceptor are the set of default interceptors that should be applied to all stream client calls

func DefaultClientStreamInterceptors

func DefaultClientStreamInterceptors(defaultOpts ...interface{}) []grpc.StreamClientInterceptor

DefaultClientStreamInterceptors are the set of default interceptors that should be applied to all stream client calls

func DefaultInterceptors

func DefaultInterceptors() []grpc.UnaryServerInterceptor

DefaultInterceptors are the set of default interceptors that are applied to all coldbrew methods

func DefaultStreamInterceptors

func DefaultStreamInterceptors() []grpc.StreamServerInterceptor

DefaultStreamInterceptors are the set of default interceptors that should be applied to all coldbrew streams

func DoHTTPtoGRPC

func DoHTTPtoGRPC(ctx context.Context, svr interface{}, handler func(ctx context.Context, req interface{}) (interface{}, error), in interface{}) (interface{}, error)

func FilterMethodsFunc

func FilterMethodsFunc(ctx context.Context, fullMethodName string) bool

FilterMethodsFunc is the default implementation of Filter function

func GRPCClientInterceptor

func GRPCClientInterceptor(options ...interface{}) grpc.UnaryClientInterceptor

Deprecated: GRPCClientInterceptor is no longer needed. gRPC tracing is now handled by otelgrpc.NewClientHandler stats handler configured at the client level. This function is retained for backwards compatibility but returns a no-op interceptor.

func HystrixClientInterceptor

func HystrixClientInterceptor(defaultOpts ...grpc.CallOption) grpc.UnaryClientInterceptor

HystrixClientInterceptor returns a unary client interceptor that executes the RPC inside a Hystrix command.

Note: This interceptor wraps github.com/afex/hystrix-go which has been unmaintained since 2018. Consider migrating to github.com/failsafe-go/failsafe-go for circuit breaker functionality.

The interceptor applies provided default and per-call client options to configure Hystrix behavior (for example the command name, disabled flag, excluded errors, and excluded gRPC status codes). If Hystrix is disabled via options, the RPC is invoked directly. If the underlying RPC returns an error that matches any configured excluded error or whose gRPC status code matches any configured excluded code, Hystrix fallback is skipped and the RPC error is returned. Panics raised during the RPC invocation are captured and reported to the notifier before being converted into an error. If the RPC itself returns an error, that error is returned; otherwise any error produced by Hystrix is returned.

func NRHttpTracer

func NRHttpTracer(pattern string, h http.HandlerFunc) (string, http.HandlerFunc)

NRHttpTracer adds newrelic tracing to this http function

func NewRelicClientInterceptor

func NewRelicClientInterceptor() grpc.UnaryClientInterceptor

NewRelicClientInterceptor intercepts all client actions and reports them to newrelic

func NewRelicInterceptor

func NewRelicInterceptor() grpc.UnaryServerInterceptor

NewRelicInterceptor intercepts all server actions and reports them to newrelic

func OptionsInterceptor

func OptionsInterceptor() grpc.UnaryServerInterceptor

func PanicRecoveryInterceptor

func PanicRecoveryInterceptor() grpc.UnaryServerInterceptor

func ResponseTimeLoggingInterceptor

func ResponseTimeLoggingInterceptor(ff FilterFunc) grpc.UnaryServerInterceptor

ResponseTimeLoggingInterceptor logs response time for each request on server

func ResponseTimeLoggingStreamInterceptor

func ResponseTimeLoggingStreamInterceptor() grpc.StreamServerInterceptor

ResponseTimeLoggingStreamInterceptor logs response time for stream RPCs.

func ServerErrorInterceptor

func ServerErrorInterceptor() grpc.UnaryServerInterceptor

ServerErrorInterceptor intercepts all server actions and reports them to error notifier

func ServerErrorStreamInterceptor

func ServerErrorStreamInterceptor() grpc.StreamServerInterceptor

ServerErrorStreamInterceptor intercepts server errors for stream RPCs and reports them to the error notifier.

func SetClientMetricsOptions

func SetClientMetricsOptions(opts ...grpcprom.ClientMetricsOption)

SetClientMetricsOptions appends gRPC client metrics options. Must be called during initialization, before the server starts. Not safe for concurrent use.

func SetFilterFunc

func SetFilterFunc(ctx context.Context, ff FilterFunc)

SetFilterFunc sets the default filter function to be used by interceptors. Must be called during initialization, before the server starts. Not safe for concurrent use.

func SetResponseTimeLogLevel

func SetResponseTimeLogLevel(ctx context.Context, level loggers.Level)

SetResponseTimeLogLevel sets the log level for response time logging. Default is InfoLevel. Must be called during initialization, before the server starts. Not safe for concurrent use.

func SetServerMetricsOptions

func SetServerMetricsOptions(opts ...grpcprom.ServerMetricsOption)

SetServerMetricsOptions appends gRPC server metrics options (histogram, labels, namespace, etc.). Must be called during initialization, before the server starts. Not safe for concurrent use.

func TraceIdInterceptor

func TraceIdInterceptor() grpc.UnaryServerInterceptor

TraceIdInterceptor allows injecting trace id from request objects

func UseColdBrewClientInterceptors

func UseColdBrewClientInterceptors(ctx context.Context, flag bool)

UseColdBrewClientInterceptors allows enabling/disabling coldbrew client interceptors. When set to false, the coldbrew client interceptors will not be used. Must be called during initialization, before any RPCs are made. Not safe for concurrent use.

func UseColdBrewServerInterceptors

func UseColdBrewServerInterceptors(ctx context.Context, flag bool)

UseColdBrewServerInterceptors allows enabling/disabling coldbrew server interceptors. When set to false, the coldbrew server interceptors will not be used. Must be called during initialization, before the server starts. Not safe for concurrent use.

type FilterFunc

If it returns false, the given request will not be traced.

type FilterFunc func(ctx context.Context, fullMethodName string) bool

Generated by gomarkdoc

Documentation

Overview

Package interceptors provides gRPC server and client interceptors for the ColdBrew framework.

Interceptor configuration functions (AddUnaryServerInterceptor, SetFilterFunc, etc.) must be called during program initialization, before the gRPC server starts. They are not safe for concurrent use.

Index

Constants

View Source
const SupportPackageIsVersion1 = true

SupportPackageIsVersion1 is a compile-time assertion constant. Downstream packages (e.g. core) reference this constant to enforce version compatibility. When interceptors makes a breaking change, export a new constant and remove this one to force coordinated updates.

Variables

View Source
var (
	// Deprecated: FilterMethods is the list of methods that are filtered by default.
	// Use SetFilterMethods instead. Only some direct mutations (replacing the slice
	// or changing the first element) are detected by internal change detection;
	// other in-place changes may not invalidate caches correctly.
	FilterMethods = []string{"healthcheck", "readycheck", "serverreflectioninfo"}
)

Functions

func AddStreamClientInterceptor added in v0.1.5

func AddStreamClientInterceptor(ctx context.Context, i ...grpc.StreamClientInterceptor)

AddStreamClientInterceptor adds a client stream interceptor to default client stream interceptors. Must be called during initialization, before any RPCs are made. Not safe for concurrent use.

func AddStreamServerInterceptor added in v0.1.5

func AddStreamServerInterceptor(ctx context.Context, i ...grpc.StreamServerInterceptor)

AddStreamServerInterceptor adds a server interceptor to default server interceptors. Must be called during initialization, before the server starts. Not safe for concurrent use.

func AddUnaryClientInterceptor added in v0.1.5

func AddUnaryClientInterceptor(ctx context.Context, i ...grpc.UnaryClientInterceptor)

AddUnaryClientInterceptor adds a client interceptor to default client interceptors. Must be called during initialization, before any RPCs are made. Not safe for concurrent use.

func AddUnaryServerInterceptor added in v0.1.5

func AddUnaryServerInterceptor(ctx context.Context, i ...grpc.UnaryServerInterceptor)

AddUnaryServerInterceptor adds a server interceptor to default server interceptors. Must be called during initialization, before the server starts. Not safe for concurrent use.

func DebugLoggingInterceptor

func DebugLoggingInterceptor() grpc.UnaryServerInterceptor

DebugLoggingInterceptor is the interceptor that logs all request/response from a handler

func DefaultClientInterceptor

func DefaultClientInterceptor(defaultOpts ...interface{}) grpc.UnaryClientInterceptor

DefaultClientInterceptor are the set of default interceptors that should be applied to all client calls

func DefaultClientInterceptors

func DefaultClientInterceptors(defaultOpts ...interface{}) []grpc.UnaryClientInterceptor

DefaultClientInterceptors are the set of default interceptors that should be applied to all client calls

func DefaultClientStreamInterceptor added in v0.1.4

func DefaultClientStreamInterceptor(defaultOpts ...interface{}) grpc.StreamClientInterceptor

DefaultClientStreamInterceptor are the set of default interceptors that should be applied to all stream client calls

func DefaultClientStreamInterceptors added in v0.1.4

func DefaultClientStreamInterceptors(defaultOpts ...interface{}) []grpc.StreamClientInterceptor

DefaultClientStreamInterceptors are the set of default interceptors that should be applied to all stream client calls

func DefaultInterceptors

func DefaultInterceptors() []grpc.UnaryServerInterceptor

DefaultInterceptors are the set of default interceptors that are applied to all coldbrew methods

func DefaultStreamInterceptors

func DefaultStreamInterceptors() []grpc.StreamServerInterceptor

DefaultStreamInterceptors are the set of default interceptors that should be applied to all coldbrew streams

func DoHTTPtoGRPC added in v0.1.2

func DoHTTPtoGRPC(ctx context.Context, svr interface{}, handler func(ctx context.Context, req interface{}) (interface{}, error), in interface{}) (interface{}, error)

func FilterMethodsFunc

func FilterMethodsFunc(ctx context.Context, fullMethodName string) bool

FilterMethodsFunc is the default implementation of Filter function

func GRPCClientInterceptor deprecated

func GRPCClientInterceptor(_ ...interface{}) grpc.UnaryClientInterceptor

Deprecated: GRPCClientInterceptor is no longer needed. gRPC tracing is now handled by otelgrpc.NewClientHandler stats handler configured at the client level. This function is retained for backwards compatibility but returns a no-op interceptor.

func HystrixClientInterceptor

func HystrixClientInterceptor(defaultOpts ...grpc.CallOption) grpc.UnaryClientInterceptor

HystrixClientInterceptor returns a unary client interceptor that executes the RPC inside a Hystrix command.

Note: This interceptor wraps github.com/afex/hystrix-go which has been unmaintained since 2018. Consider migrating to github.com/failsafe-go/failsafe-go for circuit breaker functionality.

The interceptor applies provided default and per-call client options to configure Hystrix behavior (for example the command name, disabled flag, excluded errors, and excluded gRPC status codes). If Hystrix is disabled via options, the RPC is invoked directly. If the underlying RPC returns an error that matches any configured excluded error or whose gRPC status code matches any configured excluded code, Hystrix fallback is skipped and the RPC error is returned. Panics raised during the RPC invocation are captured and reported to the notifier before being converted into an error. If the RPC itself returns an error, that error is returned; otherwise any error produced by Hystrix is returned.

func NRHttpTracer

func NRHttpTracer(pattern string, h http.HandlerFunc) (string, http.HandlerFunc)

NRHttpTracer adds newrelic tracing to this http function

func NewRelicClientInterceptor

func NewRelicClientInterceptor() grpc.UnaryClientInterceptor

NewRelicClientInterceptor intercepts all client actions and reports them to newrelic. When NewRelic app is nil (no license key configured), returns a pass-through interceptor to avoid overhead.

func NewRelicInterceptor

func NewRelicInterceptor() grpc.UnaryServerInterceptor

NewRelicInterceptor intercepts all server actions and reports them to newrelic. When NewRelic app is nil (no license key configured), returns a pass-through interceptor to avoid overhead.

func OptionsInterceptor

func OptionsInterceptor() grpc.UnaryServerInterceptor

func PanicRecoveryInterceptor

func PanicRecoveryInterceptor() grpc.UnaryServerInterceptor

func ResponseTimeLoggingInterceptor

func ResponseTimeLoggingInterceptor(ff FilterFunc) grpc.UnaryServerInterceptor

ResponseTimeLoggingInterceptor logs response time for each request on server

func ResponseTimeLoggingStreamInterceptor

func ResponseTimeLoggingStreamInterceptor() grpc.StreamServerInterceptor

ResponseTimeLoggingStreamInterceptor logs response time for stream RPCs.

func ServerErrorInterceptor

func ServerErrorInterceptor() grpc.UnaryServerInterceptor

ServerErrorInterceptor intercepts all server actions and reports them to error notifier

func ServerErrorStreamInterceptor

func ServerErrorStreamInterceptor() grpc.StreamServerInterceptor

ServerErrorStreamInterceptor intercepts server errors for stream RPCs and reports them to the error notifier.

func SetClientMetricsOptions added in v0.1.12

func SetClientMetricsOptions(opts ...grpcprom.ClientMetricsOption)

SetClientMetricsOptions appends gRPC client metrics options. Must be called during initialization, before the server starts. Not safe for concurrent use.

func SetFilterFunc

func SetFilterFunc(ctx context.Context, ff FilterFunc)

SetFilterFunc sets the default filter function to be used by interceptors. Must be called during initialization, before the server starts. Not safe for concurrent use.

func SetFilterMethods added in v0.1.14

func SetFilterMethods(ctx context.Context, methods []string)

SetFilterMethods sets the list of method substrings to exclude from tracing/logging. It rebuilds the internal cache. Must be called during initialization, before the server starts. Not safe for concurrent use.

func SetResponseTimeLogErrorOnly added in v0.1.15

func SetResponseTimeLogErrorOnly(errorOnly bool)

SetResponseTimeLogErrorOnly when set to true, only logs response time when the request returns an error. Successful requests are not logged. Must be called during initialization, before the server starts. Not safe for concurrent use.

func SetResponseTimeLogLevel added in v0.1.11

func SetResponseTimeLogLevel(ctx context.Context, level loggers.Level)

SetResponseTimeLogLevel sets the log level for response time logging. Default is InfoLevel. Must be called during initialization, before the server starts. Not safe for concurrent use.

func SetServerMetricsOptions added in v0.1.12

func SetServerMetricsOptions(opts ...grpcprom.ServerMetricsOption)

SetServerMetricsOptions appends gRPC server metrics options (histogram, labels, namespace, etc.). Must be called during initialization, before the server starts. Not safe for concurrent use.

func TraceIdInterceptor added in v0.1.2

func TraceIdInterceptor() grpc.UnaryServerInterceptor

TraceIdInterceptor allows injecting trace id from request objects

func UseColdBrewClientInterceptors added in v0.1.5

func UseColdBrewClientInterceptors(ctx context.Context, flag bool)

UseColdBrewClientInterceptors allows enabling/disabling coldbrew client interceptors. When set to false, the coldbrew client interceptors will not be used. Must be called during initialization, before any RPCs are made. Not safe for concurrent use.

func UseColdBrewServerInterceptors added in v0.1.5

func UseColdBrewServerInterceptors(ctx context.Context, flag bool)

UseColdBrewServerInterceptors allows enabling/disabling coldbrew server interceptors. When set to false, the coldbrew server interceptors will not be used. Must be called during initialization, before the server starts. Not safe for concurrent use.

func WithHystrix

func WithHystrix() clientOption

WithHystrix enables hystrix

func WithHystrixExcludedCodes added in v0.1.9

func WithHystrixExcludedCodes(codes ...codes.Code) clientOption

WithHystrixExcludedCodes returns a clientOption that appends the provided gRPC codes to the list of codes excluded from the Hystrix circuit breaker.

func WithHystrixExcludedErrors added in v0.1.8

func WithHystrixExcludedErrors(errors ...error) clientOption

WithHystrixExcludedErrors returns a clientOption that adds the provided errors to the list of errors excluded from the Hystrix circuit breaker.

func WithHystrixName

func WithHystrixName(name string) clientOption

WithHystrixName creates a clientOption that sets the Hystrix command name used by client interceptors. If name is empty, the existing Hystrix name is left unchanged.

func WithoutHystrix

func WithoutHystrix() clientOption

WithoutHystrix disables hystrix

Types

type FilterFunc

type FilterFunc func(ctx context.Context, fullMethodName string) bool

If it returns false, the given request will not be traced.

Jump to

Keyboard shortcuts

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