grpcx

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package grpcx provides Kernel's standard gRPC runtime layer.

grpcx is a thin facade over gRPC interceptors and selected middleware from github.com/grpc-ecosystem/go-grpc-middleware/v2. Business services should use grpcx helpers instead of assembling raw interceptors by hand so request context propagation, internal/scoped token handling, protovalidate, metrics, logging and recovery stay consistent across services.

Index

Constants

View Source
const (
	HeaderRequestID       = "x-request-id"
	HeaderTraceID         = "x-trace-id"
	HeaderAuthzDecisionID = "x-authz-decision-id"
	HeaderAuthorization   = "authorization"
	HeaderBaggage         = "baggage"
	HeaderTraceparent     = "traceparent"
	HeaderTracestate      = "tracestate"
)

Variables

This section is empty.

Functions

func DialOptions

func DialOptions(conf ClientConfig) []grpc.DialOption

DialOptions builds grpc.DialOption values for Kernel gRPC clients. Add credentials/load-balancing options separately through ExtraDialOption or the caller's Dial call.

func HTTPValidate

HTTPValidate is a tiny adapter for BFF/go-http handlers that decode protobuf request messages before calling business logic.

func IncomingContext

func IncomingContext(ctx context.Context) context.Context

IncomingContext copies selected incoming metadata into contextx.

func NewValidator

NewValidator creates a protovalidate validator for services that want to validate protobuf request messages at HTTP, gRPC and BFF boundaries.

func OutgoingContext

func OutgoingContext(ctx context.Context) context.Context

OutgoingContext copies contextx values to outgoing metadata.

func ServerOptions

func ServerOptions(conf ServerConfig) []grpc.ServerOption

ServerOptions builds grpc.ServerOption values for Kernel services. It uses go-grpc-middleware recovery/protovalidate interceptors and otelgrpc stats handlers, while Kernel-specific context/log/metrics interceptors keep the business-facing API stable.

func StreamClientLoggingInterceptor

func StreamClientLoggingInterceptor(logger logx.Logger) grpc.StreamClientInterceptor

func StreamClientMetricsInterceptor

func StreamClientMetricsInterceptor(m metricsx.Manager) grpc.StreamClientInterceptor

func StreamContextClientInterceptor

func StreamContextClientInterceptor() grpc.StreamClientInterceptor

StreamContextClientInterceptor is the streaming counterpart of UnaryContextClientInterceptor.

func StreamContextServerInterceptor

func StreamContextServerInterceptor() grpc.StreamServerInterceptor

StreamContextServerInterceptor restores contextx values for streaming RPCs.

func StreamProtovalidateServerInterceptor

func StreamProtovalidateServerInterceptor(v protovalidate.Validator) grpc.StreamServerInterceptor

StreamProtovalidateServerInterceptor validates inbound streaming protobuf requests with Buf Protovalidate annotations.

func StreamServerLoggingInterceptor

func StreamServerLoggingInterceptor(logger logx.Logger) grpc.StreamServerInterceptor

func StreamServerMetricsInterceptor

func StreamServerMetricsInterceptor(m metricsx.Manager) grpc.StreamServerInterceptor

func UnaryClientLoggingInterceptor

func UnaryClientLoggingInterceptor(logger logx.Logger) grpc.UnaryClientInterceptor

func UnaryClientMetricsInterceptor

func UnaryClientMetricsInterceptor(m metricsx.Manager) grpc.UnaryClientInterceptor

func UnaryContextClientInterceptor

func UnaryContextClientInterceptor() grpc.UnaryClientInterceptor

UnaryContextClientInterceptor propagates trusted request context values from contextx into outgoing gRPC metadata. It never forwards browser cookies; if a scoped/internal token exists in contextx, it is sent as Authorization.

func UnaryContextServerInterceptor

func UnaryContextServerInterceptor() grpc.UnaryServerInterceptor

UnaryContextServerInterceptor restores contextx values from incoming gRPC metadata. Authentication tokens are only copied into contextx as opaque scoped/internal token values; verification belongs to auth interceptors.

func UnaryProtovalidateServerInterceptor

func UnaryProtovalidateServerInterceptor(v protovalidate.Validator) grpc.UnaryServerInterceptor

UnaryProtovalidateServerInterceptor validates inbound unary protobuf requests with Buf Protovalidate annotations.

func UnaryServerLoggingInterceptor

func UnaryServerLoggingInterceptor(logger logx.Logger) grpc.UnaryServerInterceptor

func UnaryServerMetricsInterceptor

func UnaryServerMetricsInterceptor(m metricsx.Manager) grpc.UnaryServerInterceptor

func ValidateProto

func ValidateProto(v protovalidate.Validator, msg proto.Message) error

ValidateProto validates a single protobuf message. Use this from generated go-http handlers or BFF logic after decoding the request and before authz or business logic.

Types

type ClientConfig

type ClientConfig struct {
	ServiceName string
	Logger      logx.Logger
	Metrics     metricsx.Manager
	Timeout     time.Duration

	EnableOTel      bool
	EnableContext   bool
	EnableLogging   bool
	EnableMetrics   bool
	EnableTimeout   bool
	EnableRetry     bool
	RetryMax        uint
	RetryCodes      []codes.Code
	ExtraUnary      []grpc.UnaryClientInterceptor
	ExtraStream     []grpc.StreamClientInterceptor
	ExtraDialOption []grpc.DialOption
}

ClientConfig defines the default Kernel gRPC client chain.

func DefaultClientConfig

func DefaultClientConfig(serviceName string) ClientConfig

type ServerConfig

type ServerConfig struct {
	ServiceName string
	Logger      logx.Logger
	Metrics     metricsx.Manager
	Validator   protovalidate.Validator

	EnableOTel        bool
	EnableContext     bool
	EnableLogging     bool
	EnableMetrics     bool
	EnableValidation  bool
	EnableRecovery    bool
	ExtraUnary        []grpc.UnaryServerInterceptor
	ExtraStream       []grpc.StreamServerInterceptor
	ExtraServerOption []grpc.ServerOption
}

ServerConfig defines the default Kernel gRPC server chain.

func DefaultServerConfig

func DefaultServerConfig(serviceName string) ServerConfig

DefaultServerConfig returns the standard gRPC server runtime profile.

Jump to

Keyboard shortcuts

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