Documentation
¶
Overview ¶
Package interceptor provides gRPC interceptors for logging, metrics collection, panic recovery, and request ID handling. These interceptors can be used to enhance gRPC services with observability and reliability features.
Index ¶
- Constants
- Variables
- func DefaultInFlightLimitStreamOnError(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, ...) error
- func DefaultInFlightLimitStreamOnReject(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, ...) error
- func DefaultInFlightLimitStreamOnRejectInDryRun(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, ...) error
- func DefaultInFlightLimitUnaryOnError(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, ...) (interface{}, error)
- func DefaultInFlightLimitUnaryOnReject(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, ...) (interface{}, error)
- func DefaultInFlightLimitUnaryOnRejectInDryRun(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, ...) (interface{}, error)
- func DefaultRateLimitStreamOnError(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, ...) error
- func DefaultRateLimitStreamOnReject(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, ...) error
- func DefaultRateLimitStreamOnRejectInDryRun(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, ...) error
- func DefaultRateLimitUnaryOnError(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, ...) (interface{}, error)
- func DefaultRateLimitUnaryOnReject(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, ...) (interface{}, error)
- func DefaultRateLimitUnaryOnRejectInDryRun(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, ...) (interface{}, error)
- func GetCallStartTimeFromContext(ctx context.Context) time.Time
- func GetInternalRequestIDFromContext(ctx context.Context) string
- func GetLoggerFromContext(ctx context.Context) log.FieldLogger
- func GetRequestIDFromContext(ctx context.Context) string
- func GetTraceIDFromContext(ctx context.Context) string
- func InFlightLimitStreamInterceptor(limit int, options ...InFlightLimitOption) (...)
- func InFlightLimitUnaryInterceptor(limit int, options ...InFlightLimitOption) (...)
- func LoggingStreamInterceptor(logger log.FieldLogger, options ...LoggingOption) ...
- func LoggingUnaryInterceptor(logger log.FieldLogger, options ...LoggingOption) ...
- func MetricsStreamInterceptor(collector MetricsCollector, opts ...MetricsOption) grpc.StreamServerInterceptor
- func MetricsUnaryInterceptor(collector MetricsCollector, opts ...MetricsOption) grpc.UnaryServerInterceptor
- func NewContextWithCallStartTime(ctx context.Context, startTime time.Time) context.Context
- func NewContextWithInternalRequestID(ctx context.Context, internalRequestID string) context.Context
- func NewContextWithLogger(ctx context.Context, logger log.FieldLogger) context.Context
- func NewContextWithLoggingParams(ctx context.Context, loggingParams *LoggingParams) context.Context
- func NewContextWithMetricsParams(ctx context.Context, metricsParams *MetricsParams) context.Context
- func NewContextWithRequestID(ctx context.Context, requestID string) context.Context
- func NewContextWithTraceID(ctx context.Context, traceID string) context.Context
- func RateLimitStreamInterceptor(maxRate Rate, options ...RateLimitOption) (...)
- func RateLimitUnaryInterceptor(maxRate Rate, options ...RateLimitOption) (...)
- func RecoveryStreamInterceptor(options ...RecoveryOption) ...
- func RecoveryUnaryInterceptor(options ...RecoveryOption) ...
- func RequestIDStreamInterceptor(options ...RequestIDOption) ...
- func RequestIDUnaryInterceptor(options ...RequestIDOption) ...
- type CallInfoMetrics
- type CallMethodType
- type InFlightLimitOption
- func WithInFlightLimitBacklogLimit(backlogLimit int) InFlightLimitOption
- func WithInFlightLimitBacklogTimeout(backlogTimeout time.Duration) InFlightLimitOption
- func WithInFlightLimitDryRun(dryRun bool) InFlightLimitOption
- func WithInFlightLimitMaxKeys(maxKeys int) InFlightLimitOption
- func WithInFlightLimitStreamGetKey(getKey InFlightLimitStreamGetKeyFunc) InFlightLimitOption
- func WithInFlightLimitStreamGetRetryAfter(getRetryAfter InFlightLimitStreamGetRetryAfterFunc) InFlightLimitOption
- func WithInFlightLimitStreamOnError(onError InFlightLimitStreamOnErrorFunc) InFlightLimitOption
- func WithInFlightLimitStreamOnReject(onReject InFlightLimitStreamOnRejectFunc) InFlightLimitOption
- func WithInFlightLimitStreamOnRejectInDryRun(onReject InFlightLimitStreamOnRejectFunc) InFlightLimitOption
- func WithInFlightLimitUnaryGetKey(getKey InFlightLimitUnaryGetKeyFunc) InFlightLimitOption
- func WithInFlightLimitUnaryGetRetryAfter(getRetryAfter InFlightLimitUnaryGetRetryAfterFunc) InFlightLimitOption
- func WithInFlightLimitUnaryOnError(onError InFlightLimitUnaryOnErrorFunc) InFlightLimitOption
- func WithInFlightLimitUnaryOnReject(onReject InFlightLimitUnaryOnRejectFunc) InFlightLimitOption
- func WithInFlightLimitUnaryOnRejectInDryRun(onReject InFlightLimitUnaryOnRejectFunc) InFlightLimitOption
- type InFlightLimitParams
- type InFlightLimitStreamGetKeyFunc
- type InFlightLimitStreamGetRetryAfterFunc
- type InFlightLimitStreamOnErrorFunc
- type InFlightLimitStreamOnRejectFunc
- type InFlightLimitUnaryGetKeyFunc
- type InFlightLimitUnaryGetRetryAfterFunc
- type InFlightLimitUnaryOnErrorFunc
- type InFlightLimitUnaryOnRejectFunc
- type LoggingOption
- func WithLoggingAddCallInfoToLogger(addCallInfo bool) LoggingOption
- func WithLoggingCallHeaders(headers map[string]string) LoggingOption
- func WithLoggingCallStart(logCallStart bool) LoggingOption
- func WithLoggingExcludedMethods(methods ...string) LoggingOption
- func WithLoggingSlowCallThreshold(threshold time.Duration) LoggingOption
- func WithLoggingStreamCustomLoggerProvider(provider StreamCustomLoggerProvider) LoggingOption
- func WithLoggingTimeSlotsThreshold(threshold time.Duration) LoggingOption
- func WithLoggingUnaryCustomLoggerProvider(provider UnaryCustomLoggerProvider) LoggingOption
- type LoggingParams
- type MetricsCollector
- type MetricsOption
- type MetricsParams
- type PrometheusMetrics
- func (pm *PrometheusMetrics) DecInFlightCalls(callInfo CallInfoMetrics, methodType CallMethodType)
- func (pm *PrometheusMetrics) IncInFlightCalls(callInfo CallInfoMetrics, methodType CallMethodType)
- func (pm *PrometheusMetrics) MustCurryWith(labels prometheus.Labels) *PrometheusMetrics
- func (pm *PrometheusMetrics) MustRegister()
- func (pm *PrometheusMetrics) ObserveCallFinish(callInfo CallInfoMetrics, methodType CallMethodType, code codes.Code, ...)
- func (pm *PrometheusMetrics) Unregister()
- type PrometheusOption
- func WithPrometheusConstLabels(labels prometheus.Labels) PrometheusOption
- func WithPrometheusCurriedLabelNames(labelNames []string) PrometheusOption
- func WithPrometheusCustomLabelNames(labelNames []string) PrometheusOption
- func WithPrometheusDurationBuckets(buckets []float64) PrometheusOption
- func WithPrometheusNamespace(namespace string) PrometheusOption
- type Rate
- type RateLimitAlg
- type RateLimitOption
- func WithRateLimitAlg(alg RateLimitAlg) RateLimitOption
- func WithRateLimitBacklogLimit(backlogLimit int) RateLimitOption
- func WithRateLimitBacklogTimeout(backlogTimeout time.Duration) RateLimitOption
- func WithRateLimitDryRun(dryRun bool) RateLimitOption
- func WithRateLimitMaxBurst(maxBurst int) RateLimitOption
- func WithRateLimitMaxKeys(maxKeys int) RateLimitOption
- func WithRateLimitStreamGetKey(getKey RateLimitStreamGetKeyFunc) RateLimitOption
- func WithRateLimitStreamGetRetryAfter(getRetryAfter RateLimitStreamGetRetryAfterFunc) RateLimitOption
- func WithRateLimitStreamOnError(onError RateLimitStreamOnErrorFunc) RateLimitOption
- func WithRateLimitStreamOnReject(onReject RateLimitStreamOnRejectFunc) RateLimitOption
- func WithRateLimitStreamOnRejectInDryRun(onReject RateLimitStreamOnRejectFunc) RateLimitOption
- func WithRateLimitUnaryGetKey(getKey RateLimitUnaryGetKeyFunc) RateLimitOption
- func WithRateLimitUnaryGetRetryAfter(getRetryAfter RateLimitUnaryGetRetryAfterFunc) RateLimitOption
- func WithRateLimitUnaryOnError(onError RateLimitUnaryOnErrorFunc) RateLimitOption
- func WithRateLimitUnaryOnReject(onReject RateLimitUnaryOnRejectFunc) RateLimitOption
- func WithRateLimitUnaryOnRejectInDryRun(onReject RateLimitUnaryOnRejectFunc) RateLimitOption
- type RateLimitParams
- type RateLimitStreamGetKeyFunc
- type RateLimitStreamGetRetryAfterFunc
- type RateLimitStreamOnErrorFunc
- type RateLimitStreamOnRejectFunc
- type RateLimitUnaryGetKeyFunc
- type RateLimitUnaryGetRetryAfterFunc
- type RateLimitUnaryOnErrorFunc
- type RateLimitUnaryOnRejectFunc
- type RecoveryOption
- type RequestIDOption
- type StreamCustomLoggerProvider
- type StreamUserAgentTypeProvider
- type UnaryCustomLoggerProvider
- type UnaryUserAgentTypeProvider
- type WrappedServerStream
Constants ¶
const DefaultInFlightLimitMaxKeys = 10000
DefaultInFlightLimitMaxKeys is a default value of maximum keys number for the InFlightLimit interceptor.
const DefaultRateLimitBacklogTimeout = ratelimit.DefaultRateLimitBacklogTimeout
DefaultRateLimitBacklogTimeout determines how long the gRPC request may be in the backlog status.
const DefaultRateLimitMaxKeys = 10000
DefaultRateLimitMaxKeys is a default value of maximum keys number for the RateLimit interceptor.
const InFlightLimitLogFieldBacklogged = "in_flight_limit_backlogged"
InFlightLimitLogFieldBacklogged is the name of the logged field that indicates if the request was backlogged.
const InFlightLimitLogFieldKey = "in_flight_limit_key"
InFlightLimitLogFieldKey is the name of the logged field that contains a key for the in-flight limiting.
const RateLimitLogFieldKey = "rate_limit_key"
RateLimitLogFieldKey it is the name of the logged field that contains a key for the requests rate limiter.
const (
// RecoveryDefaultStackSize defines the default size of stack part which will be logged.
RecoveryDefaultStackSize = 8192
)
Variables ¶
var DefaultPrometheusDurationBuckets = []float64{0.01, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10, 30, 60, 150, 300, 600}
DefaultPrometheusDurationBuckets is default buckets into which observations of serving gRPC calls are counted.
var InternalError = status.Error(codes.Internal, "Internal error")
InternalError is the default error returned when a panic is recovered.
Functions ¶
func DefaultInFlightLimitStreamOnError ¶ added in v1.21.0
func DefaultInFlightLimitStreamOnError(
srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler, params InFlightLimitParams, err error,
) error
DefaultInFlightLimitStreamOnError sends gRPC error response when an error occurs during in-flight limiting in stream requests.
func DefaultInFlightLimitStreamOnReject ¶ added in v1.21.0
func DefaultInFlightLimitStreamOnReject(
srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler, params InFlightLimitParams,
) error
DefaultInFlightLimitStreamOnReject sends gRPC error response when the in-flight limit is exceeded for stream requests.
func DefaultInFlightLimitStreamOnRejectInDryRun ¶ added in v1.21.0
func DefaultInFlightLimitStreamOnRejectInDryRun(
srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler, params InFlightLimitParams,
) error
DefaultInFlightLimitStreamOnRejectInDryRun continues processing stream requests when in-flight limit is exceeded in dry run mode.
func DefaultInFlightLimitUnaryOnError ¶ added in v1.21.0
func DefaultInFlightLimitUnaryOnError( ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler, params InFlightLimitParams, err error, ) (interface{}, error)
DefaultInFlightLimitUnaryOnError sends gRPC error response when an error occurs during in-flight limiting in unary requests.
func DefaultInFlightLimitUnaryOnReject ¶ added in v1.21.0
func DefaultInFlightLimitUnaryOnReject( ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler, params InFlightLimitParams, ) (interface{}, error)
DefaultInFlightLimitUnaryOnReject sends gRPC error response when the in-flight limit is exceeded for unary requests.
func DefaultInFlightLimitUnaryOnRejectInDryRun ¶ added in v1.21.0
func DefaultInFlightLimitUnaryOnRejectInDryRun( ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler, params InFlightLimitParams, ) (interface{}, error)
DefaultInFlightLimitUnaryOnRejectInDryRun continues processing unary requests when in-flight limit is exceeded in dry run mode.
func DefaultRateLimitStreamOnError ¶ added in v1.21.0
func DefaultRateLimitStreamOnError(
srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler, params RateLimitParams, err error,
) error
DefaultRateLimitStreamOnError sends gRPC error response when an error occurs during rate limiting in stream requests.
func DefaultRateLimitStreamOnReject ¶ added in v1.21.0
func DefaultRateLimitStreamOnReject(
srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler, params RateLimitParams,
) error
DefaultRateLimitStreamOnReject sends gRPC error response when the rate limit is exceeded for stream requests.
func DefaultRateLimitStreamOnRejectInDryRun ¶ added in v1.21.0
func DefaultRateLimitStreamOnRejectInDryRun(
srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler, params RateLimitParams,
) error
DefaultRateLimitStreamOnRejectInDryRun continues processing stream requests when rate limit is exceeded in dry run mode.
func DefaultRateLimitUnaryOnError ¶ added in v1.21.0
func DefaultRateLimitUnaryOnError( ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler, params RateLimitParams, err error, ) (interface{}, error)
DefaultRateLimitUnaryOnError sends gRPC error response when an error occurs during rate limiting in unary requests.
func DefaultRateLimitUnaryOnReject ¶ added in v1.21.0
func DefaultRateLimitUnaryOnReject( ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler, params RateLimitParams, ) (interface{}, error)
DefaultRateLimitUnaryOnReject sends gRPC error response when the rate limit is exceeded for unary requests.
func DefaultRateLimitUnaryOnRejectInDryRun ¶ added in v1.21.0
func DefaultRateLimitUnaryOnRejectInDryRun( ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler, params RateLimitParams, ) (interface{}, error)
DefaultRateLimitUnaryOnRejectInDryRun continues processing unary requests when rate limit is exceeded in dry run mode.
func GetCallStartTimeFromContext ¶
GetCallStartTimeFromContext extracts request start time from the context.
func GetInternalRequestIDFromContext ¶
GetInternalRequestIDFromContext extracts internal request id from the context.
func GetLoggerFromContext ¶
func GetLoggerFromContext(ctx context.Context) log.FieldLogger
GetLoggerFromContext extracts logger from the context.
func GetRequestIDFromContext ¶
GetRequestIDFromContext extracts external request id from the context.
func GetTraceIDFromContext ¶
GetTraceIDFromContext extracts trace id from the context.
func InFlightLimitStreamInterceptor ¶ added in v1.21.0
func InFlightLimitStreamInterceptor(limit int, options ...InFlightLimitOption) (func( srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler, ) error, error)
InFlightLimitStreamInterceptor is a gRPC stream interceptor that limits the number of in-flight requests.
func InFlightLimitUnaryInterceptor ¶ added in v1.21.0
func InFlightLimitUnaryInterceptor(limit int, options ...InFlightLimitOption) (func( ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler, ) (interface{}, error), error)
InFlightLimitUnaryInterceptor is a gRPC unary interceptor that limits the number of in-flight requests.
func LoggingStreamInterceptor ¶
func LoggingStreamInterceptor(logger log.FieldLogger, options ...LoggingOption) func( srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler, ) error
LoggingStreamInterceptor is a gRPC stream interceptor that logs the start and end of each RPC call.
func LoggingUnaryInterceptor ¶
func LoggingUnaryInterceptor(logger log.FieldLogger, options ...LoggingOption) func( ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler, ) (interface{}, error)
LoggingUnaryInterceptor is a gRPC unary interceptor that logs the start and end of each RPC call.
func MetricsStreamInterceptor ¶
func MetricsStreamInterceptor( collector MetricsCollector, opts ...MetricsOption, ) grpc.StreamServerInterceptor
MetricsStreamInterceptor is an interceptor that collects metrics for incoming gRPC stream calls.
func MetricsUnaryInterceptor ¶
func MetricsUnaryInterceptor( collector MetricsCollector, opts ...MetricsOption, ) grpc.UnaryServerInterceptor
MetricsUnaryInterceptor is an interceptor that collects metrics for incoming gRPC calls.
func NewContextWithCallStartTime ¶
NewContextWithCallStartTime creates a new context with request start time.
func NewContextWithInternalRequestID ¶
NewContextWithInternalRequestID creates a new context with internal request id.
func NewContextWithLogger ¶
NewContextWithLogger creates a new context with logger.
func NewContextWithLoggingParams ¶
func NewContextWithLoggingParams(ctx context.Context, loggingParams *LoggingParams) context.Context
NewContextWithLoggingParams creates a new context with logging params.
func NewContextWithMetricsParams ¶ added in v1.26.0
func NewContextWithMetricsParams(ctx context.Context, metricsParams *MetricsParams) context.Context
NewContextWithMetricsParams creates a new context with metrics params.
func NewContextWithRequestID ¶
NewContextWithRequestID creates a new context with external request id.
func NewContextWithTraceID ¶
NewContextWithTraceID creates a new context with trace id.
func RateLimitStreamInterceptor ¶ added in v1.21.0
func RateLimitStreamInterceptor(maxRate Rate, options ...RateLimitOption) (func( srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler, ) error, error)
RateLimitStreamInterceptor is a gRPC stream interceptor that limits the rate of requests.
func RateLimitUnaryInterceptor ¶ added in v1.21.0
func RateLimitUnaryInterceptor(maxRate Rate, options ...RateLimitOption) (func( ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler, ) (interface{}, error), error)
RateLimitUnaryInterceptor is a gRPC unary interceptor that limits the rate of requests.
func RecoveryStreamInterceptor ¶
func RecoveryStreamInterceptor(options ...RecoveryOption) func( srv interface{}, ss grpc.ServerStream, _ *grpc.StreamServerInfo, handler grpc.StreamHandler, ) error
RecoveryStreamInterceptor is a gRPC stream interceptor that recovers from panics and returns Internal error.
func RecoveryUnaryInterceptor ¶
func RecoveryUnaryInterceptor(options ...RecoveryOption) func( ctx context.Context, req interface{}, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler, ) (interface{}, error)
RecoveryUnaryInterceptor is a gRPC unary interceptor that recovers from panics and returns Internal error.
func RequestIDStreamInterceptor ¶
func RequestIDStreamInterceptor(options ...RequestIDOption) func( srv interface{}, ss grpc.ServerStream, _ *grpc.StreamServerInfo, handler grpc.StreamHandler, ) error
RequestIDStreamInterceptor is a gRPC stream interceptor that extracts the request ID from the incoming context metadata and attaches it to the context. If the request ID is missing, a new one is generated.
func RequestIDUnaryInterceptor ¶
func RequestIDUnaryInterceptor(options ...RequestIDOption) func( ctx context.Context, req interface{}, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler, ) (interface{}, error)
RequestIDUnaryInterceptor is a gRPC unary interceptor that extracts the request ID from the incoming context metadata and attaches it to the context. If the request ID is missing, a new one is generated.
Types ¶
type CallInfoMetrics ¶
type CallInfoMetrics struct {
Service string
Method string
UserAgentType string
CustomValues map[string]string
}
CallInfoMetrics represents a call info for collecting metrics.
type CallMethodType ¶
type CallMethodType string
CallMethodType represents the type of gRPC method call.
const ( // CallMethodTypeUnary represents a unary gRPC method call. CallMethodTypeUnary CallMethodType = "unary" // CallMethodTypeStream represents a streaming gRPC method call. CallMethodTypeStream CallMethodType = "stream" )
type InFlightLimitOption ¶ added in v1.21.0
type InFlightLimitOption func(*inFlightLimitOptions)
InFlightLimitOption represents a configuration option for the in-flight limit interceptor.
func WithInFlightLimitBacklogLimit ¶ added in v1.21.0
func WithInFlightLimitBacklogLimit(backlogLimit int) InFlightLimitOption
WithInFlightLimitBacklogLimit sets the backlog limit for queuing requests.
func WithInFlightLimitBacklogTimeout ¶ added in v1.21.0
func WithInFlightLimitBacklogTimeout(backlogTimeout time.Duration) InFlightLimitOption
WithInFlightLimitBacklogTimeout sets the timeout for backlogged requests.
func WithInFlightLimitDryRun ¶ added in v1.21.0
func WithInFlightLimitDryRun(dryRun bool) InFlightLimitOption
WithInFlightLimitDryRun enables dry run mode where limits are checked but not enforced.
func WithInFlightLimitMaxKeys ¶ added in v1.21.0
func WithInFlightLimitMaxKeys(maxKeys int) InFlightLimitOption
WithInFlightLimitMaxKeys sets the maximum number of keys to track.
func WithInFlightLimitStreamGetKey ¶ added in v1.21.0
func WithInFlightLimitStreamGetKey(getKey InFlightLimitStreamGetKeyFunc) InFlightLimitOption
WithInFlightLimitStreamGetKey sets the function to extract in-flight limiting key from stream gRPC requests.
func WithInFlightLimitStreamGetRetryAfter ¶ added in v1.21.0
func WithInFlightLimitStreamGetRetryAfter(getRetryAfter InFlightLimitStreamGetRetryAfterFunc) InFlightLimitOption
WithInFlightLimitStreamGetRetryAfter sets the function to calculate retry-after value for stream requests.
func WithInFlightLimitStreamOnError ¶ added in v1.21.0
func WithInFlightLimitStreamOnError(onError InFlightLimitStreamOnErrorFunc) InFlightLimitOption
WithInFlightLimitStreamOnError sets the callback for handling in-flight limiting errors in stream requests.
func WithInFlightLimitStreamOnReject ¶ added in v1.21.0
func WithInFlightLimitStreamOnReject(onReject InFlightLimitStreamOnRejectFunc) InFlightLimitOption
WithInFlightLimitStreamOnReject sets the callback for handling rejected stream requests.
func WithInFlightLimitStreamOnRejectInDryRun ¶ added in v1.21.0
func WithInFlightLimitStreamOnRejectInDryRun(onReject InFlightLimitStreamOnRejectFunc) InFlightLimitOption
WithInFlightLimitStreamOnRejectInDryRun sets the callback for handling rejected stream requests in dry run mode.
func WithInFlightLimitUnaryGetKey ¶ added in v1.21.0
func WithInFlightLimitUnaryGetKey(getKey InFlightLimitUnaryGetKeyFunc) InFlightLimitOption
WithInFlightLimitUnaryGetKey sets the function to extract in-flight limiting key from unary gRPC requests.
func WithInFlightLimitUnaryGetRetryAfter ¶ added in v1.21.0
func WithInFlightLimitUnaryGetRetryAfter(getRetryAfter InFlightLimitUnaryGetRetryAfterFunc) InFlightLimitOption
WithInFlightLimitUnaryGetRetryAfter sets the function to calculate retry-after value for unary requests.
func WithInFlightLimitUnaryOnError ¶ added in v1.21.0
func WithInFlightLimitUnaryOnError(onError InFlightLimitUnaryOnErrorFunc) InFlightLimitOption
WithInFlightLimitUnaryOnError sets the callback for handling in-flight limiting errors in unary requests.
func WithInFlightLimitUnaryOnReject ¶ added in v1.21.0
func WithInFlightLimitUnaryOnReject(onReject InFlightLimitUnaryOnRejectFunc) InFlightLimitOption
WithInFlightLimitUnaryOnReject sets the callback for handling rejected unary requests.
func WithInFlightLimitUnaryOnRejectInDryRun ¶ added in v1.21.0
func WithInFlightLimitUnaryOnRejectInDryRun(onReject InFlightLimitUnaryOnRejectFunc) InFlightLimitOption
WithInFlightLimitUnaryOnRejectInDryRun sets the callback for handling rejected unary requests in dry run mode.
type InFlightLimitParams ¶ added in v1.21.0
type InFlightLimitParams struct {
UnaryGetRetryAfter InFlightLimitUnaryGetRetryAfterFunc
StreamGetRetryAfter InFlightLimitStreamGetRetryAfterFunc
Key string
RequestBacklogged bool
}
InFlightLimitParams contains data that relates to the in-flight limiting procedure and could be used for rejecting or handling an occurred error.
type InFlightLimitStreamGetKeyFunc ¶ added in v1.21.0
type InFlightLimitStreamGetKeyFunc func(srv interface{}, ss grpc.ServerStream,
info *grpc.StreamServerInfo) (key string, bypass bool, err error)
InFlightLimitStreamGetKeyFunc is a function that is called for getting key for in-flight limiting in stream requests.
type InFlightLimitStreamGetRetryAfterFunc ¶ added in v1.21.0
type InFlightLimitStreamGetRetryAfterFunc func(srv interface{}, ss grpc.ServerStream,
info *grpc.StreamServerInfo) time.Duration
InFlightLimitStreamGetRetryAfterFunc is a function that is called to get a value for retry-after header when the in-flight limit is exceeded in stream requests.
type InFlightLimitStreamOnErrorFunc ¶ added in v1.21.0
type InFlightLimitStreamOnErrorFunc func(srv interface{}, ss grpc.ServerStream,
info *grpc.StreamServerInfo, handler grpc.StreamHandler, params InFlightLimitParams, err error) error
InFlightLimitStreamOnErrorFunc is a function that is called when an error occurs during in-flight limiting in stream requests.
type InFlightLimitStreamOnRejectFunc ¶ added in v1.21.0
type InFlightLimitStreamOnRejectFunc func(srv interface{}, ss grpc.ServerStream,
info *grpc.StreamServerInfo, handler grpc.StreamHandler, params InFlightLimitParams) error
InFlightLimitStreamOnRejectFunc is a function that is called for rejecting gRPC stream request when the in-flight limit is exceeded.
type InFlightLimitUnaryGetKeyFunc ¶ added in v1.21.0
type InFlightLimitUnaryGetKeyFunc func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo) (key string, bypass bool, err error)
InFlightLimitUnaryGetKeyFunc is a function that is called for getting key for in-flight limiting in unary requests.
type InFlightLimitUnaryGetRetryAfterFunc ¶ added in v1.21.0
type InFlightLimitUnaryGetRetryAfterFunc func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo) time.Duration
InFlightLimitUnaryGetRetryAfterFunc is a function that is called to get a value for retry-after header when the in-flight limit is exceeded in unary requests.
type InFlightLimitUnaryOnErrorFunc ¶ added in v1.21.0
type InFlightLimitUnaryOnErrorFunc func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler, params InFlightLimitParams, err error) (interface{}, error)
InFlightLimitUnaryOnErrorFunc is a function that is called when an error occurs during in-flight limiting in unary requests.
type InFlightLimitUnaryOnRejectFunc ¶ added in v1.21.0
type InFlightLimitUnaryOnRejectFunc func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler, params InFlightLimitParams) (interface{}, error)
InFlightLimitUnaryOnRejectFunc is a function that is called for rejecting gRPC unary request when the in-flight limit is exceeded.
type LoggingOption ¶
type LoggingOption func(*loggingOptions)
LoggingOption represents a configuration option for the logging interceptor.
func WithLoggingAddCallInfoToLogger ¶
func WithLoggingAddCallInfoToLogger(addCallInfo bool) LoggingOption
WithLoggingAddCallInfoToLogger adds call information to the logger context.
func WithLoggingCallHeaders ¶
func WithLoggingCallHeaders(headers map[string]string) LoggingOption
WithLoggingCallHeaders specifies custom headers to log from gRPC metadata.
func WithLoggingCallStart ¶
func WithLoggingCallStart(logCallStart bool) LoggingOption
WithLoggingCallStart enables logging of call start events.
func WithLoggingExcludedMethods ¶
func WithLoggingExcludedMethods(methods ...string) LoggingOption
WithLoggingExcludedMethods specifies gRPC methods to exclude from logging.
func WithLoggingSlowCallThreshold ¶
func WithLoggingSlowCallThreshold(threshold time.Duration) LoggingOption
WithLoggingSlowCallThreshold sets the threshold for slow call detection.
func WithLoggingStreamCustomLoggerProvider ¶
func WithLoggingStreamCustomLoggerProvider(provider StreamCustomLoggerProvider) LoggingOption
WithLoggingStreamCustomLoggerProvider sets a custom logger provider function for stream interceptors.
func WithLoggingTimeSlotsThreshold ¶ added in v1.24.0
func WithLoggingTimeSlotsThreshold(threshold time.Duration) LoggingOption
WithLoggingTimeSlotsThreshold sets the threshold for adding time_slots structure for calls analysis.
func WithLoggingUnaryCustomLoggerProvider ¶
func WithLoggingUnaryCustomLoggerProvider(provider UnaryCustomLoggerProvider) LoggingOption
WithLoggingUnaryCustomLoggerProvider sets a custom logger provider function.
type LoggingParams ¶
type LoggingParams struct {
// contains filtered or unexported fields
}
LoggingParams stores parameters for the gRPC logging interceptor that may be modified dynamically by the other underlying interceptors/handlers.
func GetLoggingParamsFromContext ¶
func GetLoggingParamsFromContext(ctx context.Context) *LoggingParams
GetLoggingParamsFromContext extracts logging params from the context.
func (*LoggingParams) AddTimeSlotDurationInMs ¶
func (lp *LoggingParams) AddTimeSlotDurationInMs(name string, dur time.Duration)
AddTimeSlotDurationInMs sets (if new) or adds duration value in milliseconds to the element of the time_slots map
func (*LoggingParams) AddTimeSlotInt ¶
func (lp *LoggingParams) AddTimeSlotInt(name string, dur int64)
AddTimeSlotInt sets (if new) or adds duration value to the element of the time_slots map
func (*LoggingParams) ExtendFields ¶
func (lp *LoggingParams) ExtendFields(fields ...log.Field)
ExtendFields extends list of fields that will be logged by the logging interceptor.
type MetricsCollector ¶
type MetricsCollector interface {
// IncInFlightCalls increments the counter of in-flight calls.
IncInFlightCalls(callInfo CallInfoMetrics, methodType CallMethodType)
// DecInFlightCalls decrements the counter of in-flight calls.
DecInFlightCalls(callInfo CallInfoMetrics, methodType CallMethodType)
// ObserveCallFinish observes the duration of the call and the status code.
ObserveCallFinish(callInfo CallInfoMetrics, methodType CallMethodType, code codes.Code, startTime time.Time)
}
MetricsCollector is an interface for collecting metrics for incoming gRPC calls.
type MetricsOption ¶
type MetricsOption func(*metricsOptions)
MetricsOption is a function type for configuring the metrics interceptor.
func WithMetricsExcludedMethods ¶
func WithMetricsExcludedMethods(methods ...string) MetricsOption
WithMetricsExcludedMethods returns an option that excludes the specified methods from metrics collection.
func WithMetricsStreamUserAgentTypeProvider ¶
func WithMetricsStreamUserAgentTypeProvider(provider StreamUserAgentTypeProvider) MetricsOption
WithMetricsStreamUserAgentTypeProvider sets a user agent type provider for stream interceptors.
func WithMetricsUnaryUserAgentTypeProvider ¶
func WithMetricsUnaryUserAgentTypeProvider(provider UnaryUserAgentTypeProvider) MetricsOption
WithMetricsUnaryUserAgentTypeProvider sets a user agent type provider for unary interceptors.
type MetricsParams ¶ added in v1.26.0
type MetricsParams struct {
// contains filtered or unexported fields
}
MetricsParams stores parameters for the gRPC metrics interceptor that may be modified dynamically by the other underlying interceptors/handlers.
func GetMetricsParamsFromContext ¶ added in v1.26.0
func GetMetricsParamsFromContext(ctx context.Context) *MetricsParams
GetMetricsParamsFromContext extracts metrics params from the context.
func (*MetricsParams) SetValue ¶ added in v1.26.0
func (mp *MetricsParams) SetValue(name, value string)
SetValue adds a new label with the specified name and value. If a label with the same name already exists, it will be overwritten.
type PrometheusMetrics ¶
type PrometheusMetrics struct {
Durations *prometheus.HistogramVec
InFlight *prometheus.GaugeVec
// contains filtered or unexported fields
}
PrometheusMetrics represents collector of metrics for incoming gRPC calls.
func NewPrometheusMetrics ¶
func NewPrometheusMetrics(opts ...PrometheusOption) *PrometheusMetrics
NewPrometheusMetrics creates a new instance of PrometheusMetrics with the provided options.
func (*PrometheusMetrics) DecInFlightCalls ¶
func (pm *PrometheusMetrics) DecInFlightCalls(callInfo CallInfoMetrics, methodType CallMethodType)
DecInFlightCalls decrements the counter of in-flight calls.
func (*PrometheusMetrics) IncInFlightCalls ¶
func (pm *PrometheusMetrics) IncInFlightCalls(callInfo CallInfoMetrics, methodType CallMethodType)
IncInFlightCalls increments the counter of in-flight calls.
func (*PrometheusMetrics) MustCurryWith ¶
func (pm *PrometheusMetrics) MustCurryWith(labels prometheus.Labels) *PrometheusMetrics
MustCurryWith curries the metrics collector with the provided labels.
func (*PrometheusMetrics) MustRegister ¶
func (pm *PrometheusMetrics) MustRegister()
MustRegister does registration of metrics collector in Prometheus and panics if any error occurs.
func (*PrometheusMetrics) ObserveCallFinish ¶
func (pm *PrometheusMetrics) ObserveCallFinish( callInfo CallInfoMetrics, methodType CallMethodType, code codes.Code, startTime time.Time, )
ObserveCallFinish observes the duration of the call and the status code.
func (*PrometheusMetrics) Unregister ¶
func (pm *PrometheusMetrics) Unregister()
Unregister cancels registration of metrics collector in Prometheus.
type PrometheusOption ¶
type PrometheusOption func(*prometheusOptions)
PrometheusOption is a function type for configuring the metrics collector.
func WithPrometheusConstLabels ¶
func WithPrometheusConstLabels(labels prometheus.Labels) PrometheusOption
WithPrometheusConstLabels sets constant labels that will be applied to all metrics.
func WithPrometheusCurriedLabelNames ¶
func WithPrometheusCurriedLabelNames(labelNames []string) PrometheusOption
WithPrometheusCurriedLabelNames sets label names that will be curried.
func WithPrometheusCustomLabelNames ¶ added in v1.26.0
func WithPrometheusCustomLabelNames(labelNames []string) PrometheusOption
WithPrometheusCustomLabelNames sets custom label names that will be added to the metrics. Values for these labels provided via CallInfoMetrics.CustomValues when recording metrics. Values in CallInfoMetrics.CustomValues can be set dynamically during request processing using MetricsParams.SetValue() method. MetricsParams can be obtained from the context via GetMetricsParamsFromContext(). If a custom value is not set for a label name provided here, it will be recorded as an empty string in the metric. If a custom value is set with a name not listed here, it will be ignored.
func WithPrometheusDurationBuckets ¶
func WithPrometheusDurationBuckets(buckets []float64) PrometheusOption
WithPrometheusDurationBuckets sets the duration buckets for histogram metrics.
func WithPrometheusNamespace ¶
func WithPrometheusNamespace(namespace string) PrometheusOption
WithPrometheusNamespace sets the namespace for metrics.
type RateLimitAlg ¶ added in v1.21.0
type RateLimitAlg int
RateLimitAlg represents a type for specifying rate-limiting algorithm.
const ( RateLimitAlgLeakyBucket RateLimitAlg = iota RateLimitAlgSlidingWindow )
Supported rate-limiting algorithms.
type RateLimitOption ¶ added in v1.21.0
type RateLimitOption func(*rateLimitOptions)
RateLimitOption represents a configuration option for the rate limit interceptor.
func WithRateLimitAlg ¶ added in v1.21.0
func WithRateLimitAlg(alg RateLimitAlg) RateLimitOption
WithRateLimitAlg sets the rate limiting algorithm.
func WithRateLimitBacklogLimit ¶ added in v1.21.0
func WithRateLimitBacklogLimit(backlogLimit int) RateLimitOption
WithRateLimitBacklogLimit sets the backlog limit for queuing requests.
func WithRateLimitBacklogTimeout ¶ added in v1.21.0
func WithRateLimitBacklogTimeout(backlogTimeout time.Duration) RateLimitOption
WithRateLimitBacklogTimeout sets the timeout for backlogged requests.
func WithRateLimitDryRun ¶ added in v1.21.0
func WithRateLimitDryRun(dryRun bool) RateLimitOption
WithRateLimitDryRun enables dry run mode where limits are checked but not enforced.
func WithRateLimitMaxBurst ¶ added in v1.21.0
func WithRateLimitMaxBurst(maxBurst int) RateLimitOption
WithRateLimitMaxBurst sets the maximum burst size for leaky bucket algorithm.
func WithRateLimitMaxKeys ¶ added in v1.21.0
func WithRateLimitMaxKeys(maxKeys int) RateLimitOption
WithRateLimitMaxKeys sets the maximum number of keys to track.
func WithRateLimitStreamGetKey ¶ added in v1.21.0
func WithRateLimitStreamGetKey(getKey RateLimitStreamGetKeyFunc) RateLimitOption
WithRateLimitStreamGetKey sets the function to extract rate limiting key from stream gRPC requests.
func WithRateLimitStreamGetRetryAfter ¶ added in v1.21.0
func WithRateLimitStreamGetRetryAfter(getRetryAfter RateLimitStreamGetRetryAfterFunc) RateLimitOption
WithRateLimitStreamGetRetryAfter sets the function to calculate retry-after value for stream requests.
func WithRateLimitStreamOnError ¶ added in v1.21.0
func WithRateLimitStreamOnError(onError RateLimitStreamOnErrorFunc) RateLimitOption
WithRateLimitStreamOnError sets the callback for handling rate limiting errors in stream requests.
func WithRateLimitStreamOnReject ¶ added in v1.21.0
func WithRateLimitStreamOnReject(onReject RateLimitStreamOnRejectFunc) RateLimitOption
WithRateLimitStreamOnReject sets the callback for handling rejected stream requests.
func WithRateLimitStreamOnRejectInDryRun ¶ added in v1.21.0
func WithRateLimitStreamOnRejectInDryRun(onReject RateLimitStreamOnRejectFunc) RateLimitOption
WithRateLimitStreamOnRejectInDryRun sets the callback for handling rejected stream requests in dry run mode.
func WithRateLimitUnaryGetKey ¶ added in v1.21.0
func WithRateLimitUnaryGetKey(getKey RateLimitUnaryGetKeyFunc) RateLimitOption
WithRateLimitUnaryGetKey sets the function to extract rate limiting key from unary gRPC requests.
func WithRateLimitUnaryGetRetryAfter ¶ added in v1.21.0
func WithRateLimitUnaryGetRetryAfter(getRetryAfter RateLimitUnaryGetRetryAfterFunc) RateLimitOption
WithRateLimitUnaryGetRetryAfter sets the function to calculate retry-after value for unary requests.
func WithRateLimitUnaryOnError ¶ added in v1.21.0
func WithRateLimitUnaryOnError(onError RateLimitUnaryOnErrorFunc) RateLimitOption
WithRateLimitUnaryOnError sets the callback for handling rate limiting errors in unary requests.
func WithRateLimitUnaryOnReject ¶ added in v1.21.0
func WithRateLimitUnaryOnReject(onReject RateLimitUnaryOnRejectFunc) RateLimitOption
WithRateLimitUnaryOnReject sets the callback for handling rejected unary requests.
func WithRateLimitUnaryOnRejectInDryRun ¶ added in v1.21.0
func WithRateLimitUnaryOnRejectInDryRun(onReject RateLimitUnaryOnRejectFunc) RateLimitOption
WithRateLimitUnaryOnRejectInDryRun sets the callback for handling rejected unary requests in dry run mode.
type RateLimitParams ¶ added in v1.21.0
type RateLimitParams struct {
Key string
RequestBacklogged bool
EstimatedRetryAfter time.Duration
UnaryGetRetryAfter RateLimitUnaryGetRetryAfterFunc
StreamGetRetryAfter RateLimitStreamGetRetryAfterFunc
}
RateLimitParams contains data that relates to the rate limiting procedure and could be used for rejecting or handling an occurred error.
type RateLimitStreamGetKeyFunc ¶ added in v1.21.0
type RateLimitStreamGetKeyFunc func(srv interface{}, ss grpc.ServerStream,
info *grpc.StreamServerInfo) (key string, bypass bool, err error)
RateLimitStreamGetKeyFunc is a function that is called for getting key for rate limiting in stream requests.
type RateLimitStreamGetRetryAfterFunc ¶ added in v1.21.0
type RateLimitStreamGetRetryAfterFunc func(srv interface{}, ss grpc.ServerStream,
info *grpc.StreamServerInfo, estimatedTime time.Duration) time.Duration
RateLimitStreamGetRetryAfterFunc is a function that is called to get a value for retry-after header when the rate limit is exceeded in stream requests.
type RateLimitStreamOnErrorFunc ¶ added in v1.21.0
type RateLimitStreamOnErrorFunc func(srv interface{}, ss grpc.ServerStream,
info *grpc.StreamServerInfo, handler grpc.StreamHandler, params RateLimitParams, err error) error
RateLimitStreamOnErrorFunc is a function that is called when an error occurs during rate limiting in stream requests.
type RateLimitStreamOnRejectFunc ¶ added in v1.21.0
type RateLimitStreamOnRejectFunc func(srv interface{}, ss grpc.ServerStream,
info *grpc.StreamServerInfo, handler grpc.StreamHandler, params RateLimitParams) error
RateLimitStreamOnRejectFunc is a function that is called for rejecting gRPC stream request when the rate limit is exceeded.
type RateLimitUnaryGetKeyFunc ¶ added in v1.21.0
type RateLimitUnaryGetKeyFunc func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo) (key string, bypass bool, err error)
RateLimitUnaryGetKeyFunc is a function that is called for getting key for rate limiting in unary requests.
type RateLimitUnaryGetRetryAfterFunc ¶ added in v1.21.0
type RateLimitUnaryGetRetryAfterFunc func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, estimatedTime time.Duration) time.Duration
RateLimitUnaryGetRetryAfterFunc is a function that is called to get a value for retry-after header when the rate limit is exceeded in unary requests.
type RateLimitUnaryOnErrorFunc ¶ added in v1.21.0
type RateLimitUnaryOnErrorFunc func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler, params RateLimitParams, err error) (interface{}, error)
RateLimitUnaryOnErrorFunc is a function that is called when an error occurs during rate limiting in unary requests.
type RateLimitUnaryOnRejectFunc ¶ added in v1.21.0
type RateLimitUnaryOnRejectFunc func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler, params RateLimitParams) (interface{}, error)
RateLimitUnaryOnRejectFunc is a function that is called for rejecting gRPC unary request when the rate limit is exceeded.
type RecoveryOption ¶
type RecoveryOption func(*recoveryOptions)
RecoveryOption is a function type for configuring recoveryOptions.
func WithRecoveryStackSize ¶
func WithRecoveryStackSize(size int) RecoveryOption
WithRecoveryStackSize sets the stack size for logging stack traces.
type RequestIDOption ¶
type RequestIDOption func(*requestIDOptions)
RequestIDOption is a function type for configuring requestIDOptions.
func WithInternalRequestIDGenerator ¶
func WithInternalRequestIDGenerator(generator func() string) RequestIDOption
WithInternalRequestIDGenerator sets the function for generating internal request IDs.
func WithRequestIDGenerator ¶
func WithRequestIDGenerator(generator func() string) RequestIDOption
WithRequestIDGenerator sets the function for generating request IDs.
type StreamCustomLoggerProvider ¶
type StreamCustomLoggerProvider func(ctx context.Context, info *grpc.StreamServerInfo) log.FieldLogger
StreamCustomLoggerProvider returns a custom logger or nil based on the gRPC context and stream method info.
type StreamUserAgentTypeProvider ¶
type StreamUserAgentTypeProvider func(ctx context.Context, info *grpc.StreamServerInfo) string
StreamUserAgentTypeProvider returns a user agent type or empty string based on the gRPC context and stream method info.
type UnaryCustomLoggerProvider ¶
type UnaryCustomLoggerProvider func(ctx context.Context, info *grpc.UnaryServerInfo) log.FieldLogger
UnaryCustomLoggerProvider returns a custom logger or nil based on the gRPC context and method info.
type UnaryUserAgentTypeProvider ¶
type UnaryUserAgentTypeProvider func(ctx context.Context, info *grpc.UnaryServerInfo) string
UnaryUserAgentTypeProvider returns a user agent type or empty string based on the gRPC context and method info.
type WrappedServerStream ¶
type WrappedServerStream struct {
grpc.ServerStream
Ctx context.Context
}
WrappedServerStream wraps grpc.ServerStream to provide a custom context for the stream.
func (*WrappedServerStream) Context ¶
func (ss *WrappedServerStream) Context() context.Context
Context returns the custom context for the wrapped server stream.