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 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 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 NewContextWithRequestID(ctx context.Context, requestID string) context.Context
- func NewContextWithTraceID(ctx context.Context, traceID string) context.Context
- func RecoveryStreamInterceptor(options ...RecoveryOption) ...
- func RecoveryUnaryInterceptor(options ...RecoveryOption) ...
- func RequestIDStreamInterceptor(options ...RequestIDOption) ...
- func RequestIDUnaryInterceptor(options ...RequestIDOption) ...
- type CallInfoMetrics
- type CallMethodType
- 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 WithLoggingUnaryCustomLoggerProvider(provider UnaryCustomLoggerProvider) LoggingOption
- type LoggingParams
- type MetricsCollector
- type MetricsOption
- 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
- type RecoveryOption
- type RequestIDOption
- type StreamCustomLoggerProvider
- type StreamUserAgentTypeProvider
- type UnaryCustomLoggerProvider
- type UnaryUserAgentTypeProvider
- type WrappedServerStream
Constants ¶
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 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 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 NewContextWithRequestID ¶
NewContextWithRequestID creates a new context with external request id.
func NewContextWithTraceID ¶
NewContextWithTraceID creates a new context with trace id.
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 ¶
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 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 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 PrometheusMetrics ¶
type PrometheusMetrics struct {
Durations *prometheus.HistogramVec
InFlight *prometheus.GaugeVec
}
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 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 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.