Documentation
¶
Index ¶
- Constants
- func FromContext(ctx context.Context) string
- func HTTPMiddleware(next http.Handler) http.Handler
- func NewContext(ctx context.Context, id string) context.Context
- func NewHTTPMiddleware(opts ...Option) func(http.Handler) http.Handler
- func NewStreamServerInterceptor(opts ...Option) grpc.StreamServerInterceptor
- func NewUnaryServerInterceptor(opts ...Option) grpc.UnaryServerInterceptor
- func StreamServerInterceptor(srv any, ss grpc.ServerStream, _ *grpc.StreamServerInfo, ...) error
- func UnaryServerInterceptor(ctx context.Context, req any, _ *grpc.UnaryServerInfo, ...) (any, error)
- type IDGenerator
- type Option
Constants ¶
View Source
const Header = "X-Request-ID"
Variables ¶
This section is empty.
Functions ¶
func FromContext ¶
FromContext extracts the request ID from ctx.
func HTTPMiddleware ¶
HTTPMiddleware 注入或透传 X-Request-ID。 优先级:上游 Header > OTel trace ID > UUIDv7。 ID 会设置到响应 Header 并注入 request context。
func NewContext ¶
NewContext returns a copy of ctx with the given request ID attached.
func NewHTTPMiddleware ¶ added in v0.7.7
NewHTTPMiddleware 创建支持自定义选项的 requestid HTTP 中间件。
webserver.New(addr, mux, webserver.WithMiddleware(
requestid.NewHTTPMiddleware(requestid.WithIDGenerator(myGen)),
))
func NewStreamServerInterceptor ¶ added in v0.7.7
func NewStreamServerInterceptor(opts ...Option) grpc.StreamServerInterceptor
NewStreamServerInterceptor 创建支持自定义选项的 gRPC stream 拦截器。
func NewUnaryServerInterceptor ¶ added in v0.7.7
func NewUnaryServerInterceptor(opts ...Option) grpc.UnaryServerInterceptor
NewUnaryServerInterceptor 创建支持自定义选项的 gRPC unary 拦截器。
func StreamServerInterceptor ¶
func StreamServerInterceptor(srv any, ss grpc.ServerStream, _ *grpc.StreamServerInfo, handler grpc.StreamHandler) error
StreamServerInterceptor 为 gRPC streaming 调用透传或生成 x-request-id。
func UnaryServerInterceptor ¶
func UnaryServerInterceptor(ctx context.Context, req any, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error)
UnaryServerInterceptor 为 gRPC unary 调用透传或生成 x-request-id。
Types ¶
type IDGenerator ¶ added in v0.7.7
IDGenerator 根据当前 context 生成请求 ID。
type Option ¶ added in v0.7.7
type Option func(*config)
Option 配置 requestid 中间件的可选参数。
func WithIDGenerator ¶ added in v0.7.7
func WithIDGenerator(gen IDGenerator) Option
WithIDGenerator 自定义 ID 生成函数,替代默认的 trace_id > UUIDv7 策略。
Click to show internal directories.
Click to hide internal directories.